Kotlin
kotlinx
Learn kotlinx libraries.
kotlinx: Kotlin extension libraries
kotlinx is the namespace of Kotlin's official extension libraries. They provide capabilities beyond the standard library that are often needed. The best known are kotlinx-coroutines-core (async programming), kotlinx-serialization-json (JSON processing) and kotlinx-datetime (date and time).
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
To use these libraries you first add them as a dependency in build.gradle.kts. Then import brings the types into the file. The kotlinx packages are updated in parallel with Kotlin's release rhythm; keeping version numbers compatible matters.
Common kotlinx libraries
Some common kotlinx libraries: kotlinx-coroutines-core for coroutines and Flow, kotlinx-serialization-json for JSON conversion, kotlinx-datetime for date and time. Each is a separate dependency; adding only what you need keeps compile time short.
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
delay(100)
println("ready")
}
Here delay and runBlocking are imported from kotlinx.coroutines. Each dependency version must be compatible with the Kotlin version; mismatched versions cause compile errors. Version catalogs are a common tool to manage this compatibility.
Exercise Use kotlinx.coroutines
At the top of the file add import kotlinx.coroutines.*. Write main as fun main() = runBlocking { ... }. Inside it call println("Ready"). runBlocking comes from kotlinx.coroutines.
Show example solution
import kotlinx.coroutines.*
fun main() = runBlocking {
println("Ready")
}
Test yourself
-
Which of the following is a kotlinx library?
- kotlin.test
- java.util.List
- kotlinx-coroutines-core
- javax.swing
Show answer
kotlinx-coroutines-core — kotlinx-coroutines-core is the kotlinx library for async programming.
-
Import kotlinx
import ____.coroutines.*
Show answer
import kotlinx.coroutines.*
-
Match the kotlinx libraries
- kotlinx-coroutines-core
- Async programming
- kotlinx-serialization-json
- JSON conversion
- kotlinx-datetime
- Date and time
- kotlinx.coroutines.flow
- Flow streams
Next lesson: Summary - Packages