I have multi module kotlin gradle project in github here.
One of my sub project introducing-coroutines
with build file build.gradle.kts
file is here
The contents of build.gradle.kts
is -
import org.jetbrains.kotlin.gradle.dsl.Coroutines
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
java
kotlin("jvm") version "1.3.11"
}
group = "chapter2"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
compile(kotlin("stdlib-jdk8"))
compile(kotlin ("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0"))
testCompile("junit", "junit", "4.12")
}
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
kotlin {
experimental {
coroutines = Coroutines.ENABLE
}
}
I'm trying to create my first coroutine program from this link.
import kotlinx.coroutines.*
fun main() {
GlobalScope.launch { // launch new coroutine in background and continue
delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
println("World!") // print after delay
}
println("Hello,") // main thread continues while coroutine is delayed
Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive
}
The issue is GlobalScope
is not available in kotlin.coroutines.*
or kotlinx.coroutines.*
. Below is the screenshot -
gradle version - 5.1.1 kotlin version - 1.3.11 kotlinx-coroutines-core - 1.1.0
Can anyone help me the package import details what is package GlobalScope
/ runBlocking
required?
Definition of runBlocking() functionRuns a new coroutine and blocks the current thread interruptible until its completion. This function should not be used from a coroutine. It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in main functions and in tests.
Let's look at GlobalScope source code /** * A global [CoroutineScope] not bound to any job. * * Global scope is used to launch top-level coroutines which are operating on the whole application lifetime * and are not cancelled prematurely.
runBlocking() - use when you need to bridge regular and suspending code in synchronous way, by blocking the thread. Just don't overuse it. Don't treat runBlocking() as a quick fix for calling suspend functions. Always think if instead you shouldn't propagate suspending to the caller.
Coroutines were added to Kotlin in version 1.3 and are based on established concepts from other languages. On Android, coroutines help to manage long-running tasks that might otherwise block the main thread and cause your app to become unresponsive.
The simplest way to solve your issue is to replace
compile(kotlin ("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0"))
with
compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0")
So why do you need to remove kotlin
function? If you check its source code (below) you will see that it appends module name to string "org.jetbrains.kotlin:kotlin-"
so in your case the final string becomes "org.jetbrains.kotlin:kotlin-org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0"
which is obviously invalid and should cause an error (but it doesn't, so it is a bug).
/**
* Builds the dependency notation for the named Kotlin [module] at the given [version].
*
* @param module simple name of the Kotlin module, for example "reflect".
* @param version optional desired version, unspecified if null.
*/
fun DependencyHandler.kotlin(module: String, version: String? = null): Any =
"org.jetbrains.kotlin:kotlin-$module${version?.let { ":$version" } ?: ""}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With