Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeated Build fails "To use Coroutine features, you must add `ktx`......."

I'm new to Kotlin and Android Studio and my current problem is this...
I'm trying get the Codelabs "android-room-with-a-view-kotlin" to work and whilst fixing various build errors along the way, I think my build.gradle has become very confused! I corrected the last build failure at Word.kt by adding the dependency

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

The next build went a little further on to WordDao.kt but failed with the same type of error..

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

I'm unable to continue because I don't know what to change in the build.gradle as I've already added that dependency?

As I've said my file is now very confused and I would appreciate any assistance in making it more sensible. Thanks, 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'
}
like image 370
DaveinUk Avatar asked Nov 02 '19 14:11

DaveinUk


2 Answers

You're using

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

But androidx.room:room-ktx is not an input into kapt, but a normal implementation dependency (the room-compiler is the annotation processor). Therefore your dependencies need to be

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

As per the Declaring Room dependencies documentation. Note that room-ktx pulls in room-runtime as a transitive dependency, so you can remove that dependency and rely solely on room-ktx if you want.

like image 71
ianhanniballake Avatar answered Jan 04 '23 15:01

ianhanniballake


These are the ones you should add.

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" 
like image 32
Dissek Razziel Avatar answered Jan 04 '23 14:01

Dissek Razziel