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

Kse*_*nia 6 kotlin kotlinx.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!")
    }
Run Code Online (Sandbox Code Playgroud)

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

但是,当我运行此示例时,出现以下错误:

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

为什么会发生这些错误?我完全感到困惑,因为第一个错误说启动“仅在Kotlin 1.3之后才可用,不能在Kotlin 1.2中使用”,但是我在build.gradle文件中使用了Kotlin 1.3(尤其是'1.3.0-rc -146')...

UPD

看来问题的原因在IntelliJ IDEA设置中:

在此处输入图片说明

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

Sur*_* TR 5

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

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

plugins {
    id 'org.jetbrains.kotlin.jvm' version "1.3.0"
}
Run Code Online (Sandbox Code Playgroud)

并包括 coroutines

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.0.0'
}
Run Code Online (Sandbox Code Playgroud)

现在应该没事了。