← Back to Kotlin Course | Chapter 14: JVM Interop & Best Practices | Lesson 4 of 7

Kotlin Multiplatform Intro

Kotlin Multiplatform lets you write one shared piece of logic that can run on Android, iOS, the web, and the desktop all at once.

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

markup
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))}")
}

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

markup
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())
}

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

markup
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))
}

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

bash
kotlin {
    jvm()
    iosArm64()
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib-common"))
            }
        }
    }
}

⚠️ Run this command in your terminal.

Common Mistakes
  1. 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).
  2. Forgetting expect/actual declarations require every target platform to provide a matching actual implementation, or the build fails for that target.
  3. Mixing up Kotlin Multiplatform (targeting multiple platforms from one codebase) with Kotlin/JS or Kotlin/Native individually, which are just single-target compilation backends.
Chapter Summary
  • 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 expect declarations for anything that needs platform-specific implementation.
  • Each target platform provides an actual implementation matching every expect declaration 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.