[Part 1] All about Kotlin Multiplatform

Jyoti Sheoran
3 min readMay 23, 2024

--

Kotlin Multiplatform (KMP) allows developers to share code across multiple platforms, including Android, iOS, web, and desktop. By using KMP, you can streamline your development process, reduce code duplication, and maintain platform-specific functionalities.

This is part 1 of a series where we will discuss integrating existing Android and iOS apps with KMP. In this part, we cover the basics of KMP.

What is Kotlin Multiplatform (KMP)?

KMP is a feature of Kotlin that allows you to write code that runs on multiple platforms. It enables code sharing between common platforms like Android, iOS, web, and desktop while still allowing you to write platform-specific code when necessary.

Why Use KMP?

KMP offers benefits such as code reusability, consistency, and efficiency, similar to other cross-platform technologies like React Native and Flutter. Additionally, KMP provides native UI and performance, which those technologies may lack.

How Does Kotlin Multiplatform Work?

KMP separates your code into shared and platform-specific modules:

  • Common Module(shared): Contains code that is shared across all platforms.
  • Platform-specific Modules(ios, app — for Android): Contain code that is specific to each platform (Android, iOS, etc.).
KMP in Action

The shared module can call platform-specific code through the use of expect/actual declarations. An expect declaration in the shared module is matched with an actual declaration in the platform-specific modules.

KMP Use Cases:

  1. Share a piece of logic: Improve app stability by sharing isolated, critical parts of the app. Reuse existing native code to keep applications in sync. This use case will be discussed in this series.
  2. Share logic and keep the UI native: Start a new project with KMP, implement data handling and business logic once, and keep the UI native to meet strict requirements. Here’s a KMP Project if you want to learn more about this use case.
  3. Share up to 100% of the code: Increase development efficiency by sharing up to 100% of your code with Compose Multiplatform, a framework by JetBrains for sharing UI across platforms. Here’s a Compose MultiPlatform Project, if you want to know more about it.

KMP Project Architecture:

A typical KMP project looks like this:

  • View (Presentation Layer): Written in iOS and app (Android) modules.
  • Domain and Data Layer: In the shared module.

This structure can vary:

  • For use case 1, data and domain layers may also reside in native modules.
  • For use case 3, the presentation layer might be in the shared module.

That’s it for this part. In Part 2, we will discuss how to integrate KMP into an Android project.

Stay tuned for the next part of this series, where we will explore integrating KMP into an existing Android project.

--

--