ユニファ開発者ブログ

ユニファ株式会社プロダクトデベロップメント本部メンバーによるブログです。

Introduction to Kotlin Multiplatform Mobile (KMM)

Hello, I'm Ahsan Ullah Rasel, an Android Engineer from the Android team of Unifa Inc. Japan. Today, I am going to briefly introduce the Kotlin Multiplatform Mobile (hereinafter referred as KMM) framework to you.
With KMM you can create applications for both Android and iOS using all modern Kotlin APIs. It reduces the development time while giving freedom of creating UIs with platform native code as well as using platform specific APIs.
I will try to briefly discuss what this platform is, what benefits it offers, what are the drawbacks of this platform etc. So, let's begin.

What is Kotlin Multiplatform Mobile (KMM)?

Before knowing about KMM, let first know about Kotlin Multiplatform (KMP). This is a cross-platform framework that offers sharing codes between mobile apps, web apps, desktop apps, multiplatform libraries etc. Using KMP, you can share common business logics among platforms and then write platform specific codes with their own style.

Kotlin Multiplatform Mobile diagram
Figure: Kotlin Multiplatform Mobile (Source: https://kotlinlang.org/lp/mobile )

Kotlin Mulptiplatform Mobile, a subset of Kotlin Multiplatform, is dedicated to mobile applications. We can share business logic between Android and iOS apps while keeping their UXs native. The above figure gives an image of what the KMM framework is.

What benefits does it offer?

KMM offers to build native like applications using the power of the modern Kotlin language. With the power of KMM, you can share business logics, connectivity, storage logics, algorithms, utilities etc. between Android and iOS. With its powerful expect/actual mechanism, you can define platform specific versions of implementation of certain classes to access platform APIs. It also gives you the freedom of using Native UI frameworks..

The key benefits of KMM is:

  1. Common business logics can be shared fully as a module
  2. Great app performance by keeping the advantages of native programming and full access to the Android and iOS SDKs
  3. Offers great maintainability as modification is easy and fast
  4. Super easy to integrate KMM into existing Application
  5. Can use the latest technology of platforms like Jetpack Compose on Android side and SwiftUI on iOS side
  6. Improve the quality of your code while reducing the development time
  7. Low risk in adoption, as Kotlin shared codes can be moved anytime to Android library modules and can be translated to Swift and thus both app's code will become independent
  8. No extra engine needed to run code as code becomes a module in Android side and becomes Kotlin/Native Library framework on iOS side like other Swift/Objective-C libraries

Output diagram of KMM
Figure: Output diagram of KMM (source: https://kotlinlang.org/docs/multiplatform-mobile-faq.html#what-is-kotlin-native-and-how-does-it-relate-to-kotlin-multiplatform-mobile )

What are the drawbacks?

Though KMM offers great benefits in developing cross platform applications, it has some drawbacks compared to other cross-platform frameworks.

Some of the drawbacks are:

  1. As UIs have to be developed natively and separately for both Android and iOS, development time will be little higher than other cross platform frameworks
  2. The platform is still in Alpha, so any breakdown of code can occur and language features & tooling may change with time
  3. Little tough for iOS developers as they have to adopt Kotlin and Gradle, also debugging on iOS is a little bit tricky
  4. Less number of readymade libraries and community support as this platform is new and upgrading day by day

Considering Pros and Cons of the Kotlin Multiplatform Mobile aka KMM platform, you might think that, in what kind of situation we should use KMM?
KMM is suitable for applications that are logic dependents. For example, if you are developing applications with complex computations, algorithms or if your application has features like data synchronization, networking, data storage and payments etc. then you can use the real power of KMM technology by creating and sharing these common logics.
But if your application is UI dependent and you have very little business logic, then you might end up not using the true power of KMM technology.

Kotlin Multiplatform Mobile allows you to use a single codebase for the business logic of iOS and Android apps. So if you develop a very thin client and the majority of the code is UI logic, then the main power of Kotlin Multiplatform Mobile will be unused in your project.


Demonstration

Let us now see some demonstrations of KMM. For demo purposes, I have created a project with KMM which has two screens, one is List and another is Details screen. List screen shows some basic info of Launches in List manner with the help of SpaceX API and Details screen shows the details of certain Launch with its ship's image as well as links of Article, video and wiki page describing about that Launch.

The technologies, being used in this demo project, are:

  • Kotlinx-datetime, a Date Time formatter library that works for both Android and iOS
  • Ktor HTTP client framework for network calls
  • Koin dependency injection library
  • SQLDelight for data caching with local DB
  • Kotlinx Serialization for parsing API data models
  • Jetpack Compose for creating Android UIs
  • SwiftUI for creating iOS UIs

This project is based on the MVVM architectural patterns. Let's see how the actual app looks on both Android and iOS device

List screen and details screen of iOS AppList screen and details screen of Android App
Figure: List screen and details screen of the apps on both Android and iOS device

As you can see in the above figure, both apps have the same data with different (own) designs. Their business logic is the same but the UI is created differently. And the main point is, there are no performance issues on both apps like other cross-platform!

So, if you are planning to develop business logic dominating cross platform apps without sacrificing your app performance, then KMM might be a great option for you.

You can explore more about Kotlin Multiplatform Mobile (KMM) from here. Let's give it a try!


If you have potential, and want to work in a dynamic environment with the latest technologies, let's work together at Unifa!

unifa-e.com