skip to content

Which core kotlinx libraries are commonly added to commonMain in a Kotlin Multiplatform project, and what does each provide?

level: juniorimportance: must knowfreq 70%

answer

  1. coroutines = async, serialization = JSON, datetime = time, io = bytes
  2. group org.jetbrains.kotlinx
  3. declared once in commonMain
  4. serialization needs core + a format (json)
  5. datetime replaces java.time on Native/JS

basics

~10 s

The main ones are kotlinx-coroutines-core for async work, kotlinx-serialization for turning objects to/from JSON, kotlinx-datetime for dates and times, and kotlinx-io for reading and writing bytes. All work in shared code.

solid answer

~30 s

Four artifacts dominate commonMain: kotlinx-coroutines-core (structured concurrency: suspend functions, CoroutineScope, Flow, Dispatchers), kotlinx-serialization-core plus a format like kotlinx-serialization-json (compile-time @Serializable serializers, Json.encodeToString/decodeFromString), kotlinx-datetime (Instant, LocalDate, LocalDateTime, TimeZone, Clock.System for cross-platform time), and kotlinx-io (Buffer, Source, Sink for efficient binary/streaming IO). They are all official JetBrains libraries published with Kotlin Multiplatform metadata, so you depend on the single common artifact and Gradle resolves the right platform variant (JVM, Native, JS, Wasm) automatically. You add them to the commonMain source set's dependencies in your build script; no per-platform duplication needed.

code

kotlin · 12 lines
kotlin
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.datetime.Clock
import kotlinx.coroutines.flow.flowOf

@Serializable
data class Event(val name: String, val atEpochMs: Long)

val e = Event("launch", Clock.System.now().toEpochMilliseconds())
val text = Json.encodeToString(e)        // serialization
val back = Json.decodeFromString<Event>(text)
val stream = flowOf(e, back)             // coroutines Flow

go deeper

for a junior

Can name the four libraries and what each broadly does, and knows they go in commonMain.

for a middle

Knows serialization needs core plus a format, datetime replaces java.time, and dependencies are declared once.

for a senior

Explains Gradle Module Metadata variant resolution and why JDK types are unavailable off-JVM.

for a principal

Reasons about versioning/BOM alignment, API-stability tiers (e.g. datetime/io pre-1.0) and dependency footprint across targets.

## What this is about Kotlin Multiplatform (KMP) lets you write code once in a `commonMain` source set and compile it for several platforms (JVM, Android, iOS/Native, JS, Wasm). For common code to do anything useful it needs **multiplatform libraries** — libraries compiled for every target. JetBrains publishes a family of these under the `org.jetbrains.kotlinx` group. ## The four core kotlinx libraries - **kotlinx-coroutines-core** — asynchronous and concurrent programming. Gives you `suspend` functions, `CoroutineScope`, `launch`/`async`, `Dispatchers`, and the reactive `Flow`/`StateFlow`/`SharedFlow` types. This is how shared code does non-blocking IO and concurrency. - **kotlinx-serialization-core** + a format module — converting objects to/from a wire format. The core has the runtime; you add a format such as **kotlinx-serialization-json**. You annotate a class with `@Serializable` and the compiler plugin generates a serializer at compile time (no reflection), then call `Json.encodeToString(obj)` / `Json.decodeFromString<T>(text)`. - **kotlinx-datetime** — date and time types that exist on all platforms: `Instant` (a moment in time), `LocalDate`, `LocalDateTime`, `TimeZone`, and `Clock.System.now()`. The JVM `java.time` types are not available on Native/JS, so this fills the gap. - **kotlinx-io** — low-level, efficient byte IO: `Buffer`, `Source`, `Sink`. A multiplatform replacement for things like `java.io`/Okio basics. ## How you declare them You add them once to `commonMain`: ```kotlin kotlin { sourceSets { commonMain.dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3") implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.1") implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.5.4") } } } ``` Gradle resolves the **Gradle Module Metadata** for each artifact and downloads the matching variant per target. You write one dependency line, not one per platform. ## Why it matters The Kotlin standard library only covers language basics. These four libraries cover the most common real needs — concurrency, JSON, time, and IO — in pure common code, so the bulk of your logic stays platform-agnostic.

  • Why can't you just use java.time and java.io in commonMain?
    Those are JVM-only JDK classes; commonMain compiles to Native, JS and Wasm too, where the JDK does not exist. kotlinx-datetime and kotlinx-io provide equivalents available on every target.
  • Do you need a separate dependency per platform?
    No. You add the artifact once to commonMain; Gradle Module Metadata lets Gradle pick the correct per-target variant automatically.

Think of commonMain as a kitchen that ships everywhere; these four libraries are the universal appliances (mixer, oven, fridge, scale) that work in every country's outlets.

saying these in an interview costs you the question

  • Naming java.util or java.time as the multiplatform option
  • Thinking you must add the dependency separately to each platform source set
  • Saying serialization works with only kotlinx-serialization-core and no format module
  • Claiming these are third-party/community libs rather than official JetBrains ones
  • Confusing kotlinx (libraries) with kotlin (the stdlib/language)

context