Using Gradle 4.4.1 with the Kotlin-DSL, I want to configure the experimental Kotlin coroutines in my Kotlin 1.2.20 project. This proves to be surprisingly difficult.
The official Kotlin documentation itself only mentions Gradle with the Groovy DSL instead of Kotlin.
The Kotlin-DSL project has an example for coroutines that does not work. Probably, it’s for an older Gradle version (the Kotlin version in that script is 1.1.51, which is suspicious in itself).
PS: Even IntelliJ’s configure Kotlin writes incorrect Gradle code. Is downgrading the only option?
Addendum: The build script was very red in IntelliJ IDEA. The IDE kept adding import statements all the time. It seems that these futile attempts were what made the script non-functional. I disabled ‘Add unambiguos imports on the fly’ temporarily, and even though the script remained mostly red, Gradle could execute it. And afterwards, the error notices withing IntelliJ were gone as well.
Assuming you have the kotlin
plugin applied to the project:
import org.jetbrains.kotlin.gradle.dsl.Coroutines
plugins {
kotlin("jvm")
}
kotlin { // type is KotlinJvmProjectExtension
experimental.coroutines = Coroutines.ENABLE
}
This works for me with Gradle 4.4.1 and 4.5-rc-1, and I should also work for versions before that. If you are bringing in the Kotlin plugin by using buildscript
instead of plugins
, you may not have the generated accessors and will have to use extensions.getByType(KotlinJvmProjectExtension::class.java).apply {}
(or something like that).