What is Kotlin Multiplatform?
Last Updated :
24 Jan, 2025
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- 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.
Step 2: Choose the option Kotlin Multiplatform App from the given list of templates and then click Next.
Step 3: Configure the initial details such as Project name, Package name, Save location and Minimum SDK and click Next.
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.
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.
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
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.
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
- Code Sharing: KMP allows developers to share common business logic and functionalities while maintaining native UI.
- 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.
- Native UI: KMP enables native-like performance by allowing developers to use platform-specific UI components.
- 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.
- Access to Platform APIs: KMP provides total access to native APIs, whereas other frameworks generally do not.
- 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
- Less Popularity and Community Support: Unlike popular cross platform frameworks like Flutter and React-Native, KMP has lesser community support and libraries.
- More Learning: Developers are required to understand both Kotlin and other platform specific languages such as Swift/Objective-C for IOS.
Similar Reads
Multiple Return Values in Kotlin
Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, App code, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
3 min read
Kotlin Elvis Operator(?:)
Elvis Operator (?:)It is used to return the not null value even the conditional expression is null. It is also used to check the null safety of values. In some cases, we can declare a variable which can hold a null reference. If a variable st which contains null reference, before using st in program
2 min read
Triple in Kotlin
In programming, we call functions to perform a particular task. The best thing about function is that we can call it any number of times and it returns some value after computation i.e. if we are having add() function then it always returns the sum of both the numbers entered. But, functions have so
4 min read
Multiconditional Loop in Kotlin
As we all know about loops, in Kotlin, loops are compiled down to optimized loops wherever possible. For example, if you iterate over a number range, the bytecode will be compiled down to a corresponding loop based on plain int values to avoid the overhead of object creation. Conditional loops are c
3 min read
Pair in Kotlin
In programming, we call functions to perform a particular task. The best thing about function is that we can call it any number of times and it return some value after computation i.e. if we are having add() function then it always returns the sum of both the numbers entered. But, functions has some
3 min read
Kotlin | Collection Transformation
Kotlin standard library provides different set of extension functions for collection transformations. These functions help in building new collections from existing collections based on the rules defined by the transformation. There are different number of transformation functions:  MappingZippingA
5 min read
Kotlin Null Safety
Kotlin has a powerful type system that helps developers avoid one of the most common problems in programming: Null Pointer Exceptions. In many languages like Java, trying to use a variable that holds null can cause a NullPointerException (NPE), which can crash your application if not handled properl
7 min read
Kotlin Tutorial
This Kotlin tutorial is designed for beginners as well as professional, which covers basic and advanced concepts of Kotlin programming language. In this Kotlin tutorial, you'll learn various important Kotlin topics, including data types, control flow, functions, object-oriented programming, collecti
4 min read
Polymorphism in Kotlin
The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. Â Real-life Illustration: Polymorphism A person at the same time can have different characteristics. Like a man at the same time is a father,
2 min read
Named Parameters in Kotlin
A parameter is a value that you can pass to a method. Then the method can use the parameter as though it were a local variable initialized with the value of the variable passed to it by the calling method. Function parameters are defined using Pascal notation - name: type. Parameters are separated u
3 min read