Edit: With Kotlin 1.1 and Kapt3 it works slightly different:
in your project build.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 app build.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 project build.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 app build.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)