0Pricing
Kotlin Academy · Lesson

KMP Project Structure: commonMain, androidMain, iosMain

Understand the source set hierarchy and how code is shared across targets.

What Is KMP?

Kotlin Multiplatform (KMP) lets you write shared Kotlin code that compiles to JVM, Android, iOS (via Kotlin/Native), and JS. Platform-specific code lives in separate source sets.

// Typical KMP module structure:
// shared/
//   src/
//     commonMain/kotlin/   <- shared code
//     androidMain/kotlin/  <- Android-only
//     iosMain/kotlin/      <- iOS-only
//     commonTest/kotlin/   <- shared tests
fun main() { println("KMP: one codebase, many targets") }

Source Set Hierarchy

Source sets form a hierarchy. commonMain is the root; androidMain and iosMain depend on it. Code in commonMain is visible to all targets.

// build.gradle.kts (Kotlin DSL)
kotlin {
    androidTarget()
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    sourceSets {
        val commonMain by getting {
            dependencies { /* shared deps */ }
        }
        val androidMain by getting
        val iosMain by getting
    }
}

All lessons in this course

  1. KMP Project Structure: commonMain, androidMain, iosMain
  2. expect/actual Mechanism for Platform APIs
  3. Sharing Repository and Use Case Layers
  4. Dependency Injection in KMP with Koin
← Back to Kotlin Academy