Search code examples
gradlekotlinkotlin-coroutines

Unresolved reference: launch


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`

Solution

  • 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")
        }
    
    }