Kotlin Multiplatform for Cross-Platform Mobile Development

by

Mobile Cross-platform app development has become an important alternative to traditional native development. With cross-platform app development, developers can design an app once, and then launch it on both Android and iOS simultaneously. It has become especially popular among startups, as it represents a rapid, cost-effective way to reach a wide range of users. 

One of the newest cross-platform frameworks to hit the market is Kotlin Multiplatform. As you can see in the table below from Statista, Kotlin Multiplatform is not among the largest market shareholders. However, while it is still new to the market, I believe it contains promising features that may attract more developers in the upcoming year. 

Introduction to Kotlin

Koltin is a development framework that was introduced by JetBrains in 2011. It was named after Kotlin Island, an island located near St. Petersburg, as a nod to Java’s Java Island namesake. Kotlin was officially released in 2016. In 2017, Google announced first-class support for Kotlin on Android. In 2019, Google announced Kotlin as the preferred language for Android app developers over Java. 

Kotlin is a statistically typed language that fully interoperates with Java. It supports both procedural and object-oriented programming. Kotlin mainly targets Java virtual machine (JVM), but also compiles to JavaScript (e.g., for frontend web applications using React) or native code via LLVM (e.g., for native iOS apps).

While Kotlin is primarily a programming language, it does have a different side: Kotlin Multiplatform. Kotlin Multiplatform is primarily designed to allow compilation for platforms where virtual machines are not desirable or possible, such as embedded devices or iOS. 

The target platforms for Kotlin Multiplatform include:

  • macOS
  • iOS, tvOS, watchOS
  • Linux
  • Windows (MinGW)
  • Android NDK (code written in C/C++)

Kotlin Multiplatform Mobile (KMM)

KMM is a software development kit (SDK) for iOS and Android app development that offers all the combined benefits of creating cross-platform and native apps with code shared between Android and iOS. The alpha was released in 2020, so many companies are waiting to make the switch until it is more established and reliable. However, there are production apps that are running using KMM, including Philips, Netflix, Leroy Merlin, and VMWare. 

Why KMM?

The benefits of KMM include:

  • Building the Android and iOS applications simultaneously
  • DRY (Do not Repeat Yourself) principle. Code sharing between iOS and Android keeps you from repeating your work. 
  • KISS (Keep it simple, stupid) principle. Building within the KMM shared library keeps your codebase efficient and organized. 
  • Allows software engineers to deliver features quickly but with quality. 

In KMM, you can use a single, shared code to write:

  • Data Models/ Serialization from API JSON
  • API Authentication and Calls
  • Analytics Tags
  • Monitoring of exceptions and problems
  • Caching
  • Accessibility Voiceover versions of text from the API calls
  • Dev/Feature Switch Management
  • Any Business rules that involved manipulating the data before it went to the client
  • Data mocks for testing on iOS and Android 

With KMM projects, you typically have a shared module, an iOS application, and an Android application. The shared module contains all the common logic for both the Android and iOS applications. When you compile the scripts, it builds into an Android library and an iOS framework. KMM contains a common code that implements your business logic, and then the shared module specifies what is used to invoke any specific native APIs in your application. For example, if you have a unique identifier (UID) in the common code, you can designate a call to a specific operating system using either of the two libraries. 

As you can see in the visual below, KMM is most beneficial for business logic and the data layer: 

The business logic and data layer of your app can be completely shared between Android and iOS with KMM. Since KMM was released, there have also been additional efforts to increase sharing in the presentation logic and UI logic. 

Based on the results of the Kotlin Multiplatform Survey in Q1-Q2 2021, we can see that the majority of networking and data storage utilized KMM: 

Final Thoughts

This is just a brief summary of KMM and doesn’t delve into the technical aspects of the framework. Like any framework, KMM comes with its own combination of benefits and challenges. Overall, I feel that the benefits to KMM, such as the shared and reusable code library and the development efficiency, will allow the framework to continue gaining traction and popularity among software developers. 

You can learn more about Kotlin in Meet Kotlin and Concurrency on Android wth Kotlin Coroutines.

Leave a Reply

Your email address will not be published. Required fields are marked