未解决的参考:BR(Android Studio) [英] Unresolved reference: BR (Android Studio)

查看:797
本文介绍了未解决的参考:BR(Android Studio)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的顶级build.gradle:

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"
    }
}

我的项目级别build.gradle:

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"
}

我还在每个Google文档的gradle.properties中添加了android.databinding.enableV2=true( https: //developer.android.com/topic/libraries/data-binding/start )

I also added android.databinding.enableV2=true to gradle.properties per Google documents (https://developer.android.com/topic/libraries/data-binding/start)

当我尝试运行时,它在我的BaseViewHolder类中显示Unresolved reference: BR错误.看来BR类已经正确生成,但是当我将鼠标悬停在类名上时,它也说在文件.../R.java中找到了重复的类.我做错了什么?

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

推荐答案

经过大量研究后,发现为了使用数据绑定库需要添加一些内容

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"
    }
}

应用程序build.gradle

apply plugin: 'kotlin-kapt'

android {
    dataBinding {
        enabled = true
    }
}

dependencies {
    kapt "com.android.databinding:compiler:$compiler_version"
}

kapt {
    generateStubs = true
}

添加数据绑定库(例如3rd-party Gradle plug-ins may be the causeClasspath entry points to a non-existent location)后,我开始出现一些警告.但是可以编译并正常运行

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

这篇关于未解决的参考:BR(Android Studio)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆