TExcelTAD
Home › Kotlin › Kotlin Multiplatform

Kotlin Multiplatform

Most apps exist on several platforms: Android, iOS, the web, maybe desktop and a server. Writing the same business logic — validation, pricing rules, networking, data models — once per platform is slow and leads to subtle differences. Kotlin Multiplatform (KMP) lets you write that logic once in Kotlin and compile it for every target, while each app can keep its native UI.

How it works

The Kotlin compiler has several back-ends:

Target Compiles to Runs on
Kotlin/JVM JVM bytecode Android, servers, desktop
Kotlin/Native native machine code via LLVM iOS, macOS, Linux, Windows
Kotlin/JS JavaScript browsers, Node.js
Kotlin/Wasm WebAssembly modern browsers

A KMP project has a common source set that every target compiles, plus platform source sets for code that needs platform APIs:

text
shared/
  src/
    commonMain/kotlin/     shared logic (pure Kotlin + multiplatform libraries)
    androidMain/kotlin/    Android-specific implementations
    iosMain/kotlin/        iOS-specific implementations
    commonTest/kotlin/     tests that run on every platform
androidApp/                Android UI (Jetpack Compose)
iosApp/                    iOS UI (SwiftUI) - calls the shared framework

On iOS the shared module is packaged as a framework that Swift code imports like any other.

Common code is plain Kotlin

Anything that only uses the Kotlin standard library can live in commonMain. For example, a shared password checker used by both the Android and the iOS sign-up screen:

kotlin · try it yourself
data class PasswordCheck(val ok: Boolean, val problems: List<String>)

fun checkPassword(pw: String): PasswordCheck {
    val problems = buildList {
        if (pw.length < 8) add("at least 8 characters")
        if (pw.none { it.isDigit() }) add("a digit")
        if (pw.none { it.isUpperCase() }) add("an uppercase letter")
    }
    return PasswordCheck(problems.isEmpty(), problems)
}

fun main() {
    for (pw in listOf("kotlin", "Kotlin2024", "multiplatform1")) {
        val r = checkPassword(pw)
        println(if (r.ok) "$pw: OK" else "$pw: needs ${r.problems.joinToString(", ")}")
    }
}
kotlin: needs at least 8 characters, a digit, an uppercase letter
Kotlin2024: OK
multiplatform1: needs an uppercase letter

Every platform now shows exactly the same messages, because there is only one implementation.

expect and actual

When common code needs something platform-specific — the device name, a UUID, secure storage — it declares an expect declaration, and each platform provides the actual implementation:

kotlin
// commonMain
expect fun platformName(): String

fun greeting() = "Hello from ${platformName()}!"
kotlin
// androidMain
actual fun platformName(): String = "Android ${android.os.Build.VERSION.SDK_INT}"
kotlin
// iosMain
import platform.UIKit.UIDevice

actual fun platformName(): String =
    UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion

The compiler checks that every expect has a matching actual for each target. Often an interface plus dependency injection is an equally good alternative.

The library ecosystem

Common code can't use Java libraries (they don't exist on iOS), so KMP has its own multiplatform libraries:

Need Library
asynchronous code kotlinx.coroutines
JSON serialization kotlinx.serialization
HTTP client Ktor client
dates and times kotlinx-datetime
local database SQLDelight, Room (KMP)
shared UI Compose Multiplatform

Sharing the UI too: Compose Multiplatform

KMP lets you choose how much to share:

  1. Logic only — shared module + native UIs (Compose on Android, SwiftUI on iOS).
  2. Logic and UI — Compose Multiplatform from JetBrains renders the same Compose UI on Android, iOS, desktop and web.

The composables you saw in the Android chapter work almost unchanged in Compose Multiplatform.

Simulating a platform difference

You can model the expect/actual idea in plain Kotlin with an interface — the same code, different platform behaviour:

kotlin · try it yourself
interface Platform {
    val name: String
    fun lineSeparatorName(): String
}

class AndroidPlatform : Platform {
    override val name = "Android"
    override fun lineSeparatorName() = "LF"
}

class WindowsPlatform : Platform {
    override val name = "Windows"
    override fun lineSeparatorName() = "CRLF"
}

fun describe(p: Platform) = "Running on ${p.name}, lines end with ${p.lineSeparatorName()}"

fun main() {
    listOf(AndroidPlatform(), WindowsPlatform()).forEach { println(describe(it)) }
}
Running on Android, lines end with LF
Running on Windows, lines end with CRLF

When is KMP a good fit?

Good fit Think twice
Android + iOS apps with substantial shared business logic very thin apps that are almost all UI
a team that already knows Kotlin a team with no Kotlin experience and a tight deadline
need for native performance and native look heavy reliance on platform-only libraries

Note: KMP is stable and used in production by companies such as Netflix, McDonald's, Cash App and Philips. Start with a template from the Kotlin Multiplatform wizard (kmp.jetbrains.com) and open it in IntelliJ IDEA or Android Studio.

Test yourself

Which source set contains code shared by all platforms?

  1. androidMain
  2. iosMain
  3. commonMain
  4. sharedUi

What are expect and actual used for?

  1. Unit testing
  2. Declaring a common API whose implementation differs per platform
  3. Exception handling
  4. Type casting

Which Kotlin back-end produces code for iOS?

  1. Kotlin/JVM
  2. Kotlin/JS
  3. Kotlin/Native
  4. Kotlin/Wasm