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
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
Kotlin Android Tutorial Kotlin is a cross-platform programming language that may be used as an alternative to Java for Android App Development. Kotlin is an easy language so that you can create powerful applications immediately. Kotlin is much simpler for beginners to try as compared to Java, and this Kotlin Android Tutori
6 min read
Retrofit with Kotlin Coroutine in Android Retrofit is a type-safe http client which is used to retrieve, update and delete the data from web services. Nowadays retrofit library is popular among the developers to use the API key. The Kotlin team defines coroutines as âlightweight threadsâ. They are sort of tasks that the actual threads can e
3 min read
Introduction to Kotlin Kotlin is a statically typed, general-purpose programming language developed by JetBrains, which has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 as a new language for the JVM. Kotlin is an object-oriented language, and a better lang
4 min read
Kotlin Data Types The most fundamental data type in Kotlin is the Primitive data type and all others are reference types like array and string. Java needs to use wrappers (java.lang.Integer) for primitive data types to behave like objects but Kotlin already has all data types as objects.There are different data types
3 min read
Kotlin when expression In Kotlin, when replaces the switch operator of other languages like Java. A certain block of code needs to be executed when some condition is fulfilled. The argument of when expression compares with all the branches one by one until some match is found. After the first match is found, it reaches to
6 min read
Kotlin Constructor A constructor is a special member function that is automatically called when an object of a class is created. Its main purpose is to initialize properties or perform setup operations. In Kotlin, constructors are concise, expressive, and provide significant flexibility with features like default para
6 min read
Android RecyclerView in Kotlin In this article, you will know how to implement RecyclerView in Android using Kotlin . Before moving further let us know about RecyclerView. A RecyclerView is an advanced version of ListView with improved performance. When you have a long list of items to show you can use RecyclerView. It has the ab
4 min read
ProgressBar in Android Progress Bar are used as loading indicators in android applications. These are generally used when the application is loading the data from the server or database. There are different types of progress bars used within the android application as loading indicators. In this article, we will take a lo
3 min read
Kotlin Array An Array is one of the most fundamental data structures in practically all programming languages. The idea behind an array is to store multiple items of the same data type, such as an integer or string, under a single variable name. Arrays are used to organize data in programming so that a related s
6 min read