Issue With Databinding Using Kotlin For Android
Solution 1:
Edit: With Kotlin 1.1 and Kapt3 it works slightly different:
in your projectbuild.gradle:
buildscript {
ext {
...
plugin_version = "2.3.1"
kotlin_version = "1.1.2-3"
...
}
...
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.android.tools.build:gradle:$plugin_version"
...
}
}
and in your appbuild.gradle:
apply plugin: "kotlin-android"
apply plugin: "kotlin-kapt"
...
android {
...
dataBinding {
enabled = true
}
...
}
dependencies {
...
kapt "com.android.databinding:compiler:$plugin_version"
...
}
It's really important that the databinding compiler version and the plugin version are identical.
Also note that with kapt3, you shouldn't use the generateStubs flag anymore.
Old Answer
Having the Android Studio plugin enabled isn't enough, you also need to adjust your gradle files a little bit (add and apply the kotlin-gradle-plugin)
Here are excerpts of my gradle files with working Java and Kotlin Databinding
In your projectbuild.gradle:
buildscript {
...
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.5"
classpath 'com.android.tools.build:gradle:2.2.3'
...
}
}
and in your appbuild.gradle:
apply plugin: "kotlin-android"
...
android {
...
dataBinding {
enabled = true
}
...
}
kapt {
generateStubs = true
}
dependencies {
...
kapt "com.android.databinding:compiler:2.2.0"
...
}
(I'm using a newer version of the databinding compiler here, you probably should do this as well)
Solution 2:
Try to include the missing blocks in your gradle files with the help of the following reference gradle file source.
App level Build.Gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.example.adventure.abc"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin/com/dougritter/marvelmovies'
}
dataBinding {
enabled = true
}
}
kapt {
generateStubs = true
}
dependencies {
//Compatibility
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
kapt 'com.android.databinding:compiler:2.3.0'//Libraries
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-vector-drawable:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile project(':domain')
compile project(':androidutils')
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.jakewharton.timber:timber:4.5.1'
}
Project level Build.Gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.1.2-2'
repositories {
maven { url 'https://maven.google.com' }
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Solution 3:
You don’t need to include any extra libraries to enable view binding
remove kapt "androidx.lifecycle:lifecycle-compiler:2.2.0" and add
android {
..
buildFeatures {
viewBinding true
}
}
to build.gradle
Post a Comment for "Issue With Databinding Using Kotlin For Android"