【问题标题】:Repeated Build fails "To use Coroutine features, you must add `ktx`......."重复构建失败“要使用协程功能,必须添加`ktx`.......”
【发布时间】:2020-02-28 12:05:57
【问题描述】:

我是 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'
}

【问题讨论】:

    标签: android kotlin android-room coroutine


    【解决方案1】:

    这些是您应该添加的。

    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" 
    

    【讨论】:

      【解决方案2】:

      你正在使用

      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

      【讨论】:

      • 谢谢,我会尽快尝试。但是我对构建错误消息“要使用协程功能,您必须添加来自 Room 的 ktx artifact 作为依赖项。androidx.room:room-ktx:”感到有点困惑。
      • 这只是意味着你实际上并没有声明 implementationroom-ktx 的依赖,而是使用 suspendFlow DAO 方法。 kapt 仅适用于注释处理器
      • 我刚刚意识到我在错误消息中读到了“kapt”而不是“ktx”。感谢您指出。
      • 一般情况下,答案左侧有一个复选框
      • 那个答案消除了我的错误,但是现在当我运行我的模拟器时,它关闭时没有任何错误。我不知道该怎么办?我来自同一个代码实验室
      猜你喜欢
      • 1970-01-01
      • 2011-07-08
      • 2019-09-08
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多