I'm trying to run 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!")
    }

And my build.gradle file looks like this:

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

But when I run this example, I've got the following errors:

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

Why do these errors occur? I'm completely confused, because the first error says that launch "is only available since Kotlin 1.3 and cannot be used in Kotlin 1.2", but I use Kotlin 1.3 in my build.gradle file (in particular, '1.3.0-rc-146')...

UPD

It seems that the reason of problem is in IntelliJ IDEA Settings:

enter image description here

But how to fix it, if the latest language version, which can be selected there, is 1.2, not 1.3?

推荐答案

Make sure you have updated Kotlin to 1.3. You can do this from 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相关问答推荐

Gradle Jooq配置自定义生成器

在Kotlin中,我是否可以访问已知的WHEN子句值?

用vararg替换列表的设计弃用警告

如何定义一个函数来接受任何具有特定字段的数据类

从 Kotlin 的父类获取函数注解

如何在 kotlin 中的数据类中为变量提供多种类型

Kotlin - 如何避免在密封类的 when() 语句中转换第二个变量

Kotlin SIZE_BYTES

如何将glide显示的图像下载到设备存储中.Kotlin

即使 Kotlin 的 `Map` 不是 `Iterable`,它如何以及为什么是可迭代的?

匹配在单词边界上包含特殊字符的变量字符串的正则表达式

interface扩展

如何设置两列recyclerview?

如何在MVVM架构中观察RecyclerView适配器中的LiveData?

Jacoco在Gradle 7.0.2和Kotlin 1.5.10上失败

用Gradle Kotlin DSL构建源jar?

在 suspendCoroutine 块中调用挂起函数的适当方法是什么?

Java中lazy的Kotlin类似功能是什么?

在 Android 12 (SDK 31) 中获取 android.app.ForegroundServiceStartNotAllowedException

如何将 Kotlin 的 `with` 表达式用于可空类型