commonMain, androidMain & iosMain
The three source sets you will live in every day.
commonMain, androidMain & iosMain is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Source Sets, Not Folders
Inside shared, code lives in source sets. Each one targets a different platform, and Kotlin compiles them accordingly.
Meet commonMain
commonMain is where shared Kotlin lives. Code here compiles for every target, so it must stay platform-neutral.
Meet androidMain
androidMain holds Android-only code in the shared module. Here you can touch JVM and Android APIs freely.
Meet iosMain
iosMain holds iOS-only Kotlin. It can reach Apple frameworks through Kotlin/Native interop.
A Place for Everything
Put cross-platform logic in commonMain. Drop into androidMain or iosMain only when you truly need that platform.
What commonMain Looks Like
Plain Kotlin with no platform imports belongs in commonMain, like this small greeting function.
fun greet(name: String): String {
return "Hello, " + name
}Platform Code in androidMain
An Android API call belongs in androidMain, never in commonMain, because iOS has no such class.
import android.os.Build
fun osName(): String = Build.MODELThey Share One Name
commonMain can declare an API and each platform set fills it in. That handshake is the expect/actual pattern you will meet soon.
Folder Layout
On disk the sets sit under src: src/commonMain, src/androidMain, src/iosMain, each with its own kotlin folder.
Compilation Merges Sets
For Android the compiler merges commonMain with androidMain. For iOS it merges commonMain with iosMain.
Start in Common
A good habit: write in commonMain first, and only move code outward when the compiler tells you a platform API is missing. 🙂
Quick Check
Where should this code go?
Recap
Three homes: commonMain for shared logic, androidMain for Android-only, iosMain for iOS-only. Default to common.
Frequently asked questions
Is the “commonMain, androidMain & iosMain” lesson free?
Yes — the full text of “commonMain, androidMain & iosMain” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.
What will I learn in “commonMain, androidMain & iosMain”?
The three source sets you will live in every day. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Multiplatform Academy?
No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “commonMain, androidMain & iosMain” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Multiplatform Academy lesson?
Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The shared Module vs the App Modules
- commonMain, androidMain & iosMain
- Reading the shared build.gradle.kts
- Where Tests Live: commonTest & Friends