使用Kotlin for Android进行数据绑定问题

我正在尝试使用Kotlin为我的Android项目启用数据绑定。 我有Kotlin插件启用,但我不能启用数据绑定我不断收到以下错误

Error:(66, 0) Could not find method kapt() for arguments [com.android.databinding:compiler:2.0.0-beta6] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler 

我有我的gradle文件中的数据绑定以下依赖关系

 dependencies { ... kapt 'com.android.databinding:compiler:2.0.0-beta6' } kapt { generateStubs = true } 

编辑:与Kotlin 1.1和Kapt3它略有不同:

在你的项目 build.gradle

 buildscript { ext { ... plugin_version = "2.3.1" kotlin_version = "1.1.2-3" ... } ... dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "com.android.tools.build:gradle:$plugin_version" ... } } 

并在您的应用程序 build.gradle

  apply plugin: "kotlin-android" apply plugin: "kotlin-kapt" ... android { ... dataBinding { enabled = true } ... } dependencies { ... kapt "com.android.databinding:compiler:$plugin_version" ... } 

数据绑定编译器版本和插件版本是相同的。
另外请注意,使用kapt3 ,不应再使用generateStubs标志。


老答案

启用Android Studio插件还不够,还需要稍微调整Gradle文件(添加并应用kotlin-gradle-plugin )以下是使用Java和Kotlin Databinding的我的gradle文件的摘录

在你的项目 build.gradle

 buildscript { ... dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.5" classpath 'com.android.tools.build:gradle:2.2.3' ... } } 

并在您的应用程序 build.gradle

 apply plugin: "kotlin-android" ... android { ... dataBinding { enabled = true } ... } kapt { generateStubs = true } dependencies { ... kapt "com.android.databinding:compiler:2.2.0" ... } 

(我在这里使用一个更新版本的数据绑定编译器,你可能也应该这样做)

尝试在下面的引用gradle文件源的帮助下在你的gradle文件中包含缺少的块。

应用程序级别Build.Gradle

 apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' android { compileSdkVersion 25 buildToolsVersion "25.0.3" defaultConfig { applicationId "com.example.adventure.abc" minSdkVersion 16 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } sourceSets { main.java.srcDirs += 'src/main/kotlin/com/dougritter/marvelmovies' } dataBinding { enabled = true } } kapt { generateStubs = true } dependencies { //Compatibility compile fileTree(dir: 'libs', include: ['*.jar']) compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) kapt 'com.android.databinding:compiler:2.3.0' //Libraries testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support:design:25.3.1' compile 'com.android.support:support-vector-drawable:25.3.1' compile 'com.android.support:support-v4:25.3.1' compile project(':domain') compile project(':androidutils') compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.jakewharton.timber:timber:4.5.1' } 

项目级别Build.Gradle

  // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = '1.1.2-2' repositories { maven { url 'https://maven.google.com' } jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-alpha1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() maven { url 'https://maven.google.com' } mavenCentral() } } task clean(type: Delete) { delete rootProject.buildDir }