Kotlin-dsl-samples: Enable coroutine support in gradle script

Created on 5 Mar 2017  路  6Comments  路  Source: gradle/kotlin-dsl-samples

Right now there are no way to use coroutines (kotlinx.coroutines.experimental.*) functionalities from gradle script (say call a suspendable function from gradle task). Tried to add all the coroutines dependencies to buildscript classpath, but it still not working.

buildscript {
      var javaVersion : JavaVersion by extra
      var kotlinVersion : String by extra
      javaVersion = VERSION_1_8
      kotlinVersion = "1.1.0"

    repositories {
        mavenCentral()
        maven { setUrl("http://dl.bintray.com/kotlin/kotlin-eap-1.1") }
    }
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-stdlib:1.1.0")
        classpath("org.jetbrains.kotlinx:kotlinx-coroutines-core:0.12")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.0")
    }
}
feature kotlin-dsl-compilation ease-of-use fast-feedback

Most helpful comment

@sureshg I don't know what's happening with content assist there but the following works for me:

import kotlinx.coroutines.experimental.*

buildscript {
    dependencies {
        classpath("org.jetbrains.kotlinx:kotlinx-coroutines-core:0.12")
    }

    repositories {
        gradleScriptKotlin()
    }
}

// Run it with ./gradlew async -q
task("async") {
    doLast {
        runBlocking { // start main coroutine
            launch(CommonPool) { // create new coroutine in common thread pool
                delay(1000L)
                println("World!")
            }
            println("Hello,") // main coroutine continues while child is delayed
            delay(2000L) // non-blocking delay for 2 seconds to keep JVM alive
        }
    }
}

kotlinx-coroutines

All 6 comments

@metjka yeah I can use coroutine in my kotlin app, but this issue is to enable it in the gradle script.
https://kotlinlang.slack.com/archives/gradle/p1488709676002256

I mistakenly assumed coroutines were not enabled by default but this sample works for me with the latest snapshot distro:

// build.gradle.kts
import kotlin.coroutines.experimental.*

fun fib() = buildSequence {

    var a = 0
    var b = 1

    while (true) {
        yield(b)

        val next = a + b
        a = b; b = next
    }
}

task("fib") {
    doLast {
        fib().take(5).forEach(::println)
    }
}

Is there anything else that needs to be done?

@bamboo yeah I can use all the coroutines functionalities from kotlin.coroutines.experimental.* package, but most of the goodies are in kotlinx. package. I tried to use this package as given in this sample (https://github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md#your-first-coroutine) with kotlinx-coroutines-core in the buildscript class path (classpath("org.jetbrains.kotlinx:kotlinx-coroutines-core:0.12") ).

As you can see none of the extensions/classes are exposed in build script for kotlinx package.

screen shot 2017-03-06 at 1 53 12 pm

So the question is, can I use kotlinx coroutines libraries from gradle build script?

@sureshg I don't know what's happening with content assist there but the following works for me:

import kotlinx.coroutines.experimental.*

buildscript {
    dependencies {
        classpath("org.jetbrains.kotlinx:kotlinx-coroutines-core:0.12")
    }

    repositories {
        gradleScriptKotlin()
    }
}

// Run it with ./gradlew async -q
task("async") {
    doLast {
        runBlocking { // start main coroutine
            launch(CommonPool) { // create new coroutine in common thread pool
                delay(1000L)
                println("World!")
            }
            println("Hello,") // main coroutine continues while child is delayed
            delay(2000L) // non-blocking delay for 2 seconds to keep JVM alive
        }
    }
}

kotlinx-coroutines

@bamboo thanks! Really not sure what happened earlier. Your example works fine. You can close this issue now.

Was this page helpful?
0 / 5 - 0 ratings