Kotlin Multiplatform for Kotlin: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Kotlin Multiplatform and why is it needed?

\n\n

Kotlin Multiplatform (KMP) is not a library in the classical sense, but a technology (SDK and set of tools) from JetBrains that allows you to write shared code in Kotlin and compile it for multiple platforms: JVM (Android, servers), JavaScript (web browsers, Node.js), Native (iOS, macOS, Windows, Linux, watchOS, tvOS).

\n\n

Why is it needed? In modern development, it is often necessary to create applications for different platforms. Traditionally, this meant writing code in different languages (Swift for iOS, Java/Kotlin for Android, JavaScript for the web). Kotlin Multiplatform solves this problem by allowing you to move business logic (network requests, database operations, data validation, serialization) into a shared module that is used on all platforms. Platform-specific code (UI, camera work, sensors) remains native, but its volume is significantly reduced.

\n\n

Key advantages:

\n
    \n
  • Reduced development time: Write the logic once — use it everywhere.
  • \n
  • Single codebase: Fewer bugs, easier to maintain and refactor.
  • \n
  • Native performance: Unlike React Native or Flutter, KMP compiles to native code, not through an abstraction layer.
  • \n
  • Flexibility: You can start small — extract just one library (e.g., for network requests) and gradually expand the shared code.
  • \n
  • Use of existing code: You can connect native libraries via the expect/actual mechanism.
  • \n
\n\n

Kotlin Multiplatform is ideal for: mobile applications (Android + iOS), server applications (Ktor + KMP), web applications (Kotlin/JS), desktop applications (Compose Multiplatform), and libraries that need to work everywhere.

\n\n

Installation and Setup

\n\n

Kotlin Multiplatform is not installed via pip or npm. It is a build tool configured through the Gradle build system. Below is the standard process for creating a project with KMP.

\n\n

1. Install IntelliJ IDEA or Android Studio

\n

It is recommended to use IntelliJ IDEA Ultimate (Community Edition also works, but with limitations) or Android Studio with the Kotlin Multiplatform plugin.

\n\n

2. Create a Project

\n

The easiest way is to use the Kotlin Multiplatform Wizard (online tool from JetBrains): https://kmp.jetbrains.com/. Select the required platforms (Android, iOS, Desktop, Web) and download the ready-made template.

\n\n

3. Project Structure (Gradle)

\n

The main configuration file is build.gradle.kts (root and module-level). Key plugins:

\n
// Root build.gradle.kts\nplugins {\n    kotlin("multiplatform") version "1.9.22" apply false\n    kotlin("plugin.serialization") version "1.9.22" apply false\n    id("com.android.application") version "8.2.0" apply false\n    id("org.jetbrains.compose") version "1.5.11" apply false\n}\n\n// Shared module (shared code)\nplugins {\n    kotlin("multiplatform")\n    kotlin("plugin.serialization")\n    id("com.android.library")\n}\n\nkotlin {\n    androidTarget {\n        compilations.all {\n            kotlinOptions {\n                jvmTarget = "1.8"\n            }\n        }\n    }\n\n    listOf(\n        iosX64(),\n        iosArm64(),\n        iosSimulatorArm64()\n    ).forEach {\n        it.binaries.framework {\n            baseName = "shared"\n            isStatic = true\n        }\n    }\n\n    sourceSets {\n        val commonMain by getting {\n            dependencies {\n                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")\n                implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.2")\                    

Recommendations