Kotlin Multiplatform Mobile: Simplifying cross-platform app development
Looking for greater compatibility, flexibility, efficiency, and ease of adoption for cross-platform mobile app development? Read on for an engineer’s take on Kotlin Multiplatform Mobile (KMM) and how it works.
Insight in brief
Kotlin Multiplatform Mobile (KMM) is a new SDK that combines the best of both worlds, allowing app developers to reuse codebase just like a cross-platform framework while still keeping the app native.
Tackling the challenge of maintaining separate codebases while retaining flexibility, KMM is used by diverse companies from start-ups to large IT corporates and big product companies.
While still in the alpha phase, there is good potential for KMM to become the next go-to tool for cross-platform projects, aside from Flutter and React Native.
As smartphone users around the world continues to grow, cross-platform applications are gaining popularity as it allows wider outreach to potential Android and iOS users while providing high-efficiency outputs and significant cost reduction.
By the end of 2021, 40% of the global population will own a smartphone. These numbers will only continue to grow as the global smartphone users surpass 6 billion and are expected to grow by several hundreds of millions in the next few years.
Android smartphones currently lead the global market with 72% market share while iOS follows closely at 27%. In mobile development, building cross-platform applications enables wider market reach to both Android and iOS users while providing high-efficient output and significant cost reduction.
Current state of mobile app solutions
Mobile app development is the process of building specific software applications that runs on smartphones. It involves a strategic approach from researching to prototyping, building to testing, before finally launching on the app store.
Android and iOS – these two platforms always come to mind when talking about mobile development. It is very rare to see a mobile application that is only available on one platform and not the other.
Commonly, there are two options to build mobile applications for these two platforms:
- Write fully separate platform code, once for Android and once for iOS
- Write cross-platform code for both Android and iOS
Projects that need to fulfil certain performance, security, and native device requirements and have enough resources will most likely use a fully separate platform code for either. However, this means that two separate codebases will need to be maintained, potentially by two separate teams of developers with different skillsets.
Introducing Kotlin Multiplatform Mobile (KMM) and how it works
In 2020, JetBrains announced the Kotlin Multiplatform Mobile (KMM) as a new SDK that allows developers to write shared code for business logic and still maintain a platform specific UI code. It combines the best of both worlds, being able to reuse a huge amount of codebase just like a cross-platform framework but keeping the app still native.
How is Kotlin Multiplatform Mobile being used?
KMM is all about pragmatic programming to meet the growing demand across platforms. Furthermore, developers who are already familiar with Kotlin will find it easy to start using KMM to simplify their development projects.
PlanGrid by Autodesk is a construction productivity app that allows project collaborations and is used on over 1.8 million projects in more than 100 countries globally. It uses KMM to ship a single source of truth for offline sync logic and data models for 3 mobile platforms: iOS, Android and Windows.
The team at Cash App, a mobile payment service developed by Square, Inc, also had massive success in piloting KMM. Not only was the transition manageable for the different teams, developers had option to share codes in the repository without disrupting their workflow.
The Shared Module
The androidApp and iOSApp modules are straightforward. These are the modules where the platform-specific application code is written, the same as you would write for a native Android or iOS application. The shared module, however, is “shared” between platforms and the Kotlin multiplatform code will be compiled into the specific binaries as illustrated above.
A typical Hello World application for a KMM project will display a greeting string with the platform OS version used in the app, such as “Hello Android 12” or “Hello iOS 15”. This logic will lie in the shared code and the UI part will not know the specific details of this value.
From the illustration above, the Platform class, which is located inside the shared module, is accessed by both the UI code for Android and iOS. Take note of the new keywords introduced in Kotlin multiplatform, the expect and actual keywords.
Classes marked with the expect keyword indicates that KMM expects a platform-specific value or implementation for this symbol. For a class, it expects another class with the same name that is marked with the actual keyword. For a variable, it expects another variable with a value inside another actual class.
This actual implementation should be created in both iOS and Android platforms for the module to compile. If platform-specific code is not needed, then the code will be implemented in the common module.
Migrating Existing Projects into KMM
It is possible to migrate existing native projects into KMM. Existing business logic from the Android code with Kotlin is extracted into a separate library, which is used to share with the iOS app.
If the existing project is well-designed and loosely coupled, it will only take minimal effort to extract the business logic from a monolithic application.
The Future of Cross-platform Development
Kotlin Multiplatform Mobile is a great software development kit and solves a lot of problems in the mobile development ecosystem. Sharing code between platforms becomes more resource-efficient, while giving you full control of the platform-specific technologies available in Android and iOS, without being trapped in a cross-platform or hybrid framework.
However, this technology is still very new. As of this writing, it is still in the Alpha version and expected to go Beta by second quarter of 2022. Though it is expected that APIs and dependencies might change often until it becomes stable, it is also important to note that using KMM Alpha now would not affect the successful production of your apps as many companies such as Square and Autodesk are already using it.
Once Kotlin Multiplatform Mobile reaches Beta stage, it is anticipated that more companies would start to adopt it as an alternative for cross-platform projects aside from React Native and Flutter.
We recommend fellow mobile developers to keep an eye on further developments on KMM, as it is also a great opportunity to have a headstart with hands-on practice on this tool and experiment with the APIs. With JetBrains’ dedicated commitment to improve and evolve this toolkit, KMM is likely to take off in the near future.