Interview QuestionPractical QuestionFollow-up Questions

Kotlin Multiplatform

skydovesJaewoong Eum (skydoves)||8 min read

Kotlin Multiplatform

Kotlin Multiplatform (KMP) is a framework by JetBrains that enables sharing code across multiple platforms while preserving access to native APIs where platform specific behavior is needed. Unlike cross platform frameworks that abstract away the platform entirely, KMP compiles shared code into platform native binaries, producing JVM bytecode for Android, native binaries for iOS, and JavaScript for web targets. By the end of this lesson, you will be able to:

  • Explain the expect/actual mechanism for platform specific implementations.
  • Describe how KMP compiles shared code into platform native output.
  • Identify the role of source sets in organizing shared and platform specific code.
  • Compare KMP with cross platform alternatives in terms of performance and native access.
  • Describe how dependency injection and architecture patterns apply in a KMP project.

Source Sets and Project Structure

A KMP project organizes code into source sets that form a hierarchy. The commonMain source set contains code shared across all platforms. Platform specific source sets like androidMain, iosMain, and jsMain extend commonMain and provide implementations that depend on platform APIs:

// commonMain/src/commonMain/kotlin/Platform.kt
expect fun platformName(): String

// androidMain/src/androidMain/kotlin/Platform.android.kt
actual fun platformName(): String = "Android ${Build.VERSION.SDK_INT}"

// iosMain/src/iosMain/kotlin/Platform.ios.kt
actual fun platformName(): String = UIDevice.currentDevice.systemName()

The Gradle plugin resolves the source set hierarchy at build time and compiles each target with its matching actual declarations. Code in commonMain can only reference APIs available in the Kotlin standard library and other multiplatform libraries. Any platform specific API must be accessed through the expect/actual mechanism or by placing code directly in a platform source set.

The Expect and Actual Mechanism

The expect/actual pattern is the primary way KMP handles platform divergence. An expect declaration in common code defines a function, class, or property signature without an implementation. Each platform source set provides a matching actual declaration with the real implementation:

// commonMain
expect class HttpClient() {
    suspend fun get(url: String): String
}

// androidMain
actual class HttpClient actual constructor() {
    actual suspend fun get(url: String): String {
        return URL(url).readText()
    }
}

// iosMain
actual class HttpClient actual constructor() {
    actual suspend fun get(url: String): String {
        return NSURLSession.shared.dataTaskForUrl(url)
    }
}

The compiler verifies at build time that every expect declaration has a corresponding actual in each configured target. A missing actual produces a compilation error, which prevents runtime failures from unimplemented platform code.

This interview continues for subscribers

Subscribe to Dove Letter for full access to exclusive interviews about Android and Kotlin development.

Become a Sponsor