我为Android库项目启用dataBinding后立即生成错误
作者:互联网
我为我的库项目启用dataBinding后,立即收到构建错误:
AAPT:未指定资源类型(在“文本”中,值为“ @ {user.name}”)
如果我为应用程序模块启用了dataBinding,它将正常工作.
但是,如果我为我的lib项目启用dataBinding,则会收到上述错误.
应用程序模块的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
dataBinding{
enabled true
}
defaultConfig {
applicationId "xyz.databindingtrial"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile project(path: ':librarytrial')
}
lib项目的build.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
dataBinding{
enabled true
}
defaultConfig {
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
}
布局文件:
<data class="UserTrackingBinding">
<variable
name="user"
type="xyz.databindingtrial.model.User"/>
</data>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="xyz.databindingtrial.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.name}"/>
</RelativeLayout>
活动代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UserTrackingBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User user = new User("Test");
binding.setUser(user);
}
用户模型
public class User extends BaseObservable {
private final String name;
public User(String name){
this.name = name;
}
@Bindable
public String getName() {
return name;
}
}
dataBinding是否不适用于库项目?如果可以,我的设置有什么问题?
谢谢
解决方法:
它确实适用于库项目,但是依赖于使用数据绑定的库的任何应用程序都必须启用数据绑定,即使他们不使用它.
标签:android-library,data-binding,android 来源: https://codeday.me/bug/20191118/2029992.html