Author: Pixel2013

A first glance to Kotlin Multiplatform Mobile — KMM

Javier Martínez
4 min readMar 7, 2021

--

KMM stands for Kotlin Multiplatform Mobile, which is a technology built on top of Kotlin Multiplatform, created by JetBrains, that allows us to create code compilable to multiple platforms.

To be more precise, with KMM we can create a common library that, written only in Kotlin, is totally compatible with iOS and Android.

Components diagram of how Kotlin Multiplatform works
Source: official doc

At this point you will be wondering that, even though all of this sounds quite promising, there are specific situations where you need platform specific code. For these scenarios, Kotlin guys have created an “expected -> actual” paradigm that let us writing some code pointing to the native platforms.

The real benefit of this technology is that the app’s views and layouts are totally implemented with the native Framework, i.e. the tradicional .xml/.xib or the newer Jetpack Compose/SwiftUI, so the performance is not compromised at all.

Kotlin Multiplaform Mobile module structure diagram
KMM Structure

If you want to dive into how KMM works underneath, I recommend you this interesting and brief article.

After this quick introduction and, given that this article does not pretend to explain the benefits and drawbacks of this technology nor describing any other technical details, I will directly jump to the case study.

Case study

Since I wanted to create a simple case study to give an easy to understand example, I thought a typical use case I recently faced at my company.

At the present time, in our Android project, we have multiple form validation methods spread around the code, which logic is totally duplicated in the iOS app. Even though this is the traditional approach in the mobile world, it is obviously extremely error prone and hard to maintain. These form validation includes username, password, email, phone number, bank account, etc. And believe me, they are not easy validation, being each of them composed of tens of lines of code.

All in all, I decided to create a tiny validation library that will serve me as proof of concept. You can find the source code in my GitHub account.

Project structure

First of all, let me say I created this project directly from Android Studio, taking advantage of the wizard that Android Studio KMM plugin offers up. I encourage you to read the official docs in order to kick start the setup needed, which I promise you it is easy.

Android Studio “New Project” wizard

The wizard will create for you the following structure:

  • androidApp: traditional Android application.
  • iosApp: traditional iOS application.
  • shared: this is the key part here. It will include all the shared Kotlin Code. This module is locally linked to the Android project through a basic Gradle implementation(…), whilst it is locally linked to the iOS project through a framework. The iOS side requires you to spend a little more time on setting up (already included if you created the project through the Android Studio Wizard).

So let’s dive in the shared module, which is the most interesting one, to finish by going through how Android and iOS applications actually implement the logic.

shared

As I mentioned in the introduction, KMM allows you to write specific platform code, that is why you can see three main folders, androidMain, iosMain, and commonMain. This last module is the one where dreams come true.

For the proof of concept I decided to create a short platform dependent logic, just retrieving the operating system version, as you can see below.

Regarding the common validation code, it is a very simple validation based on easy regular expressions. By the way, I invite you to have a look at the tests for this class. Keep in mind that in a production app these validations are usually much more complex.

androidApp

The application interface is fully built with a Constraint layout, where the Activity is in charge to perform the form validations, relying on the Validator.kt class from the shared module.

iosApp

Meanwhile, in the iOS App the interface has been built with SwiftUI. Again, the validation logic relies on the very same Validator.kt class, from the shared module. Wow.

Conclusions

Even though KMM is not in a fully stable state, it has proven to be a stable enough technology for sharing code between platforms. Proof of it are the case studies in several production applications.

From my point of view, KMM is a great way to share those little “utils” code that usually is duplicated for both Android and iOS and I feel it could be a good choice for creating a complete domain and data layers if we assume the drawbacks and the lack of KMM libraries for this purpose.

I hope you enjoyed the reading. Have a lovely day.

--

--