Kotlin Multiplatform Intro
Kotlin Multiplatform (KMP) lets you write core logic once and run it on Android, iOS, the JVM, and beyond — sharing models, networking, and business rules while keeping native UI where it belongs.
Learn Kotlin Multiplatform Intro in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end you'll understand the commonMain shared module, the expect / actual mechanism, and what's worth sharing versus keeping platform-specific.
What You'll Learn in This Lesson
1️⃣ The Shared Module (commonMain)
Code in commonMain compiles for every target. Pure Kotlin logic, data models, and serialization live here and are reused unchanged across platforms.
One Greeter , used by every platform — no copy-paste between a Swift version and a Kotlin version.
2️⃣ expect / actual
When you need platform-specific behavior, declare an expect in common code and provide an actual in each platform source set such as androidMain and iosMain .
Your turn. Replace the TODO in commonMain, then reason about each platform's result.
Write a pure function for commonMain and explain why it needs no platform code.
📋 Quick Reference — Kotlin Multiplatform
Practice quiz
What does Kotlin Multiplatform (KMP) let you do?
- Run JavaScript only
- Share Kotlin code across platforms like Android, iOS, and the JVM
- Replace Gradle
- Write apps in Java
Answer: Share Kotlin code across platforms like Android, iOS, and the JVM. KMP lets you write common logic once and target multiple platforms.
Which source set holds code shared across all platforms?
- androidMain
- iosMain
- commonMain
- sharedTest
Answer: commonMain. commonMain contains platform-agnostic code shared by every target.
What is the 'expect' keyword used for?
- To declare an API in common code that each platform must implement
- To throw an exception
- To import a library
- To define a coroutine
Answer: To declare an API in common code that each platform must implement. expect declares a common API; platforms provide it with actual.
What does the 'actual' keyword provide?
- A test stub
- A platform-specific implementation of an expect declaration
- A default value
- A null check
Answer: A platform-specific implementation of an expect declaration. actual supplies the concrete, per-platform implementation of an expect declaration.
Where would an iOS-specific implementation typically live?
- commonMain
- androidMain
- jvmTest
- iosMain
Answer: iosMain. iosMain holds the iOS-specific actual implementations and platform code.
Which of these is commonly shared in a KMP project?
- Per-platform UI widgets only
- Business logic, networking, and data models
- Android XML layouts
- iOS storyboards
Answer: Business logic, networking, and data models. Shared modules typically hold business logic, models, networking, and serialization.
What is usually NOT fully shared and stays platform-specific?
- Data classes
- Pure Kotlin algorithms
- Native UI and platform APIs
- String constants
Answer: Native UI and platform APIs. Native UI and direct platform APIs are typically implemented per platform (though Compose Multiplatform can share UI).
An expect declaration without a matching actual on a target causes what?
- It silently does nothing
- A compile error for that target
- A runtime crash only
- Automatic code generation
Answer: A compile error for that target. Every target compiling the common code must provide an actual, or it fails to compile.
Which build tool orchestrates a typical Kotlin Multiplatform project?
- Maven only
- npm
- Gradle with the Kotlin Multiplatform plugin
- Make
Answer: Gradle with the Kotlin Multiplatform plugin. KMP projects are configured with Gradle and the Kotlin Multiplatform plugin.
What is a key benefit of sharing code with KMP?
- It removes the need for testing
- Write core logic once and reuse it, reducing duplication and bugs
- It compiles to assembly
- It bans platform-specific code
Answer: Write core logic once and reuse it, reducing duplication and bugs. Sharing common logic cuts duplication and keeps behavior consistent across platforms.