在android数据绑定中找不到符号BR

时间:2023-01-25 19:00:50

I want to use Android Data Binding and has configured according to the official document.

我想使用Android数据绑定并根据官方文档进行配置。

the error message is : error: cannot find symbol import net.juude.droidviews.BR;

错误信息是:错误:找不到符号导入net.juude.droidviews.BR;

the top level build.gradle file is:

*build.gradle文件是:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:1.3.0-beta2"
        classpath 'me.tatarka:gradle-retrolambda:3.0.1'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
        classpath "com.android.databinding:dataBinder:1.0-rc0"
    }
}
allprojects {
    repositories {
        jcenter()
        mavenCentral()
    }
}

and the build.gradle file in app module is

app模块中的build.gradle文件是

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'com.android.databinding'


android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    lintOptions {
        abortOnError false
    }

    defaultConfig {
        applicationId "net.juude.droidviews"
        minSdkVersion 15
        targetSdkVersion 22 
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    retrolambda {
        println("JAVA_HOME: " + System.getenv("JAVA_HOME"))
        println("JAVA7_HOME: " + System.getenv("JAVA7_HOME"))
        println("JAVA8_HOME: " + System.getenv("JAVA8_HOME"))
        javaVersion JavaVersion.VERSION_1_7
    }
}

dependencies {
    compile project(':ptr-lib-debug')
    compile project(':library')
    compile 'com.android.support:support-v4:22.1.0'
    compile 'com.mcxiaoke.volley:library:1.0.+'
    compile files('libs/droidmocks.jar')
    compile 'com.android.support:support-annotations:22.1.1'
    compile 'com.facebook.fresco:fresco:0.5.0'
    compile 'com.android.support:recyclerview-v7:22.1.1'
    compile 'io.reactivex:rxandroid:0.24.0'
    compile 'io.reactivex:rxjava:1.0.6'
    compile 'com.google.dagger:dagger:2.0'
    apt 'com.google.dagger:dagger-compiler:2.0'
}

the layout file is

布局文件是

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="user" type=" net.juude.droidviews.databinding.User" />
    </data>
    <LinearLayout
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/user_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.mName}"/>

        <TextView
            android:id="@+id/user_sex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.sex}"/>

    </LinearLayout>
</layout>

the fragment is

片段是

package net.juude.droidviews.databinding;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.juude.droidviews.BR;
import net.juude.droidviews.R;

/**
 * Created by juude on 15-6-9.
 */
public class DataBindingFragment extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewDataBinding dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_data_binding, container, false);
        //dataBinding.setVariable(BR.)
        User user = new User();
        user.setSex("男");
        user.setName("Juude");
        dataBinding.setVariable(BR.user, user);
        return dataBinding.getRoot();
    }
}

since I did not get syntax error in Android Studio, I can assume that the Android Studio can recognize the BR class;

由于我在Android Studio中没有得到语法错误,我可以假设Android Studio可以识别BR类;

But when I compile the project, It tells me that error:cannot find symbol class BR.

但是当我编译项目时,它告诉我错误:找不到符号类BR。

so I want to know what should I do to make it generate the right BR.java file?

所以我想知道我该怎么做才能生成正确的BR.java文件?

11 个解决方案

#1


19  

It is a problem caused by android-apt plugin. It ignores the provided dependency that data binding plugin adds.

这是android-apt插件引起的问题。它忽略了数据绑定插件添加的提供的依赖项。

https://bitbucket.org/hvisser/android-apt/issue/38/android-apt-breaks-brand-new-data-binding#comment-18504545

You can work around it by adding: apt 'com.android.databinding:compiler:1.0-rc0 to your gradle file.

你可以通过添加:apt'com.android.databinding:compiler:1.0-rc0到你的gradle文件来解决它。

#2


25  

In case someone still has this problem. I solved this by invalidating the cache in Android Studio. And I'm using Android Studio 2 with API 23

万一有人还有这个问题。我通过在Android Studio中使缓存无效来解决这个问题。我正在使用带有API 23的Android Studio 2

File > Invalidate Caches/Restart

文件>使高速缓存/重新启动无效

#3


6  

I find the cause of the problem in my case. I find in the import statements of my Data Object class contains:

在我的案例中,我找到了问题的原因。我发现在我的Data Object类的import语句中包含:

import static android.databinding.tool.util.GenerationalClassUtil.ExtensionFilter.BR;

The problem gets solved after I removed that. Android Studio somehow resolved BR class wrongly for me.

删除后,问题就解决了。 Android Studio以某种方式为我错误地解决了BR类。

#4


4  

The Bindable annotation generates an entry in the BR class file during compilation. The BR class file will be generated in the module package.

Bindable注释在编译期间在BR类文件中生成一个条目。 BR类文件将在模块包中生成。

@Bindable
   public String getFirstName() {
       return this.firstName;
   }

#5


3  

I too got the same error message, but my root cause was different from what has been shared here.

我也得到了相同的错误消息,但我的根本原因与此处分享的不同。

In my case the issue was that after I resolved a merge conflict, I had by mistake left an extra @Override annotation on a method. Thus the JVM preprocessor failed and then also the data binding classes were not generated.

在我的情况下,问题是在我解决了合并冲突后,我错误地在方法上留下了额外的@Override注释。因此,JVM预处理器失败,然后也没有生成数据绑定类。

The irritating part for me was that I was unable to see any error message related to this, or it was hard to spot among all the other compiler errors. And Android Studio does not show the extra annotation as an error either. So in case someone else struggles with the same error, check that you don't haven an error in some annotation that prevents the APT preprocessor from kicking in.

令我恼火的部分是我无法看到与此相关的任何错误消息,或者很难发现所有其他编译器错误。并且Android Studio也不会将额外注释显示为错误。因此,如果其他人遇到同样的错误,请检查一些注释中是否存在错误,导致APT预处理器无法启动。

#6


2  

I ran into this issue too. The problem was that I changed the class name and didn't update the values in the XML file. This includes the variable names and where they are accessed in the xml file.

我也遇到过这个问题。问题是我更改了类名,并没有更新XML文件中的值。这包括变量名称以及在xml文件中访问它们的位置。

#7


2  

Just restarting Android Studio, without invalidating cache, did the trick for me (I'm using version 2.2.3).

只是重新启动Android Studio,而不会使缓存失效,为我做了伎俩(我使用的是2.2.3版本)。

#8


2  

if you use Android Studio2.3 release and your project configuration is as follow:

如果您使用Android Studio2.3版本,您的项目配置如下:

Gradle version: 3.3
Android plubin version:2.3.0
Compile sdk Version:23+
build tools Version:25+

you only need to do is add

你只需要添加

dataBinding{enabled=true}

to build.gradle,than sync gradle file

build.gradle,而不是同步gradle文件

#9


1  

For my case the issue was caused by

对于我的情况,问题是由

"error: variable MyVariable was already defined in class MyModel"

“错误:变量MyVariable已在类MyModel中定义”

The annoying thing was Android Studio gradle build did not display this error at the bottom. I had to comment each databinding error > Rebuild Project until I reach the error. Lesson learnt was to scan through each error from bottom to top and notice anything you may have changed recently.

令人讨厌的是Android Studio gradle build在底部没有显示此错误。我不得不评论每个数据绑定错误>重建项目,直到我遇到错误。获得的经验教训是从下到上扫描每个错误,并注意最近可能发生的任何变化。

#10


1  

For me it helped to run ./gradlew clean build in the project folder. It showed an error which android studio didn't.

对我来说,它有助于在项目文件夹中运行./gradlew clean build。它显示了android studio没有的错误。

#11


0  

You need to add this to your gradle.properties file

您需要将其添加到gradle.properties文件中

android.databinding.enableV2=true

From developer.android.com

you might get multiple errors reporting that the binding classes aren't found. The new data binding compiler prevents these errors by generating the binding classes before the managed compiler builds your app.

您可能会收到多个错误报告未找到绑定类。新的数据绑定编译器通过在托管编译器构建应用程序之前生成绑定类来防止这些错误。

#1


19  

It is a problem caused by android-apt plugin. It ignores the provided dependency that data binding plugin adds.

这是android-apt插件引起的问题。它忽略了数据绑定插件添加的提供的依赖项。

https://bitbucket.org/hvisser/android-apt/issue/38/android-apt-breaks-brand-new-data-binding#comment-18504545

You can work around it by adding: apt 'com.android.databinding:compiler:1.0-rc0 to your gradle file.

你可以通过添加:apt'com.android.databinding:compiler:1.0-rc0到你的gradle文件来解决它。

#2


25  

In case someone still has this problem. I solved this by invalidating the cache in Android Studio. And I'm using Android Studio 2 with API 23

万一有人还有这个问题。我通过在Android Studio中使缓存无效来解决这个问题。我正在使用带有API 23的Android Studio 2

File > Invalidate Caches/Restart

文件>使高速缓存/重新启动无效

#3


6  

I find the cause of the problem in my case. I find in the import statements of my Data Object class contains:

在我的案例中,我找到了问题的原因。我发现在我的Data Object类的import语句中包含:

import static android.databinding.tool.util.GenerationalClassUtil.ExtensionFilter.BR;

The problem gets solved after I removed that. Android Studio somehow resolved BR class wrongly for me.

删除后,问题就解决了。 Android Studio以某种方式为我错误地解决了BR类。

#4


4  

The Bindable annotation generates an entry in the BR class file during compilation. The BR class file will be generated in the module package.

Bindable注释在编译期间在BR类文件中生成一个条目。 BR类文件将在模块包中生成。

@Bindable
   public String getFirstName() {
       return this.firstName;
   }

#5


3  

I too got the same error message, but my root cause was different from what has been shared here.

我也得到了相同的错误消息,但我的根本原因与此处分享的不同。

In my case the issue was that after I resolved a merge conflict, I had by mistake left an extra @Override annotation on a method. Thus the JVM preprocessor failed and then also the data binding classes were not generated.

在我的情况下,问题是在我解决了合并冲突后,我错误地在方法上留下了额外的@Override注释。因此,JVM预处理器失败,然后也没有生成数据绑定类。

The irritating part for me was that I was unable to see any error message related to this, or it was hard to spot among all the other compiler errors. And Android Studio does not show the extra annotation as an error either. So in case someone else struggles with the same error, check that you don't haven an error in some annotation that prevents the APT preprocessor from kicking in.

令我恼火的部分是我无法看到与此相关的任何错误消息,或者很难发现所有其他编译器错误。并且Android Studio也不会将额外注释显示为错误。因此,如果其他人遇到同样的错误,请检查一些注释中是否存在错误,导致APT预处理器无法启动。

#6


2  

I ran into this issue too. The problem was that I changed the class name and didn't update the values in the XML file. This includes the variable names and where they are accessed in the xml file.

我也遇到过这个问题。问题是我更改了类名,并没有更新XML文件中的值。这包括变量名称以及在xml文件中访问它们的位置。

#7


2  

Just restarting Android Studio, without invalidating cache, did the trick for me (I'm using version 2.2.3).

只是重新启动Android Studio,而不会使缓存失效,为我做了伎俩(我使用的是2.2.3版本)。

#8


2  

if you use Android Studio2.3 release and your project configuration is as follow:

如果您使用Android Studio2.3版本,您的项目配置如下:

Gradle version: 3.3
Android plubin version:2.3.0
Compile sdk Version:23+
build tools Version:25+

you only need to do is add

你只需要添加

dataBinding{enabled=true}

to build.gradle,than sync gradle file

build.gradle,而不是同步gradle文件

#9


1  

For my case the issue was caused by

对于我的情况,问题是由

"error: variable MyVariable was already defined in class MyModel"

“错误:变量MyVariable已在类MyModel中定义”

The annoying thing was Android Studio gradle build did not display this error at the bottom. I had to comment each databinding error > Rebuild Project until I reach the error. Lesson learnt was to scan through each error from bottom to top and notice anything you may have changed recently.

令人讨厌的是Android Studio gradle build在底部没有显示此错误。我不得不评论每个数据绑定错误>重建项目,直到我遇到错误。获得的经验教训是从下到上扫描每个错误,并注意最近可能发生的任何变化。

#10


1  

For me it helped to run ./gradlew clean build in the project folder. It showed an error which android studio didn't.

对我来说,它有助于在项目文件夹中运行./gradlew clean build。它显示了android studio没有的错误。

#11


0  

You need to add this to your gradle.properties file

您需要将其添加到gradle.properties文件中

android.databinding.enableV2=true

From developer.android.com

you might get multiple errors reporting that the binding classes aren't found. The new data binding compiler prevents these errors by generating the binding classes before the managed compiler builds your app.

您可能会收到多个错误报告未找到绑定类。新的数据绑定编译器通过在托管编译器构建应用程序之前生成绑定类来防止这些错误。