KMM: What's Under the hood?

Codeangi
YML Innovation Lab
Published in
6 min readFeb 21, 2023
source

There have been many tools for cross-platform development before but offered so little in terms of sharable code, there was always an overhead of writing and maintaining applications in a set-predetermined technology such as Phonegap, ReactNative, and Flutter.

Kotlin provides a huge leeway in writing sharable code, especially when you are developing Mobile applications for Android and iOS with similar features. In the real world, it translates into fewer lines of code, fewer bugs, less time, and of course lesser cost of development. Whether you are starting a new project or adding a new feature to an existing one.

This article is an effort to interpret KMM in layman’s terms, knowing programming helps but is not mandatory.

Topics Include: What is KMM, How it works, the Good, and the Bad.

source

Kotlin is a cutting-edge yet established programming language that offers a range of benefits for developers. One of the key use cases for Kotlin is its ability to share code between mobile platforms through Kotlin Multiplatform Mobile (KMM). By using KMM, developers can create cross-platform mobile apps and share crucial elements like business logic, data, and connectivity implementation between Android and iOS.

Kotlin-Native

In order to use Kotlin code in iOS, the Kotlin code is first compiled to LLVM bytecode using the Kotlin/Native compiler. LLVM (Low Level Virtual Machine) is a collection of modular and reusable compiler and toolchain technologies.

The LLVM bytecode is then compiled using the iOS toolchain and linked with the iOS runtime libraries.

The resulting binary is an iOS application bundle (an .app directory) that contains the Kotlin code and the Swift runtime. When the app is run, the Swift runtime loads the Kotlin code and uses it as if it were native Swift code.

Kotlin/JS

Kotlin/JS provides the ability to trans-compile Kotlin code, the Kotlin standard library, and any compatible dependencies to JavaScript. The current implementation of Kotlin/JS targets ES5. The Kotlin/JS compiler converts the Kotlin code into JavaScript code that can be executed by the JavaScript engine in the browser.

The process of compilation starts with the Kotlin code, which is then transformed into an intermediate representation (IR) by the Kotlin compiler. The IR code is then passed through a set of optimization passes that improve the performance of the generated JavaScript code. After that, the IR is translated into JavaScript code, which can be run in a web browser.

Kotlin/JVM

Kotlin is 100% interoperable with the Java programming language and provides easy access to existing java-code/libraries. Popularly used as a java replacement, Android has officially announced its Kotlin-first policy, recognizing its importance.

How KMM is used?

Android and iOS applications

With Kotlin Multiplatform Mobile, you can build cross-platform mobile applications and share common code between Android and iOS, such as business logic, connectivity, and more.

Multiplatform libraries

Kotlin Multiplatform is also useful for library authors. You can create a multiplatform library with common code and its platform-specific implementations for JVM, JS, and Native. Once published, a multiplatform library can be used in other cross-platform projects as a dependency.

Check out some of them already listed here:

Common code for mobile and web applications

One more popular case for using Kotlin Multiplatform is sharing the same code across Android, iOS, and web apps. It reduces the amount of business logic coded by front-end developers and helps implement products more efficiently, decreasing the coding and testing efforts.

What to share?

Developers get to choose what to share in KMM, so we can identify parts of the application that are common, such as Data (Network-response & or Local DB), Business Logic which is usually the same across platforms, these are the ideal candidate for shared modules. KMM also provides the experimental ability to share ViewModels and Views(in the works). So developers get the flexibility to choose how much code is shared, and customize how it is integrated on target platforms, this helps to keep the project concise and maintainable on the scale.

Swift InterOp

When Kotlin code is compiled, into Kotlin/native library, some nuances which may differ implementation-wise w.r.t Objective-C/Swift, are mapped to an equivalent class with similar behavior.

they can be found in Kotlin's official documentation

https://kotlinlang.org/docs/native-objc-interop.html#mappings

Where is Kotlin Multiplatform Mobile now?

First Released in August 2020, KMM has been on an accelerated path of growth, since it’s release there have been 19 major releases/updates currently at the time of this article the stable release is 0.5.1 which offers many features and tools for seamless integration across Android and iOS devices.

Why choose KMM over another cross-platform tech?

  • Cross-platform techs like ReactNative (JS), flutter, Ionic, and PhoneGap comes with a technical overhead, they require the whole application code including the UI/UX to be written in the same environment/language, which requires a dedicated development effort and developer dependency.
  • Suitable for Test-Driven Development.
  • Cross-platform tech is always prone to inconsistencies in terms of UI/UX and performance compared to Platform-Native applications.
  • Kotlin Multiplatform enables developers to partly or optionally share the code, which can collectively reduce development effort by up to 50% when used in a multi-platform setup, without compromising the performance and user experience.
  • KMM libraries can be used to modularize and maintain precise and concise code, easy to share and integrate into existing applications.
  • Suitable for plug-and-play modules with little dependency on UI.

What are the drawbacks of KMM?

Though some of these are not just unique to KMM, should be acknowledged nonetheless.

  • The major drawback is you still need Mac OS to compile and run iPhone/iPad applications.
  • No Code-Documentation. If you are a native developer you must be used to accessing handy documentation in AS/XCode, but when you are using KMM library in Xcode, you might be deprived of that luxury for now.
  • Though there is no noticeable run-time latency, there is a slight impact on compile time in iOS when developing KMM App, in XCode. (I’d give this a break though, switching IDEs is not smooth, so it is expected to have some compile-time issues.)
  • Custom classes implementing standard Kotlin collection interfaces (List, Map, Set) and other special classes are not supported in objective-c/swift.
  • Swift-class Mapping, Kotlin data classes are not supported in swift, so is the case for some of the other Kotlin-nuances(data classes, interfaces, sealed classes, nil/null etcetera..). Such features are mapped to equivalent swift implementations. this might take some time to get used to guessing the correct name/notation for such expressions.

At YML we have been using #KMM and it has received a positive response from the developers. It is easy to adapt to existing projects, and with rapid growth, KMM has a very bright future in Allied-Native-Development. A New Kind of development where Android and iOS native developers can collaborate more and learn a thing or two about the platforms of the counterpart and Share the code. Still, there is more exciting stuff coming our way from the KMM.

Thank you Enrique López-Mañas and Duc Haba for your valuable insights on the article.

If you found it helpful, these are the related articles:
1. Getting the settings right: setting up for KMM
2. Sharing Code: with KMM
3. Networking in KMM

--

--