未解决的引用:launch

64
尝试运行一些 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"
    }
}

main.kt

的内容为:

main.kt

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

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

当我运行 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`

2
你是否正在导入 kotlinx.coroutines.experimental.* 包? - nicholas.hauschild
哦!当然,你是对的,我忘记导入包了!谢谢@nicholas.hauschild - Philipp Grigoryev
8个回答

70

现在已不再直接使用Launch。 Kotlin文档建议使用:

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
    } 
}

9
GlobalScope 是从哪里来的? - hippietrail
1
@hippietrail 看起来已经被导入了。它在 kotlinx-coroutines 包中声明为 public object GlobalScope : CoroutineScope - Paul Stelian
2
现在,launch和async都是CoroutineScope类的函数,可以通过GlobalScope.launch访问,在本地作用域内运行coroutineScope { launch {} }之类的内容,或者创建自己的CoroutineScope。 - Bill Yang

36

如果您使用的是Coroutines 1.0+,则不再需要以下导入:

import kotlinx.coroutines.experimental.*

而改为使用以下导入:

import kotlinx.coroutines.launch

在build.gradle中的dependencies闭包中添加以下内容(针对Coroutines 1.0.1):

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

5
嘿,我导入了该函数,但仍出现“未解决的引用:launch”的错误。这怎么可能呢? - alexey.metelkin
14
现在他们使用不同的方法,不再有"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 - user25
7
谢谢,@user25,似乎是个问题。使用GlobalScope.launch(...解决了。 - alexey.metelkin
11
令人难以置信的是,在所有关于协程的文献和文章中,我都找不到有关如何导入协程的完整信息。谢谢。 - user1725145
这在2019年已经不再适用。请查看Christian的答案。 - That1Guy

6

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

import kotlinx.coroutines.experimental.*

fun main(args: Array<String>) {

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

}

8
我需要添加什么到Gradle才能导入kotlinx.coroutines.experimental.*? - Lance
3
这个答案有些过时了。使用新的 Kotlin 版本,你应该使用 GlobalScope.launch 替代 launch。 - Abdelhafid

4
这让我有点困惑,因为我正在做的教程显示它可以直接使用...我认为它们已经改变了它的用法,这是以前版本的不同。
我认为诀窍在于只能在协程范围内调用launch()。
所以这样做行不通:
fun main() {
    launch()  // not called within coroutine scope
}

适用于以下情况:

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

代码片段示例


2
我正在使用。
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1"

不要使用launch,使用GlobalScope.launch

以下是需要导入的内容:

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

2
尝试这种方法,

// 将以下行添加到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()

    }

1

我在使用kotlinx-coroutine-core库时遇到了这个问题,这是我的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"
}

然后在 Kotlin 协程中,启动和许多其他引用都找不到。以下是解决此问题的步骤:

首先,在 Android Studio 下查找最终依赖项。我惊奇地发现最终的 Kotlin 协程库是 1.5.0,但它应该是空的!

enter image description here

1.3.9也被包含在内,但1.5.0更大。因此,最终将使用1.5.0。

然后我尝试通过以下命令找出项目依赖关系,

>gradlew :app:dependencies

其中'aap'是项目模块名称。

然后我发现它使用了。

所以,我尝试将库升级到更高的版本1.6.0。然后,我发现它现在不再为空,每个引用都可以正常工作。

因此,要解决这个问题,您需要:

  1. 找出您的项目中使用的kotlin coroutines核心库的版本
  2. 调查该库中发生了什么,是1.5.0还是空的?
  3. 找出谁引入了这个版本
  4. 尝试升级到新版本

0

试试这个

CoroutineScope(coroutineContext).launch { 
        
    }

coroutineContext 来自哪里? - Acauã Pitta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接