How to use Data Binding and Kotlin in Android Studio 3.0.0
It seems that you need 3 gradle entries in the app .gradle at module level to add data binding
apply plugin: 'kotlin-kapt'
android {
...
dataBinding {
enabled = true
}
}dependencies {
......
kapt "com.android.databinding:compiler:$compiler_version"
}
Notice that I made compiler version a variable in the project level build gradle so it can be managed from a single place
default was: ext.kotlin_version = '1.1.3-2'
I added with bracket syntax:
ext{
kotlin_version = '1.1.3-2'
compiler_version = '3.0.0-beta6'
}
Data Binding with Android Studio 3.0 Canary 3
Add these lines to your module-level build.gradle as dependencies; Android Studio 3.0 changed the way it pulls in libraries that you previously downloaded via the SDK manager - https://developer.android.com/studio/build/dependencies.html#google-maven
implementation 'com.android.databinding:library:1.3.1'
implementation 'com.android.databinding:adapters:1.3.1'
You may not need the second line, depending on how you use data binding.
Instead of implementation
, you can also use the old compile
. Using implementation
will avoid build warnings going forward; it's a part of the new Gradle Java library plugin that the Android Gradle plugin uses by default now.
You will also need to add something like the following (typically in your module-level build.gradle
) if you don't have it yet - this adds Google's new Maven repository for Android dependencies:
repositories {
maven {
url 'https://maven.google.com'
}
}
Can't build project with Android Studio 3.0 + DataBinding + Kotlin
I just tried with similar build.gradle
settings in a new and fresh kotlin project and it builds. Unfortunately I couldn't use all your libs and your exact setting but maybe it's just a matter of order. Here is my
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
kapt {
generateStubs = true
}
android {
compileSdkVersion 26
defaultConfig {
applicationId "nice.fontaine.kottest"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation 'com.jakewharton:butterknife:6.1.0'
annotationProcessor 'com.jakewharton:butterknife:6.1.0'
implementation('de.keyboardsurfer.android.widget:crouton:1.8.5@aar') {
exclude group: 'com.google.android', module: 'support-v4'
}
implementation 'com.google.dagger:dagger:2.10'
kapt 'com.google.dagger:dagger-compiler:2.10'
kapt 'com.android.databinding:compiler:3.0.0'
implementation 'de.greenrobot:eventbus:2.4.0'
implementation 'com.mcxiaoke.volley:library:1.0.19'
implementation 'com.google.code.gson:gson:2.8.0'
implementation "com.google.android.gms:play-services-places:11.6.0"
implementation "com.google.android.gms:play-services-plus:11.6.0"
implementation "com.google.android.gms:play-services-analytics:11.6.0"
implementation "com.google.android.gms:play-services-auth:11.6.0"
implementation "com.google.android.gms:play-services-gcm:11.6.0"
implementation "com.google.android.gms:play-services-ads:11.6.0"
implementation "com.android.support:appcompat-v7:26.1.0"
implementation "com.android.support:support-v13:26.1.0"
implementation "com.android.support:recyclerview-v7:26.1.0"
implementation "com.android.support:design:26.1.0"
implementation "com.android.support:support-annotations:26.1.0"
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:multidex:1.0.2'
implementation 'commons-io:commons-io:2.4'
implementation 'org.apache.commons:commons-lang3:3.1'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
implementation 'com.google.ads.mediation:facebook:4.26.1.0'
implementation 'com.facebook.android:audience-network-sdk:4.26.1'
implementation 'org.jboss.netty:netty:3.2.10.Final'
//Testing
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:1.10.19'
androidTestImplementation 'com.android.support:multidex:1.0.2'
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test.espresso:espresso-web:3.0.1'
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2') {
exclude group: 'com.android.support', module: 'support-annotations:22.2.1'
}
androidTestImplementation('com.android.support.test:runner:0.3') {
exclude group: 'com.android.support', module: 'support-annotations:22.2.1'
}
androidTestImplementation('com.android.support.test:rules:0.3') {
exclude group: 'com.android.support', module: 'support-annotations:22.2.1'
}
androidTestImplementation('com.android.support.test.espresso:espresso-intents:2.2') {
exclude group: 'com.android.support', module: 'support-annotations:22.2.1'
}
androidTestImplementation('com.android.support.test.espresso:espresso-contrib:2.2') {
exclude group: 'com.android.support', module: 'app-compat'
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.android.support', module: 'support-annotations:22.2.1'
exclude module: 'recyclerview-v7'
}
androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
androidTestImplementation "com.android.support:support-annotations:26.1.0"
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
debugImplementation 'com.letv.sarrsdesktop:BlockCanaryExJRT:0.9.5.3'
releaseImplementation 'com.letv.sarrsdesktop:BlockCanaryExJRTNoOp:0.9.5.3'
testImplementation 'com.letv.sarrsdesktop:BlockCanaryExJRTNoOp:0.9.5.3'
implementation 'me.mvdw.recyclerviewmergeadapter:recyclerviewmergeadapter:2.0.0'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.1'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
implementation "com.squareup.retrofit2:retrofit:2.3.0"
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
implementation "com.squareup.retrofit2:converter-gson:2.3.0"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
implementation 'io.reactivex.rxjava2:rxjava:2.1.2'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'com.gojuno.koptional:koptional-rxjava2-extensions:1.2.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.newrelic.agent.android:android-agent:5.14.0'
implementation 'org.jsoup:jsoup:1.10.3'
}
At least I can verify with this test that
kapt 'com.android.databinding:compiler:3.0.0'
is working for me. Maybe try it in a new project too and check if this works and then slowly apply your other libs one by one. Hope that gets you on track!
Do I need to add kapt com.android.databinding:compiler:$compiler_version gradle entry?
That dependency is automatically added by enabling data binding. I think there were some Kotlin-related problems a few years ago (your linked-to resources are from 2017 and 2018), but they are fixed now. You do not need to add it manually.
How can I format a string when I use data binding?
You can use static function and call it in you xml. for example in file
StringFormatter.kt
fun formatString(size: Int): String {
return "$size kb"
}
and then in you xml you can import it
<layout>
<data>
<import type="com.path.to.the.package.StringFormatter" />
// your viewmodel variable here
// etc
</data>
<TextView
android:id="@+id/tvFileSizeValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/myDetailContent"
android:text="@{StringFormatter.formatString(aDetailViewModel.size)}" />
</layout>
Related Topics
Android Error - Caused By: Java.Lang.Noclassdeffounderror: Android.Support.V4.Util.Sparsearraycompat
Hax Kernel Module Is Not Installed
How to Get Bluetooth Connected Devices Using Bluetoothheadset API
Android UI Design: Supporting Multiple Screens
Viewing Excel Files in My Android App
Send Intent from Service to Activity
Firebase Authentication State Change Does Not Fire When User Is Disabled or Deleted
Changing Gradient Background Colors on Android at Runtime
How to Send JSON Object to the Server from My Android App
How to Integrate Matlab Code Library with Android
How to Enable and Disable a Component
Android Custom Layout - Ondraw() Never Gets Called
Auto Start Application After Boot Completed in Android
Listview with Choice_Mode_Multiple Using Checkedtext in a Custom View
Android - How to Check If Developer Option Is Enabled
Saving Arraylists in SQLite Databases