5

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)
            }
        }
    }
Xsims
  • 201
  • 1
  • 11
  • Can you show your code – tyczj Nov 04 '20 at 15:33
  • Where are you trying to use coroutines? are you trying it on library module or on app module? – Jeel Vankhede Nov 04 '20 at 16:05
  • @tyczj I edited my post but like I said, the error appears in the entire project and my project is compiling. So I don't think the problem is in the code. Otherwise me and coworkers would not be able to build and work. The linter error is only on my PC. – Xsims Nov 04 '20 at 16:16
  • @JeelVankhede Everywhere, in more than hundred files. Everything was working a week ago. And it's still working. It's just an Android Lint Problem. We are building an app and we are using multi-module. The unresolved reference error appears in every module. – Xsims Nov 04 '20 at 16:19
  • Oh, I just wanted to make sure that it's across modules. Follow up question, did any dependency or kotlin plugin got updated recently? It feels like some cache issue to me if. I would also check if underlying package structure/imports are not changed after version upgrade for coroutines. – Jeel Vankhede Nov 04 '20 at 16:24
  • @JeelVankhede We upgrade the dependency every 2-3 months, last time was two months ago and we didn't upgrade kotlin version and coroutine version due to several issues. Android Studio Plugin ? I didn't upgrade them. *I would also check if underlying package structure/imports are not changed after version upgrade for coroutines* can you clarify you thought please. – Xsims Nov 04 '20 at 16:50

1 Answers1

1

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"
    
Xsims
  • 201
  • 1
  • 11