为我的库项目启用 dataBinding 后,我立即收到构建错误:

AAPT:未指定资源类型(在值为 '@{user.name}' 的 'text' 处)

如果我为 应用程序 模块启用 数据绑定(bind) ,它可以正常工作
但是如果我为我的 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>

Activity 代码
@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 不适用于库项目吗?如果是这样,我的设置有什么问题?

谢谢

最佳答案

它确实适用于库项目,但任何依赖于使用数据绑定(bind)的库的应用程序都必须启用数据绑定(bind),即使他们不使用它。

关于android - 为 Android 库项目启用数据绑定(bind)后立即生成错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37146525/

10-11 22:14
查看更多