--- title: "Kotlin Multiplatform (KMP)" date: 2026-12-05T00:00:00Z draft: false description: "Kotlin Multiplatform reference: project structure, source sets, expect/actual, Gradle setup with AGP 9, iOS interop, testing and key libraries." categories: ["programming"] tags: ["kotlin", "kmp", "multiplatform", "ios", "android", "gradle"] --- ## Overview Kotlin Multiplatform (KMP) shares Kotlin code between Android, iOS, desktop, web and server, while each platform keeps its own native entry point and can still call platform APIs. You share as much as makes sense (networking, data, business logic, optionally UI with [Compose Multiplatform](/programming/compose-multiplatform/)) and write the rest natively. Platform stability (core KMP): Android, iOS, Desktop (JVM), Server-side (JVM) and Web on Kotlin/JS are Stable. Web on Kotlin/Wasm, watchOS and tvOS are Beta. Source: [stability of supported platforms](https://kotlinlang.org/docs/multiplatform/supported-platforms.html); Kotlin/Wasm is Stable in Kotlin 2.5, so check that page for the current status. ## Setup ```text Start a project kmp.jetbrains.com (Kotlin Multiplatform Wizard; links can be prefilled, see below), or the KMP plugin in Android Studio / IntelliJ IDEA Check your Mac brew install kdoctor && kdoctor (finds missing Xcode, JDK, Android SDK pieces) iOS builds need a Mac with Xcode Libraries klibs.io: AI-powered search over 5,100+ KMP libraries (JetBrains, open source) ``` ```bash # Run the shared tests on every target your machine can build ./gradlew allTests ./gradlew jvmTest ./gradlew iosSimulatorArm64Test ``` ### Prefilled Wizard Links The wizard reads its choices from the URL, so you can bookmark or share a starting point: ```text https://kmp.jetbrains.com/?android=true&ios=true&iosui=compose&includeTests=true ``` | Parameter | Meaning | |---|---| | `android`, `ios`, `desktop`, `web`, `server` | `true` includes that target; a target left out is off | | `iosui` | `compose` shares the UI with Compose Multiplatform (the default) | | `webui` | `compose` shares the UI on web too; the alternative is React with TypeScript, chosen in the page | | `includeTests` | `true` or `false` | | `buildSystem` | `kotlin` selects the Kotlin Toolchain; leave it out for Gradle | With no parameters you get Android and iOS, shared Compose UI and tests. Checked against the live wizard on 2026-12-05. Picking "Do not share UI - Use SwiftUI" writes `iosui=swiftui` into the address bar, but loading that value back showed the Compose option selected, so make that choice in the page. ## Project Structure ```text my-app/ ├── shared/ Kotlin code shared by all platforms (KMP library module) │ └── src/ │ ├── commonMain/ code for every target (no platform APIs) │ ├── commonTest/ tests for common code │ ├── androidMain/ Android-only code │ ├── iosMain/ code shared by all iOS targets │ └── jvmMain/ desktop or server code ├── androidApp/ Android entry point (its own module) ├── iosApp/ Xcode project: SwiftUI shell that uses the shared framework └── desktopApp/ optional desktop entry point ``` | Source set | Compiled for | |---|---| | `commonMain` | every target you declare | | `iosMain`, `appleMain`, `nativeMain` | intermediate sets created by the default hierarchy template (iOS, Apple, all native targets) | | `androidMain`, `jvmMain`, `jsMain`, `wasmJsMain` | one target each | Common code can only use Kotlin and multiplatform libraries. The compiler rejects `java.io.File` or any other JDK class there, because the same code also compiles to native. ## Gradle Setup With Android Gradle Plugin 9.0 or newer, the KMP plugin no longer works together with `com.android.application` or `com.android.library`. Put the Android app in its own module (`androidApp`) and use the Android-KMP library plugin in the shared module. ```kotlin // shared/build.gradle.kts plugins { alias(libs.plugins.kotlin.multiplatform) alias(libs.plugins.android.kotlin.multiplatform.library) // id: com.android.kotlin.multiplatform.library } kotlin { android { namespace = "com.example.shared" compileSdk = 36 // Compose Multiplatform 1.13 needs 37.1; see the CMP page minSdk = 24 } jvm() // desktop or server iosArm64() iosSimulatorArm64() // iOS framework that the Xcode project imports listOf(iosArm64(), iosSimulatorArm64()).forEach { it.binaries.framework { baseName = "Shared" isStatic = true } } sourceSets { commonMain.dependencies { implementation(libs.kotlinx.coroutines.core) } commonTest.dependencies { implementation(kotlin("test")) } } } ``` ```toml # gradle/libs.versions.toml (from the Android KMP plugin docs) [plugins] kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } android-kotlin-multiplatform-library = { id = "com.android.kotlin.multiplatform.library", version.ref = "androidGradlePlugin" } ``` Until you migrate you can keep the old setup with `android.enableLegacyVariantApi=true` in `gradle.properties`, but those APIs are removed in AGP 10. Guide: [updating to AGP 9](https://kotlinlang.org/docs/multiplatform/multiplatform-project-agp-9-migration.html). ## Platform APIs: expect / actual Declare the API in common code, implement it per platform. Rules: the `actual` sits in the same package, `expect` declarations have no implementation, and every platform source set needs an `actual`. ```kotlin // commonMain package identity class Identity(val userName: String, val processID: Long) expect fun buildIdentity(): Identity ``` ```kotlin // jvmMain package identity import java.lang.System import java.lang.ProcessHandle actual fun buildIdentity() = Identity(System.getProperty("user.name") ?: "none", ProcessHandle.current().pid()) ``` ```kotlin // nativeMain (iOS and other native targets) package identity import kotlinx.cinterop.toKString import platform.posix.getlogin import platform.posix.getpid actual fun buildIdentity() = Identity(getlogin()?.toKString() ?: "none", getpid().toLong()) ``` Often an interface plus dependency injection is cleaner than `expect`/`actual`: ```kotlin // commonMain interface Platform { val name: String } class Greeting(private val platform: Platform) { fun greet() = "Hello from ${platform.name}" } // androidMain: class AndroidPlatform : Platform { override val name = "Android ${android.os.Build.VERSION.SDK_INT}" } // iosMain: class IosPlatform : Platform { override val name = platform.UIKit.UIDevice.currentDevice.systemName() } ``` Docs: [expected and actual declarations](https://kotlinlang.org/docs/multiplatform/multiplatform-expect-actual.html), [use platform-specific APIs](https://kotlinlang.org/docs/multiplatform/multiplatform-connect-to-apis.html). ## iOS ```text Xcode build phase runs the Gradle task embedAndSignAppleFrameworkForXcode Framework baseName and isStatic are set in the iOS binaries block (see Gradle Setup) Swift from Kotlin Kotlin types appear to Swift through Objective-C; Swift export is Beta (Kotlin 2.5) Swift packages SwiftPM import is Beta: use SDKs such as Firebase or Google Maps from KMP Nicer Swift API SKIE (Touchlab) improves how Kotlin flows, sealed classes and enums look in Swift Exported libraries a library whose types you expose to Swift must be exported: export("group:artifact:version") in the framework block ``` ## Libraries Search [klibs.io](https://klibs.io/) first: JetBrains' AI-powered search for Kotlin Multiplatform libraries (5,100+ projects), open source at [JetBrains/klibs-io](https://github.com/JetBrains/klibs-io), with an [FAQ](https://klibs.io/faq). A community Claude Code skill, [Wavesonics/klibs-skill](https://github.com/Wavesonics/klibs-skill), searches it from a plain-language description. The usual picks: | Need | Library | |---|---| | Async | [kotlinx.coroutines](https://github.com/Kotlin/kotlinx.coroutines) | | JSON | [kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization) | | Date and time | [kotlinx-datetime](https://github.com/Kotlin/kotlinx-datetime) | | HTTP | [Ktor client](https://github.com/ktorio/ktor) (an engine per platform: OkHttp on Android, Darwin on iOS) | | Database | [SQLDelight](https://github.com/sqldelight/sqldelight) | | Key-value settings | [multiplatform-settings](https://github.com/russhwolf/multiplatform-settings) | | Dependency injection | [Koin](https://github.com/InsertKoinIO/koin) | | Images | [Coil 3](https://github.com/coil-kt/coil) | | Logging | [Kermit](https://github.com/touchlab/Kermit) | | I/O | [Okio](https://github.com/square/okio), [kotlinx-io](https://github.com/Kotlin/kotlinx-io) | | Navigation and components | [Decompose](https://github.com/arkivanov/Decompose), [Voyager](https://github.com/adrielcafe/voyager) | | Platform UI and APIs from Compose | [Calf](/programming/compose-multiplatform/#calf-platform-apis-and-adaptive-ui) | | Starter | [KaMP Kit](https://github.com/touchlab/KaMPKit) (Touchlab) | ## Publishing a Library Kotlin 2.5 adds an experimental publication format that packs platform artifacts into one `.kar` archive, which keeps multi-target libraries under Maven Central size limits: set `kotlin.publicationFormat=KOTLIN_ARCHIVE` in `gradle.properties`. Consuming such artifacts already works for every project. ## Troubleshooting | Symptom | Cause | Fix | |---|---|---| | Gradle error after moving to AGP 9 | KMP plugin cannot be combined with `com.android.application` or `com.android.library` | Move the Android entry point to `androidApp`, use the Android-KMP library plugin in `shared` | | Unresolved reference to `java.io.File` in common code | JDK classes do not exist on native targets | Use a multiplatform library (kotlinx-io, Okio) or `expect`/`actual` | | `actual` is flagged as missing or mismatched | Different package, signature, or no `actual` for one target | Same package and signature, one `actual` per platform source set | | Library types missing in Swift | Dependency not exported from the iOS framework | Add `export(...)` to the framework block | | iOS build fails in Xcode only | Stale framework or missing toolchain | Run `kdoctor`, clean the build, rerun the Gradle embed task | | Code placed in `iosArm64Main` but not seen by the simulator | Target-specific source set | Put shared iOS code in `iosMain` | ## Quick Reference | Item | Value | |---|---| | Shared code | `src/commonMain/kotlin` | | Declare targets | `kotlin { jvm(); iosArm64(); iosSimulatorArm64() }` | | Android target (AGP 9) | `kotlin { android { namespace = "..."; compileSdk = ...; minSdk = ... } }` | | Platform API | `expect fun x()` in common, `actual fun x()` per platform, same package | | All tests | `./gradlew allTests` | | iOS tests | `./gradlew iosSimulatorArm64Test` | | Check the environment | `kdoctor` | ## See Also - [Kotlin Multiplatform documentation](https://kotlinlang.org/docs/multiplatform/get-started.html) - [Project structure basics](https://kotlinlang.org/docs/multiplatform/multiplatform-discover-project.html) and [hierarchy](https://kotlinlang.org/docs/multiplatform/multiplatform-hierarchy.html) - [Android Gradle library plugin for KMP](https://developer.android.com/kotlin/multiplatform/plugin) - [KMP Wizard](https://kmp.jetbrains.com/), [klibs.io](https://klibs.io/) (library search, [source](https://github.com/JetBrains/klibs-io)), [KDoctor](https://github.com/Kotlin/kdoctor) - [Compose Multiplatform](/programming/compose-multiplatform/) - shared UI on top of KMP - [Kotlin](/programming/kotlin/) - language reference and what is new in 2.3 to 2.5