skip to Main Content

Tried to use room api and had to deal with asynchronous tasks you know why.
I chose kotlin coroutines and androidx.lifecycle. Tried to write some code and got 2 errors displaying in IDE

Cannot access ‘kotlinx.coroutines.CoroutineScope’ which is a supertype of ‘androidx.lifecycle.LifecycleCoroutineScope’. Check your module classpath for missing or conflicting dependencies
Cannot access class ‘kotlinx.coroutines.Job’. Check your module classpath for missing or conflicting dependencies
error

Here is my gradle module

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'

}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.jacksafblaze.sqlitedemo"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildFeatures{
        viewBinding true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation "androidx.room:room-ktx:2.3.0"
    implementation "androidx.room:room-runtime:2.3.0"
    kapt "androidx.room:room-compiler:2.3.0"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.0"
    implementation'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'
    implementation'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0'
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:5'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

and gradle project

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

I tried to invalidate caches and restart, to recreate app from scratch, and to reinstall android studio and I am still getting this error.

2

Answers


  1. Chosen as BEST ANSWER

    upgraded 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0' to 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0-RC' and everything works fine


  2. I have the same problem, if I manually add imports in the class the project will compile but the IDE will still display an error.

    If you don’t want to wait coroutines-android 1.6 release manually adding the import is a workaround, but you will have no autocompletion.

    Android Studio build without autocompletion example

    You could also have some problems with room, moshi and all libraries that use kapt, you can try to update to latest release (beta/RC) to see if the problem will be fixed in new releases.

    For example, when I am writing this lines, moshi is not compatible with kotlin 1.6 yet, and a fix is planed to be released as soon as possible, see here for details.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search