Android Studio - Unresolved Reference errors for Kotlin coroutines but code compiles

Viewed 1516

today I started to experience some weird behavior in Android Studio 2020.3.1. I get Unresolved Reference errors for all methods from Kotlin coroutines. I cannot narrow down this issue to any specific change I made. The code still compiles and my App works fine. The only change that might be connected to this issue is that I created a new git branch a day before (everything worked fine though).

Text

My build.gradle file looks like this:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-kapt'

apply plugin: 'com.google.gms.google-services'

apply plugin: 'com.google.firebase.crashlytics'

android {
    compileSdkVersion 31
    defaultConfig {
        minSdkVersion 24
        targetSdkVersion 31
        versionCode 13
        versionName "1.5.4"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        kapt {
            arguments {
                arg("room.schemaLocation", "$projectDir/schemas".toString())
            }
        }
        kotlinOptions {
            jvmTarget = '1.8'
        }
        compileOptions {
            targetCompatibility = "8"
            sourceCompatibility = "8"
        }
    }

    buildFeatures {
        viewBinding true
    }


    packagingOptions {
        exclude 'META-INF/atomicfu.kotlin_module'
    }

    sourceSets { main { java.srcDirs = ['src/main/java', 'src/main/java/utilities'] } }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-reflect:1.5.31"
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.5.0-alpha05'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    implementation 'androidx.preference:preference-ktx:1.1.1'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    implementation "androidx.viewpager2:viewpager2:1.0.0"
    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    // Logcat
    implementation 'com.squareup.logcat:logcat:0.1'

    // Room components (SQL Wrapper)
    implementation "androidx.room:room-runtime:$rootProject.roomVersion"
    implementation "androidx.room:room-ktx:$rootProject.roomVersion"
    implementation "androidx.room:room-rxjava3:$rootProject.roomVersion"
    kapt "androidx.room:room-compiler:$rootProject.roomVersion"
    androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

    // Paging
    implementation "androidx.paging:paging-runtime-ktx:$rootProject.pagingVersion"
    implementation "androidx.paging:paging-rxjava2-ktx:$rootProject.pagingVersion"

    // Fragment
    implementation "androidx.fragment:fragment-ktx:$fragment_version"

    // Lifecycle components
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" // ViewModel
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" // LiveData
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version" // Lifecycles only (without ViewModel or LiveData)
    implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version" // Saved state module for ViewModel
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" // Annotation processor"
    implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version" // optional - helpers for implementing LifecycleOwner in a Service
    implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version" // optional - ProcessLifecycleOwner provides a lifecycle for the whole application process
    implementation "androidx.lifecycle:lifecycle-reactivestreams-ktx:$lifecycle_version" // optional - ReactiveStreams support for LiveData
    testImplementation "androidx.arch.core:core-testing:$arch_version" // optional - Test helpers for LiveData

    // RxJava
    implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
    implementation 'io.reactivex.rxjava3:rxjava:3.0.2'
    implementation 'io.reactivex.rxjava3:rxkotlin:3.0.0'

    // ViewModel Kotlin support
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.archLifecycleVersion"

    // Coroutines
    // api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines"
    // api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines"
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'
    implementation 'androidx.core:core-ktx:1.7.0'

    // Location
    implementation 'com.google.android.gms:play-services-location:18.0.0'

    // Apache common math
    implementation 'org.apache.commons:commons-math3:3.6.1'


    // CameraX core library using the camera2 implementation
    // The following line is optional, as the core library is included indirectly by camera-camera2
    implementation("androidx.camera:camera-core:$rootProject.camerax_version")
    implementation("androidx.camera:camera-camera2:$rootProject.camerax_version")
    // If you want to additionally use the CameraX Lifecycle library
    implementation("androidx.camera:camera-lifecycle:$rootProject.camerax_version")
    // If you want to additionally use the CameraX View class
    implementation("androidx.camera:camera-view:1.0.0-alpha30")
    // If you want to additionally use the CameraX Extensions library
    implementation("androidx.camera:camera-extensions:1.0.0-alpha30")

    // FireBase
    implementation platform('com.google.firebase:firebase-bom:28.4.0')
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation 'com.google.firebase:firebase-crashlytics-ktx'
}

buildscript {
    ext.kotlin_version = '1.5.31'
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.10'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.1'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url "https://jitpack.io" }
    }
}

ext {
    roomVersion = '2.3.0'
    pagingVersion = '3.0.1'
    archLifecycleVersion = '2.2.0'
    androidxArchVersion = '2.0.0'
    coroutines = '1.2.0'
    lifecycle_version = '2.4.0'
    arch_version = '2.1.0'
    fragment_version = '1.3.6'
    camerax_version = "1.0.2"
}

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

Things I have tried to resolve this:

  • Invalidated caches and restarted Android Studio
  • Sync gradle files again
  • Deleted .idea and .gradle folders
  • Switched to another git branch that worked fine before and where no changes were made
  • Restarted my PC

Now I am out of ideas of what to try. If anyone has an idea what the issue could be, I would be really grateful.

Edit: If I edit kotlinx implementation in the build.gradle file to:

    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2-native-mt'

the errors disappear, however, I do not understand why.

0 Answers
Related