skip to content

How Kotlin Does Multiplatform

One shared source set compiles to several backends, with expect declarations in common code and actual implementations per target. The design question interviewers pose is where to draw the line between shared and platform code.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

questions

5

What is Kotlin Multiplatform (KMP), and what does it let you share across platforms?

level: juniorimportance: must knowfreq 70%

answer

  1. One front-end, many back-ends
  2. commonMain holds shared code
  3. Targets: JVM, Native/LLVM, JS, Wasm
  4. Share logic, keep UI native (or Compose MP)
  5. expect/actual bridges platform gaps

basics

~10 s

Kotlin Multiplatform lets you write code once and run it on many platforms. You put shared code in one place, and the compiler builds it into apps for Android, iOS, web, and the desktop.

solid answer

~30 s

KMP is a Kotlin feature that compiles one shared codebase into multiple platform artifacts. Shared logic lives in the commonMain source set; the same Kotlin compiler front-end produces different back-end outputs per target: JVM bytecode (Android/server), native binaries via Kotlin/Native and LLVM (iOS, macOS, Linux), JavaScript, and WebAssembly (Wasm). You typically share business logic, networking, and data models while keeping UI native. Platform-specific gaps are filled with expect/actual declarations. Targets and source sets are configured in Gradle with the kotlin-multiplatform plugin. The key win is sharing logic without forcing a single shared UI.

go deeper

for a junior

Can state that KMP shares Kotlin code across platforms with commonMain and names a few targets.

for a middle

Explains front-end/back-end split, what is shared vs native, and that expect/actual fills gaps.

for a senior

Discusses tradeoffs (share logic vs UI), library constraints in commonMain, and how artifacts stay idiomatic per platform.

for a principal

Frames adoption strategy, when KMP beats alternatives, and the org/tooling implications of shared logic with native UI.

## What Kotlin Multiplatform is **Kotlin Multiplatform (KMP)** is a language and tooling capability that lets you write code once in Kotlin and compile it for several **targets** (platforms) at once. It is built into the Kotlin compiler — you enable it with the `kotlin("multiplatform")` Gradle plugin. ## The core idea: one front-end, many back-ends The Kotlin compiler has two halves: - a **front-end** that parses and type-checks your Kotlin source (the same for every platform), and - a **back-end** that emits platform-specific output. KMP reuses the one front-end and swaps back-ends to produce: - **JVM bytecode** — for Android and server/desktop apps. - **Native binaries** — via **Kotlin/Native**, which uses **LLVM** to produce machine code for iOS, macOS, watchOS, Linux, Windows. - **JavaScript** — for browsers/Node. - **WebAssembly (Wasm)** — a newer target for the web. ## What you actually share Shared code lives in the **`commonMain`** source set. It can only use the **Kotlin common standard library** plus multiplatform-aware libraries (e.g. `kotlinx.coroutines`, `kotlinx.serialization`, Ktor client). Typical shared pieces: - business/domain logic - networking and serialization - data models and validation - view-model / presentation logic UI is often kept **native per platform** (Jetpack Compose on Android, SwiftUI on iOS), though **Compose Multiplatform** lets you share UI too. ## Bridging platform gaps When common code needs something only a platform provides (e.g. the current time source, a UUID, secure storage), you use **`expect`/`actual`**: `commonMain` declares an `expect` API, and each platform source set supplies the `actual` implementation. ```kotlin // commonMain expect fun platformName(): String // androidMain actual fun platformName(): String = "Android ${android.os.Build.VERSION.SDK_INT}" // iosMain actual fun platformName(): String = "iOS" ``` ## Why it matters You avoid re-implementing the same logic in Kotlin, Swift, and JavaScript. Bugs are fixed once. Unlike some cross-platform frameworks, KMP does **not** force a shared UI or a separate runtime — it produces idiomatic native artifacts, so a KMP iOS framework is a normal `.framework` callable from Swift.

  • Does KMP force you to share the UI?
    No. You can share only logic and keep UI native (Compose/SwiftUI), or opt into Compose Multiplatform to also share UI.
  • What library can common code use?
    Only the Kotlin common stdlib and multiplatform-aware libraries; JVM/Android/platform-only APIs are not visible in commonMain.

Like writing one recipe and having different kitchens (ovens) each bake it into a dish suited to their appliances.

saying these in an interview costs you the question

  • Saying KMP ships a separate runtime/VM like some hybrid frameworks
  • Claiming you must share the UI
  • Thinking common code can call java.* or Android APIs directly
  • Confusing KMP with a transpiler that converts to Swift

context

open as a page

How do expect/actual declarations work, and what are their rules and limits?

level: middleimportance: must knowfreq 65%

basics

~10 s

expect/actual is how common code says 'something with this shape exists' and each platform fills in the real version. Common code declares expect; every target must provide a matching actual.

open as a page

Explain the KMP source-set hierarchy and how intermediate source sets like appleMain enable code sharing.

level: middleimportance: should knowfreq 50%

basics

~10 s

Source sets form a tree. commonMain is the root every target uses. Intermediate sets like appleMain group related targets (iOS, macOS) so you can share code among them without duplicating it per target.

open as a page

How do you decide what belongs in commonMain versus platform code, and what architectural patterns keep the boundary clean?

level: seniorimportance: should knowfreq 40%

basics

~10 s

Put logic that doesn't depend on a specific platform (rules, data, networking) in commonMain. Put things that touch a device API in platform code. Hide platform details behind interfaces so common code stays clean.

open as a page

What compilation targets does KMP support, and what artifact does each produce?

level: seniorimportance: should knowfreq 45%

basics

~10 s

A target is a platform you build for. JVM produces bytecode, Native produces machine-code binaries (like an iOS framework), JS produces JavaScript, and Wasm produces WebAssembly. You declare targets in Gradle.

open as a page