android - 重复构建失败 "To use Coroutine features, you must add ` ktx`......"

标签 android kotlin android-room coroutine

我是 Kotlin 和 Android Studio 的新手,我目前的问题是……
我正在尝试让代码实验室“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'
}

最佳答案

你正在使用

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,如果你想要的。

关于android - 重复构建失败 "To use Coroutine features, you must add ` ktx`......",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58671990/

相关文章:

android - 设备文件资源管理器中的数据库文件夹为空 - ROOM 数据库

java - 未通过 FirebaseMessaging 接收前台通知,但在后台工作

android - 获取光标位置 Android 键盘

android - 单元测试中的 Looper.getMainLooper() null

android - 使用 Jetpack Compose 中的 AnimatedVisibility 进行动画处理时 IconButton 透明度存在问题

kotlin - 为什么在 nullcheck 后 Smartcast 不起作用

android - 为什么不能在 Fragment 中使用 ViewPager?它实际上是

java - 返回 List<Object> 的房间查询会卡住应用程序

Android Room 缓慢迁移竞争条件

java - 如何修复 Android java 中的 "each bind variable in the query must have a matching method parameter"错误?