kotlinx.coroutines README

The project's README from github.com/Kotlin/kotlinx.coroutines. As one markdown file: README.md.

kotlinx.coroutines

Kotlin Stable JetBrains official project GitHub license Download Kotlin KDoc link Slack channel

Library support for Kotlin coroutines with multiplatform support. This is a companion version for the Kotlin 2.2.20 release.

suspend fun main() = coroutineScope {
    launch { 
       delay(1.seconds)
       println("Kotlin Coroutines World!") 
    }
    println("Hello")
}

Play with coroutines online here

Modules

Documentation

Using in your projects

Maven

Add dependencies (you can also add other modules that you need):

<dependency>
    <groupId>org.jetbrains.kotlinx</groupId>
    <artifactId>kotlinx-coroutines-core</artifactId>
    <version>1.11.0</version>
</dependency>

And make sure that you use the latest Kotlin version:

<properties>
    <kotlin.version>2.2.20</kotlin.version>
</properties>

Gradle

Add dependencies (you can also add other modules that you need):

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.11.0")
}

And make sure that you use the latest Kotlin version:

plugins {
    // For build.gradle.kts (Kotlin DSL)
    kotlin("jvm") version "2.2.20"
    
    // For build.gradle (Groovy DSL)
    id "org.jetbrains.kotlin.jvm" version "2.2.20"
}

Make sure that you have mavenCentral() in the list of repositories:

repositories {
    mavenCentral()
}

Android

Add kotlinx-coroutines-android module as a dependency when using kotlinx.coroutines on Android:

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.11.0")

This gives you access to the Android Dispatchers.Main coroutine dispatcher and also makes sure that in case of a crashed coroutine with an unhandled exception that this exception is logged before crashing the Android application, similarly to the way uncaught exceptions in threads are handled by the Android runtime.

R8 and ProGuard

R8 and ProGuard rules are bundled into the kotlinx-coroutines-android module. For more details see "Optimization" section for Android.

Avoiding including the debug infrastructure in the resulting APK

The kotlinx-coroutines-core artifact contains a resource file that is not required for the coroutines to operate normally and is only used by the debugger. To exclude it at no loss of functionality, add the following snippet to the android block in your Gradle file for the application subproject:

packagingOptions {
    resources.excludes += "DebugProbesKt.bin"
}

Multiplatform

Core modules of kotlinx.coroutines are also available for Kotlin/JS and Kotlin/Native.

In common code that should get compiled for different platforms, you can add a dependency to kotlinx-coroutines-core right to the commonMain source set:

commonMain {
    dependencies {
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.11.0")
    }
}

Platform-specific dependencies are recommended to be used only for non-multiplatform projects that are compiled only for target platform.

JS

Kotlin/JS version of kotlinx.coroutines is published as kotlinx-coroutines-core-js (follow the link to get the dependency declaration snippet).

Native

Kotlin/Native version of kotlinx.coroutines is published as kotlinx-coroutines-core-$platform where $platform is the target Kotlin/Native platform. Targets are provided in accordance with official K/N target support.

Building and Contributing

See Contributing Guidelines.

kotlinx.coroutines (Kotlin/kotlinx.coroutines)

Searching Database…

What is this?kotlinx.coroutines is Kotlin's official library for writing asynchronous, non-blocking code using coroutines, with multiplatform support for JVM, JS, Native and Wasm.
Is it a framework or a library?It's a library you add as a dependency, not a framework: it provides builders like launch and async, Flow, channels, and dispatchers that you call from your own code.
How do I add it to a project?Add the kotlinx-coroutines-core dependency via Gradle or Maven, making sure you're on the latest Kotlin version and have mavenCentral() in your repositories; for multiplatform projects, add it to the commonMain source set.
Which modules do I need for a specific platform?On Android add kotlinx-coroutines-android for the Main dispatcher; on JS/Native it's published as kotlinx-coroutines-core-js or per-target Native artifacts; UI toolkits like JavaFX/Swing have their own ui modules.
Any gotchas when using it on Android with R8/ProGuard?R8/ProGuard rules are bundled into kotlinx-coroutines-android, but the core artifact also ships a debug-only resource file you may want to strip from your APK via a Gradle snippet to avoid bloat.
Does it integrate with reactive libraries like RxJava?Yes, the reactive module provides builders and iteration support for Reactive Streams, RxJava 2.x/3.x, Project Reactor, and JDK 9 Flow.
Can I test coroutine code with this library?Yes, the test module offers runTest and TestScope for testing suspending functions, plus Dispatchers.setMain to override the Main dispatcher in tests.
Is there a debugging tool for coroutines?Yes, the debug module provides a DebugProbes API to track, print and dump active coroutines, plus a CoroutinesTimeout test rule and BlockHound integration.
What is Flow in this library?Flow is kotlinx.coroutines' cold asynchronous stream type, built with the flow builder and a comprehensive set of operators like filter and map.
Creator
Kotlin
Size
43 MB
Stars
13.8K
Age
10y
Commits · 90 Days