Kotlin Multiplatform (KMP) Flashcards

ADVANCED › Emerging

In a typical KMP app, what code is shared versus kept native?
Shared in commonMain: business logic, networking, data models, persistence, and presentation/ViewModels. UI is often kept native (Compose on Android, SwiftUI on iOS), or shared via Compose Multiplatform if you want one UI codebase.
What do the expect and actual keywords do?
expect declares an API in commonMain with no body; each platform source set supplies a matching actual implementation. It works for functions, classes, objects, properties, and typealiases. A common modern alternative is a plain interface in common code with platform implementations injected.
Which Kotlin compiler backends does KMP use for Android, iOS, and web?
Android uses Kotlin/JVM (full Android SDK in androidMain), iOS uses Kotlin/Native which compiles via LLVM to a native binary, and web uses Kotlin/Wasm or Kotlin/JS.
What is Ktor's role in KMP and how does it stay multiplatform?
Ktor Client is a multiplatform HTTP client. It abstracts a pluggable engine chosen per platform (e.g. OkHttp on Android, Darwin on iOS) and integrates kotlinx.serialization via the ContentNegotiation plugin for JSON.
What does SQLDelight provide and how does its persistence differ per platform?
SQLDelight generates type-safe Kotlin APIs from .sq SQL files at compile time. You supply a platform SqlDriver: AndroidSqliteDriver on Android and NativeSqliteDriver on iOS. Room is now a KMP-ready alternative.
How does Swift consume shared Kotlin code, and what is the main friction?
Kotlin/Native compiles the shared module into an Objective-C framework that Swift imports. Friction: only Obj-C interop, so suspend functions surface as completion handlers, and generics and sealed classes map imperfectly. Tools like SKIE improve the generated Swift API.
What is Compose Multiplatform and how does it relate to KMP?
Compose Multiplatform is JetBrains' UI framework built on Jetpack Compose that shares UI across Android, iOS, desktop, and web. iOS support is Stable. It is optional, many teams share only logic and keep fully native UI.
Name the default KMP source sets and what the default hierarchy template adds.
commonMain and commonTest plus platform sets like androidMain and iosMain. The default hierarchy template auto-creates intermediate source sets (e.g. iosMain over iosArm64, iosX64, iosSimulatorArm64) so you can share code across a platform family.
When does KMP pay off, and what is its main build and tooling cost?
It pays off with substantial logic shared across two or more platforms, incremental adoption without a rewrite, and near-native performance. Costs: a Mac with Xcode for iOS, Gradle plus iOS framework integration, slower Kotlin/Native compiles, and harder cross-language debugging.

Back to Kotlin Multiplatform (KMP)