Kotlin: The Secret Weapon for Cross-Platform Magic

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

Why Kotlin is Not Just for Android?

\n\n

If you still think Kotlin is just an "improved Java" for Android, you are missing the big picture. In a world where everyone wants to write one code and run it everywhere, Kotlin Multiplatform (KMP) is becoming a true "killer" of frameworks like Flutter and React Native. But unlike them, Kotlin does not force you to abandon native UI or learn JavaScript.

\n\n

Imagine: you write business logic, network requests, and database operations once in Kotlin. Then you use this code on Android, iOS, the web, and even on the server (Ktor). Sounds like science fiction? Welcome to the reality of cross-platform development with Kotlin.

\n\n

In this article, we will not just talk about theory. We will show real code examples, compare KMP with competitors, and give you a step-by-step plan to start today.

\n\n

What is Kotlin Multiplatform (KMP) and Why Do You Need It?

\n\n

KMP is not a framework. It is a compilation technology built into the Kotlin language. It allows you to compile common code into different platforms:

\n\n
    \n
  • Android — JVM bytecode (as usual).
  • \n
  • iOS — native binary (via LLVM).
  • \n
  • JavaScript/Web — JS code for the browser.
  • \n
  • Native (Linux, Windows, macOS) — via Kotlin/Native.
  • \n
\n\n

The main feature: you write expect/actual — declare the expected API in common code, and implement it separately for each platform. Everything else is shared.

\n\n

Example 1: Shared Module for Network Requests

\n\n

Let's write a simple HTTP client that will work on Android and iOS. For this, we will use Ktor — a client written in Kotlin.

\n\n

Step 1: Project Structure (Gradle)

\n\n

In your shared module's build.gradle.kts file:

\n\n
plugins {\n    kotlin("multiplatform")\n    id("io.ktor.plugin") version "2.3.7"\n}\n\nkotlin {\n    androidTarget()\n    iosX64()\n    iosArm64()\n    iosSimulatorArm64()\n\n    sourceSets {\n        val commonMain by getting {\n            dependencies {\n                implementation("io.ktor:ktor-client-core:2.3.7")\n                implementation("io.ktor:ktor-client-content-negotiation:2.3.7")\n                implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.7")\n            }\n        }\n        val androidMain by getting {\n            dependencies {\n                implementation("io.ktor:ktor-client-android:2.3.7")\n            }\n        }\n        val iosMain by creating {\n            dependsOn(commonMain)\n            dependencies {\n                implementation("io.ktor:ktor-client-darwin:2.3.7")\n            }\n        }\n    }\n}
\n\n

Step 2: Common Code (commonMain)

\n\n

Let's create a data class and an API client:

\n\n
// commonMain/kotlin/com/example/User.kt\nimport kotlinx.serialization.Serializable\n\n@Serializable\ndata class User(\n    val id: Int,\n    val name: String,\n    val email: String\n)\n\n// commonMain/kotlin/com/example/ApiClient.kt\nimport io.ktor.client.*\nimport io.ktor.client.call.*\nimport io.ktor.client.plugins.contentnegotiation.*\nimport io.ktor.client.request.*\nimport io.ktor.serialization.kotlinx.json.*\n\nclass ApiClient {\n    private val client = HttpClient {\n        install(ContentNegotiation) {\n            json()\n        }\n    }\n\n    suspend fun fetchUser(id: Int): User {\n        return client.get("https://jsonplaceholder.typicode.com/users/$id").body()\n    }\n}
\n\n

See? No ifs, no platform checks. Just pure Kotlin.

\n\n

Example 2: Expect/Actual for Working with SharedPreferences

\n\n

Suppose we need to save a token. On Android — SharedPreferences, on iOS — UserDefaults. Let's show the magic of KMP:

\n\n

Common Code (commonMain):

\n\n
// commonMai                

Blogs

Book Recommendations