Unresolved reference: launch

51.2k views Asked by At

Trying to run some examples for Kotlin coroutines, but can't build my project. I'm using the latest gradle release - 4.1

Any suggestions what to check/fix?

Here is build.gradle

buildscript {
    ext.kotlin_version = '1.1.4-3'

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'kotlin'
apply plugin: 'application'

kotlin {
    repositories {
        jcenter()
    }

    experimental {
        coroutines 'enable'
    }

    dependencies {
        compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.18"
    }
}

and the main.kt

fun main(args: Array<String>) {
    launch (CommonPool) {
        delay(1000L)
        println("World!")
    }

    println("Hello, ")
    Thread.sleep(2000L)
}

when I run gradle compileKotlin I get the following

e: /Users/philippgrigoryev/projects/kotlin-coroutines/src/main/kotlin/main.kt: (2, 5): Unresolved reference: launch
e: /Users/philippgrigoryev/projects/kotlin-coroutines/src/main/kotlin/main.kt: (2, 13): Unresolved reference: CommonPool
e: /Users/philippgrigoryev/projects/kotlin-coroutines/src/main/kotlin/main.kt: (3, 9): Unresolved reference: delay`
8

There are 8 answers

2
s1m0nw1 On BEST ANSWER

Like already answered in the comments, the import is missing for the kotlinx.coroutines.experimental.* package. You can have a look at my examples at GitHub if you like.

import kotlinx.coroutines.experimental.*

fun main(args: Array<String>) {

    launch(CommonPool) {
        delay(1000)
        LOG.debug("Hello from coroutine")
    }

}
0
Shoham Wong On

I met this issue while using kotlinx-corountine-core library, here is my gradle,

buildscript {
    ext.kotlin_version = '1.5.21'
    ext.kotlin_coroutines_version = '1.3.9'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:3.6.0"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.tencent.mm:AndResGuard-gradle-plugin:1.2.20'
    }
}

// .....

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar", '*.aar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.0'
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"
}

Then the launch and many other references from kotlin coroutines can't be found. Here is my procedure to solve this problem,

First, find the final dependences under Android Studio. I'm surprised to find that the final kotlin coroutine library was 1.5.0, AND IT SHOULD BE EMPTY!

enter image description here

1.3.9 is also included, but 1.5.0 is greater. So, 1.5.0 will be used finally.

Then I tried to find out the project dependencies by the following command,

>gradlew :app:dependencies

where 'aap' is the project module name.

Then I found it's lifecycle-viewmodel-ktx using it.

So, I tried to upgrade the library to a greater version, 1.6.0. Then, I found it's not empty now and every references work again.

So, to solve it, you need,

  1. find which version of kotlin coroutines core library you are using in your project
  2. investigate what's going on in that library, is that 1.5.0 or empty?
  3. find who introduced this version
  4. try upgrade to a newer version
1
Abdullah Ejaz On

try this

CoroutineScope(coroutineContext).launch { 
        
    }
0
Sam On

Try this way,

//add this lines to gradle

 implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.kotlinx_coroutines"
 implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.kotlinx_coroutines"


  class xyz{
     private val job = Job()
        private val coroutinesScope: CoroutineScope = CoroutineScope(job + Dispatchers.IO)

      ....
     . ...
       coroutinesScope.launch {
                       // task to do
                        Timber.d("Delete Firebase Instance ID")
                    }


    // clear the job
      job.cancel()

    }
0
Aman Srivastava On

I am using

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1"

Instead of launch, GlobalScope.launch works.

Given below is the imports:-

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
5
Tash Pemhiwa On

If you are using Coroutines 1.0+, the import is no longer

import kotlinx.coroutines.experimental.*

but

import kotlinx.coroutines.launch

You would need the following in the dependencies closure of your build.gradle (for Coroutines 1.0.1):

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1"
0
AdamJ On

This confused me for a while because the tutorial I am doing shows it used directly... I think they have changed how it is used from a previous version.

I think the trick is that you can only call launch() within a Coroutine scope.

So this does not work:

fun main() {
    launch()  // not called within coroutine scope
}

Where this will work:

fun main() {
 runBlocking {
        launch()  // called within coroutine scope
    }
}

code snippit example

3
Christian On

Launch isn't used anymore directly. The Kotlin documentation suggests using:

fun main() { 
    GlobalScope.launch { // launch a new coroutine in background and continue
        delay(1000L)
        println("World!")
    }
    println("Hello,") // main thread continues here immediately
    runBlocking {     // but this expression blocks the main thread
        delay(2000L)  // ... while we delay for 2 seconds to keep JVM alive
    } 
}