我正在尝试运行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设置:
但是,如果可以在此处选择的最新语言版本是1.2,而不是1.3,该如何解决?
答案 0 :(得分:4)
确保已将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'
}
现在应该可以了。
答案 1 :(得分:1)
您必须更改kotlin插件版本
您当前的kotlin插件版本为1.2.51
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.51"
}
这是正确的
buildscript {
ext.kotlin_version = '1.3.0'
ext.kotlin_gradle_plugin_version = '1.3.0'
ext.kotlinx_coroutines = '1.0.0'
repositories {
maven { url "https://kotlin.bintray.com/kotlin-eap" }
mavenCentral()
jcenter()
google()
}
dependencies {
'org.jetbrains.kotlin:kotlin-gradle-plugin:'+kotlin_version
}
}
答案 2 :(得分:0)
我通过手动更新Kotlin-IntelliJ插件解决了这个问题。
首先,下载与您的IntelliJ https://plugins.jetbrains.com/plugin/6954-kotlin/versions/stable版本兼容的Kotlin插件的较新版本。
然后在IntelliJ的“设置”->“插件”中,单击右上角的设置/齿轮图标。从那里选择从磁盘安装插件... ,选择从intellij网站获得的zip文件。然后它将要求您重新启动IDE,就是这样:)