What is Kotlin Multiplatform (KMP), and what does it let you share across platforms?
answer
- One front-end, many back-ends
- commonMain holds shared code
- Targets: JVM, Native/LLVM, JS, Wasm
- Share logic, keep UI native (or Compose MP)
- expect/actual bridges platform gaps
basics
~10 sKotlin 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 sKMP 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
Can state that KMP shares Kotlin code across platforms with commonMain and names a few targets.
Explains front-end/back-end split, what is shared vs native, and that expect/actual fills gaps.
Discusses tradeoffs (share logic vs UI), library constraints in commonMain, and how artifacts stay idiomatic per platform.
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