Kotlin Multiplatform (KMP) Overview: Concepts, Advantages, Demo, and Ecosystem
This article introduces Kotlin Multiplatform (KMP), explains its core concepts and advantages, walks through a simple demo project with code snippets and build configurations, and discusses the underlying compilation process, Compose UI sharing, ecosystem libraries, and real‑world industry adoption.
Introduction
As Android developers we are familiar with Kotlin and its excellent interoperability with Java, which makes Kotlin the preferred language for Android. While Kotlin/JVM runs on any device that supports the JVM, true cross‑platform support requires Kotlin Multiplatform (KMP), which can target iOS, web, desktop, and server environments.
What is KMP
KMP enables reuse of Kotlin code across Android, iOS, web, desktop, and server while still allowing native code where needed. This dual capability—broad platform coverage and native interop—sets KMP apart from other cross‑platform solutions.
Advantages of KMP
Performance and Flexibility
Shared Kotlin code is compiled into platform‑specific binaries (class files on JVM, JS files on browsers, native executables on Windows, etc.), eliminating the need for a runtime engine or bridge and thus avoiding extra performance overhead.
Because the compiled output is native code, interaction with platform APIs incurs no additional cost, and the binary size is comparable to a regular library rather than a full framework.
Applicability to Any Project
KMP can be introduced incrementally—only for shared business logic such as encryption or data models—or used for an entire codebase, including UI via Compose for Desktop and iOS.
Simple Demo
Project Creation
KMP projects are initialized via the JetBrains website (https://kmp.jetbrains.com/#newProject) which offers three templates: Android‑iOS with UI, Android‑iOS logic‑only, and multi‑platform logic‑only.
Project Structure
The generated library module contains commonMain for shared code and platformMain directories for platform‑specific implementations. The build.gradle file defines supported targets and dependencies.
Code Writing
expect val firstElement: Int
expect val secondElement: Int
fun twoSum(first: Int, second: Int): Int {
return first + second
}
fun main() {
val result = twoSum(firstElement, secondElement)
println(result)
}The expect declarations are given concrete values in platform‑specific source sets, e.g., for macOS:
actual val firstElement: Int = 7
actual val secondElement: Int = 8Running the macOS binary yields 15, demonstrating platform‑specific implementations.
Build Artifacts
By default, Kotlin/Native produces a .klib library. To obtain an executable or shared library, the Gradle script must declare binaries { executable { } } for macOS and sharedLib { } for Linux, then run the appropriate Gradle tasks (e.g., ./gradlew :library:runDebugExecutableMacosArm64 or ./gradlew :library:linkDebugSharedLinuxX64 ).
Implementation Principles
Kotlin’s compiler separates front‑end (syntax and semantic analysis) from back‑end (IR generation and machine‑code emission). The front‑end is platform‑agnostic, while each back‑end emits code for a specific target, enabling true cross‑platform compilation.
Compose Cross‑Platform Rendering
Compose relies on Skia for rendering. On Android, Skia is already part of the system; on other platforms JetBrains uses the skiko library to bundle Skia with the native binary.
KMP Ecosystem
JetBrains provides first‑party libraries such as Ktor, kotlinx‑serialization, kotlinx‑datetime, and coroutines. The community contributes projects like Square’s Okio and SQLDelight, and Google is gradually porting Android components to KMP.
Industry Cases
Several overseas companies use KMP, and Chinese firms like Meituan have shared detailed adoption experiences (see linked Bilibili videos).
Conclusion
The article gives a comprehensive overview of Kotlin Multiplatform, covering its definition, benefits, practical setup, compilation details, rendering pipeline, ecosystem, and real‑world usage, helping developers decide whether KMP fits their cross‑platform needs.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.