使用Kotlin在Android中进行数据绑定出现问题

23
我正在尝试使用Kotlin为我的Android项目启用数据绑定。我已经启用了Kotlin插件,但是我无法启用数据绑定,我一直收到以下错误信息。

我正在尝试使用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
}
3个回答

40

编辑:使用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"
       ...
    }

确保 databinding 编译器版本和插件版本相同很重要。
另外请注意,使用 kapt3 时,不再需要使用 generateStubs 标志。

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"
   ...
}

(我在这里使用的是较新版本的数据绑定编译器,你也应该这样做)


是的,我有kotlin-gradle-plugin。但我仍然遇到错误。我正在尝试将Java项目转换为Kotlin。 - rahul.ramanujam
如果你的设置和我的相似,那么可以尝试清除Gradle缓存。https://dev59.com/amYr5IYBdhLWcg3wkbFM - Lovis
2
这解决了我的问题,但请记住,它只适用于kotlin_version =“1.1.2-3”,而在Android Studio 2.3.1上不适用于1.1.2-4。 - Kayvan N
@KayvanN 是的,但是根据您的设置更改版本应该是可行的。重要的是,Gradle插件和数据绑定编译器使用相同的版本。 - Lovis

2
尝试使用以下参考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
}

0

您无需包含任何额外的库来启用视图绑定

移除 kapt "androidx.lifecycle:lifecycle-compiler:2.2.0" 并添加

android {
..
    buildFeatures {
       viewBinding true
    }
}

to build.gradle


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接