未解决的参考:发射

Unresolved reference: launch

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

对check/fix有什么建议吗?

这里是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

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`

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

import kotlinx.coroutines.experimental.*

fun main(args: Array<String>) {

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

}

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

import kotlinx.coroutines.experimental.*

但是

import 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"

不再直接使用启动。 Kotlin documentation 建议使用:

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

试试这个方法,

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

    }

我正在使用

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

而不是 launchGlobalScope.launch 有效。

以下是进口商品:-

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

这让我困惑了一段时间,因为我正在做的教程显示它直接使用......我认为他们已经改变了它的使用方式从以前的版本。

我认为诀窍在于您只能在协程范围内调用 launch()。

所以这不起作用:

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

适用的地方:

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

code snippit example

我在使用 kotlinx-corountine-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"
}

然后找不到 launch 和许多其他来自 kotlin 协程的引用。这是我解决这个问题的程序,

首先在AndroidStudio下找到最后的依赖。我很惊讶地发现最终的 kotlin 协程库是 1.5.0,它应该是空的!

1.3.9 也包括在内,但 1.5.0 更大。所以,1.5.0终于用上了。

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

>gradlew :app:dependencies

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

然后我发现它lifecycle-viewmodel-ktx正在使用它。

因此,我尝试将库升级到更高版本 1.6.0。然后,我发现它现在不是空的,每个引用都再次起作用。

所以,要解决它,你需要,

  1. 查找您在项目中使用的 kotlin 协程核心库版本
  2. 调查那个库里发生了什么,是 1.5.0 还是空的?
  3. 查找谁介绍了这个版本
  4. 尝试升级到更新的版本