未解决的参考:启动

Phi*_*yev 20 gradle kotlin kotlin-coroutines

试图为Kotlin协程运行一些示例,但无法构建我的项目。我正在使用最新的gradle版本-4.1

有什么建议要检查/修复吗?

这是 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"
    }
}
Run Code Online (Sandbox Code Playgroud)

main.kt

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

    println("Hello, ")
    Thread.sleep(2000L)
}
Run Code Online (Sandbox Code Playgroud)

当我跑步时,gradle compileKotlin我得到以下内容

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`
Run Code Online (Sandbox Code Playgroud)

Tas*_*iwa 19

如果您使用的是Coroutines 1.0+,则不再导入

导入kotlinx.coroutines.experimental。*

导入kotlinx.coroutines.launch

在build.gradle的依赖项关闭中(对于协程1.0.1),您将需要以下内容:

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1"
Run Code Online (Sandbox Code Playgroud)

  • @ alexey.hippie mb现在他们使用不同的方法,不再有`launch`关键字,并且您总是需要一些`scope`,然后可以使用`scope.launch {...}`:https://github.com /googlecodelabs/kotlin-coroutines/blob/19f36f6cdc6b90322b7eea00885e86f9c02bcaea/kotlin-coroutines-end/app/src/main/java/com/example/android/kotlincoroutines/main/MainViewModel.kt#L136 (11认同)
  • 谢谢@ user25,这似乎是个问题。与`GlobalScope.launch(...` (5认同)
  • 嘿,即使导入了该函数,我仍然会收到“未解决的参考:启动”。这怎么可能呢? (4认同)
  • 令人难以置信的是,在所有有关协程的文档和文章中,我找不到有关如何在其他任何地方导入协程的完整信息。谢谢。 (3认同)

Chr*_*ian 14

启动不再直接使用。在科特林文档建议使用:

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
    } 
}
Run Code Online (Sandbox Code Playgroud)

  • “GlobalScope”从哪里来? (3认同)
  • launch、async 现在都是 CoroutineScope 类的函数,可以通过 GlobalScope.launch 在本地范围内通过运行 coroutineScope { launch {} } 之类的东西来访问它们,或者创建自己的 CoroutineScope (2认同)

s1m*_*nw1 6

就像评论中已经回答的那样,kotlinx.coroutines.experimental.*包缺少导入。如果您愿意,可以在GitHub 上查看我的示例。

import kotlinx.coroutines.experimental.*

fun main(args: Array<String>) {

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

}
Run Code Online (Sandbox Code Playgroud)

  • 我应该向 gradle 添加什么才能导入 kotlinx.coroutines.experimental.*? (8认同)
  • 这是一个旧答案。对于新的 kotlin 版本,您应该使用 GlobalScope.launch 而不是 launch (3认同)