Kotlin Multiplatform Intro
In this page:
The Idea Behind Multiplatform
Kotlin Multiplatform lets a single shared codebase, written once, be compiled to run on the JVM (Android/server), natively (iOS/desktop), and even JavaScript (web), avoiding duplicated business logic across platforms.
Example: The Idea Behind Multiplatform
fun calculateTotal(prices: List<Double>): Double = prices.sum()
fun main() {
// This same function could be shared across Android, iOS, and web targets
println("Total: ${calculateTotal(listOf(9.99, 19.99, 4.99))}")
}
Login to try C/C++/Java/PHP code in the editor
expect and actual Declarations
Common code declares an expect function or class when the implementation must differ per platform, and each target provides a matching actual implementation with the real, platform-specific code. (The expect/actual keywords need a real multiplatform Gradle module to compile; the snippet below simulates the same idea with an ordinary function standing in for the platform-specific actual one.)
Example: expect and actual Declarations
fun platformName(): String = "JVM"
fun greetPlatform(): String = "Running on ${platformName()}"
fun main() {
// In a real KMP project, platformName() would be declared
// `expect fun platformName(): String` in commonMain, with each
// target (jvm, iosArm64, js) providing its own `actual` version.
println(greetPlatform())
}
Login to try C/C++/Java/PHP code in the editor
What Is Usually Shared
Kotlin Multiplatform projects typically share business logic like networking, data parsing, and validation rules, while UI code often remains platform-specific (SwiftUI on iOS, Jetpack Compose or Views on Android).
Example: What Is Usually Shared
data class Product(val name: String, val price: Double)
fun applyDiscount(product: Product, percent: Int): Product =
product.copy(price = product.price * (100 - percent) / 100)
fun main() {
val product = Product("Headphones", 100.0)
println(applyDiscount(product, 20))
}
Login to try C/C++/Java/PHP code in the editor
Setting Up a Multiplatform Project
A Kotlin Multiplatform project is configured through Gradle with a kotlin { } block declaring targets like jvm(), iosArm64(), or js(), each with its own source set alongside a shared commonMain source set.
Example: Setting Up a Multiplatform Project
kotlin {
jvm()
iosArm64()
sourceSets {
val commonMain by getting {
dependencies {
implementation(kotlin("stdlib-common"))
}
}
}
}
⚠️ Run this command in your terminal.
- Assuming Kotlin Multiplatform automatically shares UI code too; typically only business logic is shared, while UI is usually still platform-specific (unless also using Compose Multiplatform).
- Forgetting
expect/actualdeclarations require every target platform to provide a matchingactualimplementation, or the build fails for that target. - Mixing up Kotlin Multiplatform (targeting multiple platforms from one codebase) with Kotlin/JS or Kotlin/Native individually, which are just single-target compilation backends.
- Kotlin Multiplatform (KMP) lets shared Kotlin code compile to JVM, JavaScript, and native targets (iOS, desktop, etc.) from one codebase.
- Common code lives in a shared module, using
expectdeclarations for anything that needs platform-specific implementation. - Each target platform provides an
actualimplementation matching everyexpectdeclaration in the common code. - KMP is most commonly used to share business logic (networking, data models) between Android and iOS apps.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: