20

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'
}
CoolMind
  • 26,736
  • 15
  • 188
  • 224
DaveinUk
  • 311
  • 1
  • 2
  • 7

2 Answers2

80

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.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Thanks I'll try that out ASAP. But I'm a little confused by the Build Error Message "To use Coroutine features, you must add `ktx` artifact from Room as a dependency. androidx.room:room-ktx:" – DaveinUk Nov 02 '19 at 18:25
  • 1
    That just means you haven't actually declared a `implementation` dependency on `room-ktx`, but are using `suspend` or `Flow` DAO methods. `kapt` is only for annotation processors – ianhanniballake Nov 02 '19 at 18:29
  • I've just realised that I read "kapt" instead of "ktx" in the error message. Thanks for pointing it out. – DaveinUk Nov 02 '19 at 18:34
  • Generally, there's a check box to the left of the answer – ianhanniballake Nov 02 '19 at 18:42
  • That answer removed my error, but now when I run my emulator, it closes with no error what so ever. I don't know what to do? I'm coming from the same codelab – StefanJo May 20 '20 at 19:39
13

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" 
Dissek Razziel
  • 151
  • 1
  • 5
  • 1
    Its some how missing in official docs. https://developer.android.com/jetpack/androidx/releases/room – Husnain Aug 20 '22 at 09:01