在Android数据绑定中找不到符号BR [英] cannot find symbol BR in android data binding

查看:7386
本文介绍了在Android数据绑定中找不到符号BR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

和构建应用模块中的.gradle文件是

and the build.gradle file in app module is

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

布局文件是

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

片段是

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

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

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

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

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

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

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

推荐答案

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

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

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

你可以通过添加:
apt'com.android.databinding:compiler: 1.0-rc0 到你的毕业文件。

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

这篇关于在Android数据绑定中找不到符号BR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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