kotlin - launch 仅从 Kotlin 1.3 开始可用,不能在 Kotlin 1.2 中使用

标签 kotlin kotlinx.coroutines

我正在尝试运行 the simplest example with coroutines :

    import kotlinx.coroutines.*

    fun main() {
        GlobalScope.launch {
            delay(1000L)
            println("${Thread.currentThread().name}: World")
        }
        println("${Thread.currentThread().name}: Hello")
        Thread.sleep(2000L)
        println("${Thread.currentThread().name}: Finish!")
    }

我的 build.gradle 文件如下所示:

buildscript {
    // Consider moving these values to `gradle.properties`
    ext.kotlin_version = '1.3.0-rc-146'
    ext.kotlin_gradle_plugin_version = '1.3.0-rc-198'
    ext.kotlinx_coroutines = '1.0.0-RC1'

    repositories {
        maven { url "https://kotlin.bintray.com/kotlin-eap" }
        mavenCentral()
        jcenter()
        google()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.51"
    }
}

plugins {
    id 'org.jetbrains.kotlin.jvm' version "1.1.51"
}

apply plugin: 'idea'
apply plugin: 'application'
group 'by.kotlin'
version '1.0-SNAPSHOT'

mainClassName = 'MainKt'

repositories {
    maven { url "https://kotlin.bintray.com/kotlin-eap" }
    mavenCentral()
    jcenter()
    google()
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinx_coroutines"   
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

但是当我运行这个例子时,我得到了以下错误:

e: ...Main.kt: (6, 17): 'launch(CoroutineContext = ..., CoroutineStart = ..., [ERROR : Bad suspend function in metadata with constructor: Function2]<CoroutineScope, Continuation<Unit>, Any?>): Job' is only available since Kotlin 1.3 and cannot be used in Kotlin 1.2
e: ...Main.kt: (7, 9): Suspend function 'delay' should be called only from a coroutine or another suspend function
e: ...Main.kt: (7, 9): 'delay(Long): Unit' is only available since Kotlin 1.3 and cannot be used in Kotlin 1.2
> Task :compileKotlin FAILED

为什么会出现这些错误?我完全糊涂了,因为第一个错误说启动“仅在 Kotlin 1.3 之后可用,不能在 Kotlin 1.2 中使用”,但我在 build.gradle 文件中使用 Kotlin 1.3(特别是 '1.3.0-rc -146')...

UPD

问题的原因似乎在 IntelliJ IDEA 设置中:

enter image description here

但是如果可以在那里选择的最新语言版本是 1.2,而不是 1.3,如何解决它?

最佳答案

确保您已将 Kotlin 更新到 1.3。您可以从 Preference->Lanugage & Framework->Kotlin Updates

执行此操作

然后在gradle中将kotlin.jvm插件的版本改为1.3.0。 (https://plugins.gradle.org/plugin/org.jetbrains.kotlin.jvm)

plugins {
    id 'org.jetbrains.kotlin.jvm' version "1.3.0"
}

对于包含 coroutines

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.0.0'
}

现在应该没问题了。

关于kotlin - launch 仅从 Kotlin 1.3 开始可用,不能在 Kotlin 1.2 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53030756/

相关文章:

android - 如何在函数内测试 Kotlin 协程?

android - 如何使用嵌套的挂起函数对 kotlin 协程进行单元测试

android - Kotlin - 如何创建类似于 TextView.setText(String) 的类成员函数,可以调用为 TextView.text = ""

kotlin - 如何避免返回 SAM 接口(interface)的函数的对象表达式

Kotlin 1.3 : how to execute a block on a separate thread?

使用 RxJava 或 kotlin 协程的 Android ViewState

android - 在 fragment 中点击OptionsMenu项时不需要的导航

kotlin - 延迟功能在Kotlin中如何工作而不阻塞当前线程?

java - 不幸的是 MyApp 已经停止。我该如何解决这个问题?

kotlin - 将数据从回调推送到 Kotlin 协程的方法是什么