Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Lint : Kotlin Dispatchers unresolved reference but compilation working

I have already tried invalidating caches. Clean build and rebuild project also done. But I still keep getting unresolved reference for : Dispatchers, launch, withContext, delay, ... But CoroutineScope isn't marked as unresolved reference, the import kotlinx.coroutines.* isn't marked as unknown and the project is compiling (it has been working for months, it is not a recent project).

Android studio : 4.0.1

Settings > Languages & Frameworks > Kotlin : Current kotlin plugin version : 1.4.10-release-Studio4.0-1

build.gradle

buildscript {
    ext {
        // KOTLIN
        kotlin_coroutines_version = '1.3.5'
        kotlin_version = '1.3.71'
       ...
    }

 dependencies {
        classpath 'com.android.tools.build:gradle:4.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

module_build.gradle

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 29

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 29
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), '../proguard-rules.pro'
        }
    }
compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

    testOptions {
        unitTests.returnDefaultValues = true
        unitTests {
            all {
                testLogging {
                    events 'passed', 'skipped', 'failed'
                }
            }
        }
    }
}

dependencies {
    api fileTree(dir: 'libs', include: ['*.jar'])
    api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"
}

Edit : An example of my code, but remember this error appears in all files in my project (almost 100 files). And my code is working because I can work on it since one week.

init {
        CoroutineScope(Dispatchers.Main).launch {
            ...
            withContext(Dispatchers.IO) {
                delay(1000 * 5)
            }
        }
    }
like image 353
Xsims Avatar asked Nov 06 '22 04:11

Xsims


1 Answers

I found two ways to fix it (although still not sure why this bug happened) :

  • On my laptop, I simply updated Android Studio to version 4.1.0, and after a reboot, ALL the lint errors was gone.

  • On my coworker's laptop, we upgrade kotlin_coroutines_version from 1.3.5 to 1.4.0+ :

    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"
    
like image 135
Xsims Avatar answered Nov 11 '22 12:11

Xsims