Reputation: 4664
My top level build.gradle:
buildscript {
ext.kotlin_version = '1.2.41'
ext.lifecycle_version = "1.1.1"
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
My project level build.gradle:
android {
...
dataBinding {
enabled = true
}
}
dependencies {
...
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
}
I also added android.databinding.enableV2=true
to gradle.properties per Google documents (https://developer.android.com/topic/libraries/data-binding/start)
When I try to run, it shows Unresolved reference: BR
error in my BaseViewHolder
class. It seems that BR
class has been properly generated but it also says duplicate class found in the file .../R.java
when I mouse over the class name. What have I done wrong?
fun bind(obj: Any) {
binding.setVariable(BR.obj, obj)
binding.executePendingBindings()
}
Upvotes: 28
Views: 23717
Reputation: 11
To anyone who faces this issue in Android Studio Chipmunk or above:
First add this in the App level Gradle to enable data binding. This is the obvious one but making sure just in case.
buildFeatures {
dataBinding true
}
Secondly add kotlin-kapt plugin in the App level Gradle.
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
}
BR class is generated but not referenced properly that is why Unresolved Reference is coming. By adding kotlin-kapt, it will fix this issue.
Upvotes: 1
Reputation: 546
I manually typed the line "binding.setVariable(BR.item, item)" and it worked for me. Android studio shows the suggestion of importing BR. Choose the one from your project. Also enable databinding = true in gradle.
Upvotes: 1
Reputation: 2811
Android Studio failed to import my BR class automatically. All solutions provided above failed. I just had to import the class manually, Android studio had created it correctly.
SAMPLE:
package your_packagename
import your_packagename.BR
import ...
...
I think this happened due to Copy and Paste. When i typed BR
manually, Android Stdio did the Automatic Import.
Upvotes: 45
Reputation: 3440
For Android Studio 3.3, Gradle 3.3.0 and Databinding v2, the only line that needs to be added to fix this issue is in your (app's or modules) build.gradle
:
apply plugin: "kotlin-kapt"
Upvotes: 60
Reputation: 4664
After researching quite a bit, turns out there are a couple things to add in order to use data binding library
buildscript {
ext {
compiler_version = '3.1.3'
}
dependencies {
classpath "com.android.tools.build:gradle:$compiler_version"
}
}
apply plugin: 'kotlin-kapt'
android {
dataBinding {
enabled = true
}
}
dependencies {
kapt "com.android.databinding:compiler:$compiler_version"
}
kapt {
generateStubs = true
}
I started to have some warnings after adding the data binding library, like 3rd-party Gradle plug-ins may be the cause
and Classpath entry points to a non-existent location
. But compiles and runs fine
Upvotes: 24