【问题标题】:Issue with databinding using Kotlin for Android使用 Kotlin for Android 进行数据绑定的问题
【发布时间】:2017-05-03 23:36:49
【问题描述】:

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

【问题讨论】:

    标签: android data-binding kotlin


    【解决方案1】:

    编辑: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 数据绑定的 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"
       ...
    }
    

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

    【讨论】:

    • 是的,我确实有 kotlin-gradle-plugin。但我仍然收到错误消息。我正在尝试将 java 项目转换为 Kotlin。
    • @3xplore 如果您的设置看起来像我的,那么也许尝试清除 Gradle 缓存 stackoverflow.com/questions/13565082/…
    • 这解决了我的问题,但请记住,它仅适用于 kotlin_version = "1.1.2-3" 而不适用于 Android Studio 2.3.1 上的 1.1.2-4
    • @KayvanN 是的,但应该可以根据您的设置更改版本。重要的是,gradle 插件和数据绑定编译器使用相同的版本。
    【解决方案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
    }
    

    【讨论】:

      【解决方案3】:

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

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

      android {
      ..
          buildFeatures {
             viewBinding true
          }
      }
      

      构建.gradle

      【讨论】:

        猜你喜欢
        • 2016-08-14
        • 2013-12-27
        • 1970-01-01
        • 1970-01-01
        • 2017-02-17
        • 1970-01-01
        • 2018-12-01
        • 2021-12-30
        • 1970-01-01
        相关资源
        最近更新 更多