Open In App

What is Kotlin Multiplatform?

Last Updated : 24 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Kotlin Multiplatform or KMP is a technology that allows you to write code once and run it anywhere natively. This native feature motivates developers to prefer KMP over other cross-platform frameworks. KMP supports mobile operating systems such as Android and IOS, web, and desktop operating systems such as Windows, macOS and Linux. It is an open-source, cross-platform development tool created by JetBrains that allows developers to create apps for multiple platforms while still reusing code.

Kotlin Multiplatform Mobile (KMM) which later was updated to just Kotlin Multiplatform (KMP) was announced as a mistake by Hadid Hariri, in Droidcon Berlin 2023, where he stated that "Kotlin Multiplatform Mobile (KMM) was a mistake because it was misleading, and from now on we should only refer to Kotlin Multiplatform (KMP)".

Getting started with Kotlin Multiplatform

  • Install Android Studio.
  • Install the latest version of the Java Development Kit (JDK).
  • Install the Kotlin Multiplatform plugin from within Android Studio.
kotlin-multiplatform-plugin
Kotlin Multiplatform Plugin
  • For IOS development: MacOS with Xcode installed is required.

Create your first Kotlin Multiplatform app

Step 1: Start Android Studio and create a new project.

create-new-project


Step 2: Choose the option Kotlin Multiplatform App from the given list of templates and then click Next.

kotlin-multiplatform-template-option


Step 3: Configure the initial details such as Project name, Package name, Save location and Minimum SDK and click Next.

configure-project-name-and-others


Step 4: Configure project details such choosing a name under the Android Application Name, iOS Application Name and Shared Module Name (The most preferred is androidApp, iosApp and shared respectively). Choose Regular framework (recommended) under the iOS framework distribution label and then click Finish.

configure-project-details


Step 5: Now let Android Studio build and setup the project.

Step 6: After the project setup is finished, you can build and run the project by either clicking on the "Run" button on the top bar of the screen or by pressing the keyboard shortcut Shift + F10. The below image shows the basic folder structure of the project that includes the android, iOS, and shared module.

Screenshot-2025-01-16-121258


Step 7: Ensure the dependencies are up-to-date by navigating to the two files build.grade.kts (Module :androidApp) and libs.versions.toml

build.gradle.kts
dependencies {
    implementation(projects.shared)
    implementation(libs.compose.ui)
    implementation(libs.compose.ui.tooling.preview)
    implementation(libs.compose.material3)
    implementation(libs.androidx.activity.compose)
    debugImplementation(libs.compose.ui.tooling)
}
libs.versions.toml
[versions]
agp = "8.5.0"
kotlin = "2.0.0"
compose = "1.5.4"
compose-material3 = "1.1.2"
androidx-activityCompose = "1.8.0"

[libraries]
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" }
compose-ui = { module = "androidx.compose.ui:ui", version.ref = "compose" }
compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose" }
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "compose" }
compose-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" }
compose-material3 = { module = "androidx.compose.material3:material3", version.ref = "compose-material3" }

[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
androidLibrary = { id = "com.android.library", version.ref = "agp" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlinCocoapods = { id = "org.jetbrains.kotlin.native.cocoapods", version.ref = "kotlin" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }


Step 8: To make changes to the app, navigate to Projects > shared > src > commonMain > kotlin > {package-name-directory} > Greeting.kt

Screenshot-2025-01-16-121855


Step 9: In the file Greeting.kt, make the following changes.

Greeting.kt
class Greeting 
{
    fun greet(): String 
  	{
      	// the greeting message is modified
        return "Hello, GeeksforGeeks!"
    }
}


Step 10: Now build and run the project by clicking on the run button or pressing Shift+F10 on the keyboard.

Step 11: After the project is built and the emulator has the finished setup, the below image shows the result of the build.

Screenshot-2025-01-16-122207

What is the use of "shared module"?

  • The shared module is used as a regular Android library and an iOS framework in the androidApp module and iosApp module respectively.
  • The shared module includes the Business logic (contains the core functionality of the app), Data models (contains the structure of app's data) and Utility functions (contains the common functionalities of the app).

KMP vs Other Popular Cross Platform Frameworks


KMP

Flutter

React Native

Programming language

Kotlin

Dart

JavaScript / TypeScript

Code Sharing

Can share business logic but UI is platform specific

Shares the entire codebase including UI

Can share most of the code but often requires native module for advanced features

Performance

Allows near-native performance by using native APIs

Uses it's own custom engine to provide high performance but is not fully native

Usage of JavaScript bridge results is slightly lower performance for complex apps

Integrated Development Environment (IDE)

Android Studio/IntelliJ Idea and Xcode

Android Studio, IntelliJ Idea and VS Code

VS Code

Community Support

New to the community therefore it's getting adapted gradually

Has a rapidly growing community with extensive documentations and libraries

Huge popularity of JavaScript has led to its huge community but may lack some libraries and packages

Ease of Migration

Better of android developers to switch because it is based on Kotlin

Better for new and amateur developers since it has a fast development life cycle and vast resources

Better for web developers to switch because it is based on JavaScript

Advantages of Kotlin Multiplatform

  1. Code Sharing: KMP allows developers to share common business logic and functionalities while maintaining native UI.
  2. Flexible Sharing: KMP allows users to choose how much they want to share the code. It might just be a snippet or an entire codebase.
  3. Native UI: KMP enables native-like performance by allowing developers to use platform-specific UI components.
  4. Easy Integration: Since KMP is based on Kotlin, this makes it easier for native android developers to migrate their code to work for other environments like iOS, web, and desktop.
  5. Access to Platform APIs: KMP provides total access to native APIs, whereas other frameworks generally do not.
  6. Evolving ecosystem: KMP is gaining more attention and JetBrains is improving it actively, that would lead to a better support in the future.

Disadvantages of Kotlin Multiplatform

  1. Less Popularity and Community Support: Unlike popular cross platform frameworks like Flutter and React-Native, KMP has lesser community support and libraries.
  2. More Learning: Developers are required to understand both Kotlin and other platform specific languages such as Swift/Objective-C for IOS.

Next Article
Article Tags :

Similar Reads