Unresolved reference: BR (Android Studio)

Jack Guo picture Jack Guo · Jun 20, 2018 · Viewed 10.8k times · Source

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()
}

Answer

A. Steenbergen picture A. Steenbergen · Jan 15, 2019

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"