Kotlin Multiplatform Cheat Sheet
Covers Kotlin Multiplatform project structure, expect/actual declarations, shared source sets, and common targets for sharing code across platforms.
2 PagesAdvancedApr 15, 2026
expect / actual Basics
Declaring a common API with platform-specific implementations.
kotlin
// commonMain/kotlin/Platform.ktexpect class Platform() { val name: String}expect fun getPlatform(): Platform// androidMain/kotlin/Platform.android.ktactual class Platform actual constructor() { actual val name: String = "Android ${android.os.Build.VERSION.SDK_INT}"}actual fun getPlatform(): Platform = Platform()// iosMain/kotlin/Platform.ios.ktactual class Platform actual constructor() { actual val name: String = UIDevice.currentDevice.systemName()}actual fun getPlatform(): Platform = Platform()
Gradle Source Sets
Configuring targets and shared source sets in build.gradle.kts.
yaml
kotlin { androidTarget() iosX64() iosArm64() iosSimulatorArm64() jvm("desktop") sourceSets { val commonMain by getting { dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0") } } val androidMain by getting val iosMain by creating { dependsOn(commonMain) } val iosX64Main by getting { dependsOn(iosMain) } val iosArm64Main by getting { dependsOn(iosMain) } }}
Common Compilation Targets
Platforms Kotlin Multiplatform can compile to.
- androidTarget()- Compiles Kotlin/JVM bytecode for Android
- jvm()- Compiles Kotlin/JVM for desktop or server targets
- iosX64() / iosArm64() / iosSimulatorArm64()- Compiles Kotlin/Native for iOS simulator and device
- js(IR)- Compiles Kotlin/JS using the modern IR compiler backend
- wasmJs()- Compiles Kotlin/Wasm for WebAssembly targets
- linuxX64() / macosX64() / mingwX64()- Kotlin/Native targets for desktop platforms
Common vs. Platform Code
Where shared logic and platform-only code each belong.
kotlin
// commonMain: shared business logic, no platform APIsclass UserRepository(private val api: ApiClient) { suspend fun getUser(id: String) = api.fetchUser(id)}// commonMain can use expect declarations for anything platform-specific,// like file storage, HTTP engines (via Ktor's multiplatform client), or// platform-specific date/time formatting.// androidMain / iosMain: actual implementations, plus platform-only code// that isn't part of any expect declaration at all (e.g. Android Activity// classes or iOS SwiftUI views) lives entirely in its own source set.
Popular KMP Libraries
Widely used multiplatform-compatible libraries.
- Ktor Client- Multiplatform HTTP client with pluggable engines per platform
- kotlinx.serialization- Multiplatform JSON/CBOR/protobuf serialization
- kotlinx.coroutines- Multiplatform coroutines, including Flow, work across all KMP targets
- SQLDelight- Generates typed Kotlin APIs from SQL, with multiplatform database drivers
- Koin / Kodein- Multiplatform-friendly dependency injection frameworks
- Compose Multiplatform- JetBrains UI framework sharing UI code across Android, iOS, desktop, and web
Pro Tip
Keep expect/actual declarations as thin as possible — put as much logic as you can in commonMain and only push the unavoidable platform-specific bits (file I/O, native APIs) behind the expect/actual boundary, so most of your codebase stays testable once instead of per-platform.
Was this cheat sheet helpful?
Explore Topics
#KotlinMultiplatform#KotlinMultiplatformCheatSheet#Programming#Advanced#ExpectActualBasics#GradleSourceSets#CommonCompilationTargets#CommonVsPlatformCode#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance