Compose 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

Compose Multiplatform for Kotlin: Complete Guide

\n\n

Welcome to the most detailed guide on Compose Multiplatform (CMP) — a modern technology from JetBrains that allows you to create native user interfaces for Android, iOS, Desktop (Windows, macOS, Linux) and Web using a single codebase in Kotlin. In this article, we will cover everything: from installation to advanced techniques and best practices.

\n\n

1. What is it and why is it needed?

\n\n

Compose Multiplatform is a declarative framework for building user interfaces, based on Jetpack Compose from Google, but extended to work on multiple platforms. Unlike traditional approaches (e.g., separate XML layouts for Android and SwiftUI for iOS), CMP allows you to write UI logic once and compile it into native code for each target platform.

\n\n

Why is this needed? First, it is a radical reduction in development time: instead of maintaining two or more codebases, you work with one. Second, UI consistency: the business logic and appearance of the application are identical on all platforms, simplifying testing and maintenance. Third, access to native APIs: you can use platform-specific code (e.g., camera or geolocation) via the expect/actual mechanism without leaving the Kotlin ecosystem.

\n\n

Compose Multiplatform is ideal for startups, product teams, and developers who want to reach the maximum audience without duplicating efforts. The library is actively developed by JetBrains and the community, and as of 2025, it is one of the most promising solutions in the world of cross-platform development.

\n\n

2. Installation

\n\n

Since Compose Multiplatform is not a library in the traditional sense (like a pip package), but rather a set of plugins and configurations for Gradle, installation is done through the Kotlin Multiplatform build system. Here is the exact sequence of steps to create a new project:

\n\n

Step 1: Creating a project via IntelliJ IDEA (recommended)

\n

Open IntelliJ IDEA (Community or Ultimate), select File → New → Project. In the wizard, select the Compose Multiplatform template. Specify the project name and click "Finish". The IDE will automatically generate the structure and all necessary dependencies.

\n\n

Step 2: Manual setup (for existing projects)

\n

Add the following code to the root build.gradle.kts:

\n\n
plugins {\n    kotlin("multiplatform") version "2.0.21"\n    id("org.jetbrains.compose") version "1.7.1"\n    id("org.jetbrains.kotlin.plugin.compose") version "2.0.21"\n}\n\nkotlin {\n    androidTarget()\n    listOf(\n        iosX64(),\n        iosArm64(),\n        iosSimulatorArm64()\n    ).forEach { iosTarget ->\n        iosTarget.binaries.framework {\n            baseName = "ComposeApp"\n            isStatic = true\n        }\n    }\n\n    sourceSets {\n        val commonMain by getting {\n            dependencies {\n                implementation(compose.runtime)\n                implementation(compose.foundation)\n                implementation(compose.material3)\n                implementation(compose.ui)\n            }\n        }\n        val androidMain by getting {\n            dependencies {\n                implementation("androidx.activity:activity-compose:1.9.3")\n            }\n        }\n    }\n}
\n\n

Important: Plugin and Kotlin versions must be compatible. Current versions can always be found on the Compose Multiplatform GitHub repository.

\n\n

3. Quick start — minimal working example

\n\n

After creating the project, open the file commonMain/kotlin/App.kt and replace its contents with the following code:

\n\n
import androidx.c                    

Recommendations