Juu*_*ude 40 data-binding android android-studio
我想使用Android数据绑定并根据官方文档进行配置.
错误信息是:错误:找不到符号导入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()
}
}
Run Code Online (Sandbox Code Playgroud)
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'
}
Run Code Online (Sandbox Code Playgroud)
布局文件是
<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>
Run Code Online (Sandbox Code Playgroud)
片段是
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();
}
}
Run Code Online (Sandbox Code Playgroud)
由于Android Studio中没有语法错误,我可以假设Android Studio可以识别BR类;
但是当我编译项目时,它告诉我
error:cannot find symbol class BR.
所以我想知道我该怎么做才能生成正确的BR.java文件?
Hyb*_*taX 33
如果有人仍然有这个问题.我通过在Android Studio中使缓存无效来解决这个问题.我正在使用带有API 23的Android Studio 2
yig*_*git 24
这是android-apt插件引起的问题.它忽略了数据绑定插件添加的提供的依赖项.
您可以通过添加:apt 'com.android.databinding:compiler:1.0-rc0到您的gradle文件来解决它
.
在我的案例中,我找到了问题的原因.我发现在我的Data Object类的import语句中包含:
import static android.databinding.tool.util.GenerationalClassUtil.ExtensionFilter.BR;
Run Code Online (Sandbox Code Playgroud)
删除后,问题就解决了.Android Studio以某种方式错误地为我解决了BR类.
如果您使用的是Kotlin,请尝试将以下插件应用于您的应用build.gradle文件:
apply plugin: 'kotlin-kapt'
清理您的项目,然后重新构建。这为我解决了这个问题。
小智 6
Bindable注释在编译期间在BR类文件中生成一个条目.BR类文件将在模块包中生成.
@Bindable
public String getFirstName() {
return this.firstName;
}
Run Code Online (Sandbox Code Playgroud)
我也遇到了这个问题。问题是我更改了类名并且没有更新 XML 文件中的值。这包括变量名称以及它们在 xml 文件中的访问位置。
小智 5
就我而言,问题是由
“错误:变量 MyVariable 已在类 MyModel 中定义”
令人讨厌的是 Android Studio gradle build 没有在底部显示这个错误。我必须评论每个数据绑定错误 > 重建项目,直到出现错误。吸取的教训是从下到上扫描每个错误,并注意您最近可能更改的任何内容。
您需要将其添加到您的gradle.properties文件中
android.databinding.enableV2=true
Run Code Online (Sandbox Code Playgroud)
您可能会收到多个错误报告未找到绑定类。新的数据绑定编译器通过在托管编译器构建应用程序之前生成绑定类来防止这些错误。
| 归档时间: |
|
| 查看次数: |
36040 次 |
| 最近记录: |