【问题标题】:Cannot make Android databinding plugin work无法使 Android 数据绑定插件工作
【发布时间】:2016-01-13 11:32:19
【问题描述】:

到目前为止,我正在尝试使用 android 数据绑定插件,但没有成功。

我正在使用:Gradle 2.2.1; Intellij IDEA 15。

项目级 build.gradle:

buildscript {
repositories {
    jcenter()
}
dependencies {

    classpath 'com.android.tools.build:gradle:1.5.0'
    classpath "com.android.databinding:dataBinder:1.0-rc2"

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
}

allprojects {
repositories {
    jcenter()
    }
}

模块构建.gradle:

apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'

buildscript {
repositories {
    jcenter()
}
dependencies {

}
}

repositories {
jcenter()
}

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
    applicationId "com.example.app4.app4"
    minSdkVersion 10
    targetSdkVersion 10
    versionCode 1
    versionName "1.0"
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_6
    targetCompatibility JavaVersion.VERSION_1_6
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),              'proguard-rules.pro'
    }
}

dataBinding {
    enabled = true
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
}

我在尝试更改插件版本时遇到了一些不同的错误;我现在得到的错误是“错误:Gradle:配置项目':app4'时出现问题。

未能通知项目评估侦听器。 android.databinding.tool.LayoutXmlProcessor.(Ljava/lang/String;Landroid/databinding/tool/writer/JavaFileWriter;IZLandroid/databinding/tool/LayoutXmlProcessor$OriginalFileLookup;)V"

【问题讨论】:

    标签: android android-gradle-plugin android-databinding


    【解决方案1】:

    使用新版本的数据绑定库,您无需显式添加数据绑定依赖项然后应用它。您只需要 dataBinding{enabled = true;} 其他问题:此时您的目标 SDK 版本可能不应该是 10。

    例子:

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.5.0'
            classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        }
    }
    
    allprojects {
        repositories {
            jcenter()
        }
    }
    

    apply plugin: 'com.android.application'
    apply plugin: 'com.neenbedankt.android-apt'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"
    
        defaultConfig {
            applicationId "com.yourappname"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0" 
    
    
        buildTypes {
            release {
                minifyEnabled false
                shrinkResources false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        dataBinding {
            enabled = true;
        }
    }
    
    dependencies {
        //other dependencies
    }
    

    我不记得 neenbedankt gradle 插件是否是绝对必要的,但它对于编译时注释处理非常有用。

    【讨论】:

    • +1 @Amafi82 正确,这不是必需的,但对于编译时间和注释处理非常有用。它也有助于调试并简化对绑定如何工作的理解。
    猜你喜欢
    • 2015-09-28
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    相关资源
    最近更新 更多