我无法设置 Android 数据绑定

I'm not able to setup Android Data Binding

我的根 build.gradle 文件中有这个:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
    }
}

在应用 build.gradle 文件中:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.company.app"
        minSdkVersion 14
        targetSdkVersion 21
        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:22.2.0'
    //more third-party libraries
}

这是我的布局:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="user"
            type="com.company.app.models.LoginCredentials"/>
    </data>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:text="@{user.username}"/>

    <!-- More layouts and widgets -->
</layout>

但是我得到这个错误:

Error:(82, 35) No resource type specified (at 'text' with value '@{user.username}').

我试过:

dataBinding { 
    enabled = true 
}

但我得到另一个错误:

Error:Could not find com.android.databinding:library:1.0-rc3.

我该怎么办?我刚刚将 Android Studio 更新到 v1.5.1。

  1. 像这样将 classpath "com.android.databinding:dataBinder:1.0-rc1" 添加到您的 build.gradle

    buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.5.0' classpath "com.android.databinding:dataBinder:1.0-rc1" } }

  2. 更改您的 build.gradle 以放入@IntelliJAmiya 之前的评论中提到的内容并添加 apply plugin: 'com.android.databinding' 您的 gradle 将类似于

    apply plugin: 'com.android.application'
    apply plugin: 'com.android.databinding'  
     android {
        compileSdkVersion 23
        buildToolsVersion "23.0.1"
    
    
      defaultConfig {
            applicationId "com.company.app"
            minSdkVersion 14
            targetSdkVersion 21
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'),   'proguard-rules.pro'
            }
        }
    }
     dataBinding { 
            enabled = true 
        }
        dependencies {
            compile fileTree(dir: 'libs', include: ['*.jar'])
            testCompile 'junit:junit:4.12'
            compile 'com.android.support:appcompat-v7:23.1.0'
            //more third-party libraries
        }
    `
    

实际上 compileSdkVersion 21 制造问题。您需要使用 UPGRADED 版本 .

你应该使用

compileSdkVersion 23 
buildToolsVersion "23.0.1" 

&

targetSdkVersion 23  

您需要包含 dataBinding.enabled = true