重复构建失败“要使用协同程序功能,必须添加`ktx` .......”

时间:2019-11-02 14:26:57

标签: android kotlin coroutine

我是Kotlin和Android Studio的新手,我当前的问题是...
我正在尝试使Codelabs的“ android-room-with-a-view-kotlin”正常工作,并且在解决各种构建错误的同时,我认为我的build.gradle变得非常混乱! 我通过添加依赖项

纠正了上次在 Word.kt 上的构建失败
kapt 'androidx.room:room-ktx:2.2.1'

下一个版本进一步扩展到 WordDao.kt ,但由于相同类型的错误而失败。

WordDao.java:21: error: To use Coroutine features, you must add `ktx` artifact from Room as a dependency. androidx.room:room-ktx:<version>

我无法继续,因为我不知道要在build.gradle中进行哪些更改,因为我已经添加了该依赖项?

正如我所说的,我的文件现在非常混乱,我希望提供 任何 帮助,使文件更加明智。谢谢,DaveInUk

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.prepopplus"
        //was minSdkVersion 15  Note Old phone is API16
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

        packagingOptions {
            exclude "META-INF/atomicfu.kotlin_module"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50"

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    kapt 'androidx.room:room-ktx:2.2.1'
    kapt "androidx.room:room-compiler:2.2.1"
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-rc01'

    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    kapt "android.arch.persistence.room:compiler:1.1.1"
    kapt "android.arch.lifecycle:compiler:1.1.1"

    implementation 'androidx.room:room-runtime:2.2.1'
}

2 个答案:

答案 0 :(得分:1)

您正在使用

kapt 'androidx.room:room-ktx:2.2.1'
kapt "androidx.room:room-compiler:2.2.1"

但是androidx.room:room-ktx不是kapt的输入,而是普通的implementation依赖项(room-compiler是注释处理器)。因此,您的依赖项必须是

implementation 'androidx.room:room-ktx:2.2.1'
kapt "androidx.room:room-compiler:2.2.1"

按照Declaring Room dependencies documentation。请注意,room-ktxroom-runtime作为传递依赖项引入,因此您可以删除该依赖项,并根据需要仅依赖于room-ktx

答案 1 :(得分:0)

这些是你应该添加的。

implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
// Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"