Skip to content
This repository was archived by the owner on Aug 19, 2020. It is now read-only.

Enable coroutine support in gradle script #292

Closed
sureshg opened this issue Mar 5, 2017 · 6 comments
Closed

Enable coroutine support in gradle script #292

sureshg opened this issue Mar 5, 2017 · 6 comments

Comments

@sureshg
Copy link

sureshg commented Mar 5, 2017

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")
    }
}
@metjka
Copy link

metjka commented Mar 5, 2017

coroutines sample

@sureshg
Copy link
Author

sureshg commented Mar 6, 2017

@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

@bamboo
Copy link
Member

bamboo commented Mar 6, 2017

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?

@sureshg
Copy link
Author

sureshg commented Mar 6, 2017

@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?

@bamboo
Copy link
Member

bamboo commented Mar 7, 2017

@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

@sureshg
Copy link
Author

sureshg commented Mar 7, 2017

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

@sureshg sureshg closed this as completed Mar 7, 2017
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

No branches or pull requests

3 participants