What is KMP - Kotlin Multiplatform for Unified Android, iOS, and Web Development
Kotlin Multiplatform is a feature of the Kotlin language that allows you to write code that can be shared across multiple platforms, including Android, iOS, web, desktop, and backend.
Here are some key aspects of KMP:
![]() |
Shared Code: Write common business logic, data models, and utility functions once in Kotlin and reuse them across different platforms.
Platform-Specific Code: Use platform-specific APIs and libraries when needed by writing platform-specific implementations for certain parts of your code.
Improved Code Reusability: Reduce code duplication and maintenance efforts by sharing a significant portion of your codebase.
Flexibility: Choose the platforms you want to target and gradually adopt KMP for different parts of your application
To start using Kotlin Multiplatform in your Android project, you can follow these steps:
If you are using the Android studio Ladybug Canary 4 and above you have other options like creating a KMP module under the module section.
Install the Kotlin Multiplatform Mobile plugin:
Launch studio and go to Settings/Preferences > Plugins and search for "Kotlin Multiplatform Mobile". Choose Kotlin Multiplatform developed by Jetbrains and Install the plugin and restart Android Studio.
Create a new KMP project: While creating a new project, choose the "Kotlin Multiplatform App" template. This will set up a basic project structure with modules for Android, iOS, and common code
KMP project structure:
A KMP project has a common module where you write platform-agnostic code, and platform-specific modules (like androidApp and iosApp) where you write platform-specific code.
Project Structure and Functionality:
- androidApp: This directory is specifically for Android development. Here, you'll place files related to the Presentation Layer and user interface (UI) components.
- iosApp: This directory is dedicated to iOS development. It houses files for the Presentation Layer, UI, and Swift-specific code. Use Xcode to build and run iOS applications from this directory.
- shared: This directory is used to store shared logic that can be used across both Android and iOS platforms.
- androidMain: This directory contains settings and configurations necessary for running iOS libraries within the Android environment.
- commonMain: This is the core of the shared logic. It's where you'll create data packages (like remote, repository, and util) and implement Dependency Injection. You'll also structure your domain layer with model, repository, and usecase packages. This directory adheres to the clean architecture principles, excluding the Presentation Layer.
- iosMain: Similar to androidMain, this directory holds settings for running Android/Kotlin libraries in iOS. It might involve enabling the use of coroutines or Koin Library, which might not be directly supported by default.
- shared-build.gradle: This Gradle file lists all the libraries that are used across Android, iOS, and the common codebase.
- project-build.gradle: This Gradle file specifies the plugins required for the project, such as those for Android applications, Android libraries, Android itself, and multiplatform development.
In essence, this structure promotes code reusability and maintainability by separating platform-specific code from shared logic
Write common code:
In the common module, you can write common business logic, data models, and utility functions once in Kotlin and reuse them across different platforms
Write platform-specific code: In the platform-specific modules, you can access platform-specific APIs and libraries. You can also provide platform-specific implementations for the expected declarations defined in the common module.
Build and run your app: You can build and run your app on different platforms using the appropriate build configurations.
KMP Implementation Principle
KMP is based on the Kotlin K2 compiler and adopts a multi-stage compilation architecture. Its core consists of two parts: the compilation front-end and the compilation back-end:
-
Compile the front end:
-
Syntax parsing: Parse Kotlin source code into an abstract syntax tree (AST).
-
Semantic analysis: Perform semantic checks on AST to ensure that the code complies with the Kotlin language specification and perform type inference and checking.
-
Intermediate Representation (IR) Generation: Convert AST to an intermediate representation for further processing by different compilation backends.
-
Compile backend:
-
JVM backend: Converts the intermediate representation into JVM bytecode to run on JVM platforms (such as Android).
-
Native backend: Converts the intermediate representation to LLVM bitcode, and then generates native binary code suitable for different platforms (such as iOS, Linux, Windows, etc.) through the LLVM tool chain.
-
JavaScript backend: Converts the intermediate representation into JavaScript code to run in a browser or Node.js environment.
-
WebAssembly backend: Converts the intermediate representation into WebAssembly code to run in a browser or other WebAssembly-enabled environment.
KMP organizes cross-platform code through multiplatform modules. Multiplatform modules consist of the following three parts:
-
Common Module:
Contains shared code and API definitions across platforms.
Use the expect keyword to declare platform-specific API interfaces.
-
Platform-specific Module:
-
Implement platform-specific code and APIs.
-
Use the actual keyword to implement the interface declared by expect.
-
Intermediate Module (optional):
-
Provides an intermediate layer, which contains some cross-platform code and some platform-specific code, to simplify the implementation of platform-specific modules.
The expect/actual mechanism is the key to KMP's implementation of platform-specific code. It allows platform-specific interfaces to be declared in the common module and specific implementations to be provided in individual platform-specific modules.
-
expect: Declare interfaces or classes that need to be implemented on different platforms in the common module
expect val Platform: String |
-
actual: Provide concrete implementation in platform-specific modules.
// androidMain actual val Platform: String get() = "Android" // iosMain actual val Platform: String get() = "iOS" |
KMP supports writing cross-platform common code in the common module and reusing this code in the platform-specific module. Through the expect/actual mechanism, developers can define interfaces and common logic in the common module and implement platform-specific functions in the platform-specific module
Why choose KMP?
Cross-platform development has always had the following problems:
-
Platform differences: There are huge differences in the development methods and engineering architectures of interfaces and UI interfaces on different platforms. Developers need to deal with these differences to ensure that the code runs normally on different platforms.
-
Learning and integration difficulty: Developers need to learn and understand different development environments and related tool chains. The learning curve is relatively steep, and it is difficult to get started quickly.
The two most widely used cross-platform solutions in the industry are React Native and Flutter. Although they are very different in design and principle, in order to reduce the problems caused by platform differences, they are designed to build their own Runtime environment on the platform framework and use non-native development languages ??for development. When interacting with native development languages, a special bridge layer needs to be written to achieve it. When the dependent platform framework changes, the module architectures of both sides need to coordinate and handle it together.
KMP is different from this design idea. It does not create its own operating environment, but compiles Kotlin code into platform-specific language code or machine code so that the platform can execute it directly. From this perspective, React Native and Flutter are more suitable for cross-platform development including the UI layer, while KMP is more suitable for cross-platform development of the pure logic layer.
In the upcoming tutorials, I'll be demonstrating more mobile app sample code developed using the KMP (Kotlin Multiplatform) framework for cross-platform development