Kotlin Multiplatform and KMM
Kotlin Multiplatform is one of several approaches to build cross-platform features, or apps, by sharing common code with the Kotlin language. We discuss this approach separately from other cross-platform feature development and app development approaches, thanks to its rapid rise in adoption among large apps and developer teams.
Kotlin Multiplatform was announced in 2017. With it, you can write Kotlin and build:
- JVM libraries for Android, or backend services
- Native framework for iOS and desktop
- JavaScript artifacts for frontend web or backend services
Kotlin Multiplatform The idea behind Kotlin Multiplatform: share business logic code, keep view code as native. Top: the generic idea. Bottom: how Netflix implemented KMM in their Prodicle TV production app
Kotlin Multiplatform Mobile (KMM) was released in 2020, and is a tooling layer on top of Kotlin Multiplatform. KMM gives engineers tools for easier IOS and Android development. These tools include:
- Rich IDE integration
- Better debugging capabilities for mobile apps
- Good support for Cocoapods
Touchlab are the global experts in Kotlin Multiplatform Mobile (KMM). Touchlab accelerates KMM adoption through product & SDK development, early adopter support, architectural & production-readiness reviews and open-source projects.
Touchlab advises enterprises like Square and NBC on scaling KMM and partners with JetBrains to increase KMM adoption. Looking to get started with KMM? Check out their Kotlin Multiplatform starter kit.
Several larger companies have shipped Kotlin Multiplatform projects in production: Netflix, Square, Careem and VMWare being a few of them. There are several benefits of going with this approach:
- Using Kotlin is a natural choice for many mobile engineers. Android engineers likely already use it, and it is easy to learn if you already know Swift. It is also beneficial for an iOS engineer to broaden their skills to include Android.
- KMM encourages fully native UI components. Unlike the approaches taken by tools such as Flutter and React Native, KMM is designed to allow native mobile developers to build UIs in ways that users and developers are used to.
- Investment from JetBrains and Google in the Kotlin Foundation owning the project. This also means first-class support for Kotlin and KMM in JetBrains IDEs and Android Studio.
The case study from Square, which moved Cash App over to Kotlin Multiplatform, highlights reasons why several teams might go with this approach. The Cash App team wanted to keep most of their code as native, and choosing Kotlin as the language for shared components made sense. They started off by collaborating with Touchlab (a sponsor of this book) in testing the technology. They noted how because the platform-agonistic Kotlin resembles Swift, the shift was manageable for iOS engineers. What is more, they had contributions from the server team as well.
Downsides of adopting KMM all come from the technology being less mature. KMM tools are experimental as of 2021, subject to breaking changes that can impact early adopters. Larger teams will find painful tooling gaps. The VMWare team noted that the lack of supported libraries also presented a challenge they eventually overcame.
Among all the cross-platform feature development approaches, I see Kotlin Multiplatform and KMM as the most promising. This is the only cross-platform approach where existing native Android engineers feel right at home, and iOS engineers will also have the least steep learning curve in adopting Kotlin and the tooling.
Top-Of-Mind Concerns About KMM #
This part is sponsored by Touchlab, the global experts in Kotlin Multiplatform Mobile (KMM).
Touchlab Partner Kevin Galligan shares his thoughts on top-of-mind concerns teams have about KMM.
1. Stability. Will there be breaking changes forcing rework? Will it go away or continue to be improved and maintained?
Developer experience is critical for adoption of a shared code solution at scale. JetBrains has a track record making some of the world’s most popular developer tools in a wide range of ecosystems. We share JetBrains’ commitment to the technology: “After the release of Kotlin 1.4, KMM is in Alpha status. This means the Kotlin team is fully committed to working to improve and evolve this technology and will not suddenly drop it.”
As we continue to help our clients put KMM into production, we’ll be keeping close watch on KMM’s path to Beta.
2. Dependency Management. What about modularization, creating and consuming libraries in various configurations?
Kotlin is native to Android and the preferred language for Android development. Modularization on the Android side follows similar patterns and uses the same tools as ‘standard’ Android. The Android tooling team is aware of KMP and works along with Jetbrains to ensure compatibility.
On the iOS side, Kotlin is consumed as an Xcode framework and multiple Kotlin modules can be wrapped into one framework. Presently, different Xcode frameworks need to be constructed for different combinations of Kotlin modules. If you have multiple app instances with different combinations of Kotlin modules, you should build and deploy a wrapper Xcode framework for each, rather than publishing an Xcode framework from each Kotlin module. The ability to include multiple Xcode frameworks directly rather than constructing a wrapper framework will continue to be an ongoing discussion with more options in the future.
On the consuming side, teams need to decide if they want to build Kotlin locally or distribute precompiled binaries, which avoids installing and configuring Kotlin tooling and the build time overhead involved. Multiple configuration options are available.
3. iOS DX. How will the iOS Developer Experience (DX) be impacted?
KMM is designed to interop directly with platform-native technologies, rather than replacing them, which is why engineers we work with at clients like Square and NBC choose it. Kotlin is exported as an Xcode framework, and Swift/Objc code calls directly into it rather than needing to communicate over some kind of inter-process layer.
We collaborate with JetBrains and others to deliver direct Xcode integration for Kotlin browsing and debugging. The goal is to provide ‘native code sharing’ vs ‘cross platform’. Currently, interop is through an Objective-C interface, although direct Swift interop is planned for future releases.
iOS resources:
- CrashKiOS: crash reporting for Kotlin/Native iOS applications
- Kotlin Native Xcode Plugin: facilitating debugging iOS applications using Kotlin Native in Xcode
To learn how Touchlab can help scale KMM at your organization, please contact them.
You are reading an early draft from the book Building Mobile Apps at Scale. For the final, expanded and improved content, grab the book now - it's free to download as a PDF until 31 May.
Building Mobile Apps at Scale
"An essential read for anyone working with mobile apps. Not just for mobile engineers - but also on the backend or web teams. The book is full of insights coming from someone who has done engineering at scale."
- Ruj Sabya, formerly Sr Engineering Manager @ Flipkart