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

查看:43
本文介绍了未解决的参考: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 文档(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"
    }
}

app build.gradle

apply plugin: 'kotlin-kapt'

android {
    dataBinding {
        enabled = true
    }
}

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

kapt {
    generateStubs = true
}

我在添加数据绑定库后开始有一些警告,比如3rd-party Gradle插件可能是原因Classpath入口指向一个不存在的位置.但编译并运行良好

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天全站免登陆