launch is only available since Kotlin 1.3 and cannot be used in Kotlin 1.2

Viewed 18605

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?

4 Answers

Make sure you have updated Kotlin to 1.3. You can do this from Preference->Lanugage & Framework->Kotlin Updates

Then change the version of kotlin.jvm plugin to 1.3.0 in gradle. (https://plugins.gradle.org/plugin/org.jetbrains.kotlin.jvm)

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

And for including coroutines

repositories {
    mavenCentral()
}

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

It should be fine now.

You must change kotlin plugin version

Your current kotlin plugin version is 1.2.51

dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.51"
}

this is correct

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

I solved this by manually updating the Kotlin-IntelliJ Plugin.

First, download the newer version of the Kotlin plugin that is compatible with your version of IntelliJ https://plugins.jetbrains.com/plugin/6954-kotlin/versions/stable

Then in IntelliJ's Settings -> Plugins, click on the settings/gear icon on the top-right side. From there choose Install Plugin from Disk..., choose the zip file you got from the intellij website. Then it will ask you to restart the IDE, and that's it :)

If it has started to occur only recently then chances are two links are generating same file with same name, in this case removing one of your recent file from build.gradle will help you resolving the issue.

In my case this was causing the issue

implementation 'com.miguelcatalan:materialsearchview:1.4.0'
implementation 'com.github.Ferfalk:SimpleSearchView:0.2.0'

After removing first one out of them the issue was resolved.

Related