Share logic between Android and iOS using Kotlin Multiplatform
Nowadays, many products are composed of an iOS and Android app, and sharing code between both platforms is crucial to save time and to avoid different behaviours. That’s why hybrid technologies like Flutter, React Native, and many more can be powerful tools for mobile development because they enable us to create cross-platform applications with a single codebase. There are some pros and cons between hybrid and native solutions but that’s not the topic of this blog post. I just want to mention one common constraint with hybrid solutions: it’s hard to implement in existing projects.
Fortunately for us, there have been some interesting developments regarding mobile cross-platform: a tool that enables us to share code between mobile platforms with native performance and easy integration in existing projects. It’s called Kotlin Multiplatform Mobile (KMM).
What’s Kotlin Multiplatform Mobile?
KMM is an SDK for Android and iOS apps that helps us create Kotlin Multiplatform modules that compile into Android libraries and iOS frameworks.
KMM works because of the Kotlin Multiplatform technology. Kotlin Multiplatform is a set of libraries that allow us to program multi-platform logic in common and platform-specific code. Here is how Koltin Multiplatform is composed:
Common Kotlin (core libraries and tools)
- Generates code for Java Virtual Machine.
- Generates code compatible with Android and iOS.
Code written in Common Kotlin works everywhere on all supported platforms. Libraries created for Common Kotlin (called Multiplatform libraries) can be used on platform-specific implementations (JVM, JS, and Native). This can give us cross-platform development beyond mobile. You can read more about this here: Kotlin Multiplatform use cases. But let’s focus only on Android and iOS.
How does it work?
In practice, things work like this. The code to be shared on Android and iOS is implemented in an independent Gradle module (more specifically a Kotlin Multiplatform module) and it’s written in Kotlin. Let’s call it the Shared module. You can create it using the KMM plugin for Android Studio.
Kotlin is a modern programming language developed by JetBrains and adopted by Google as the recommended language for Android development.
If the Shared module is created inside an Android application project, then the integration for Android is complete. The package inside the Shared module can be imported and its implementations would be available right away.
For the iOS application, the integration is different. To be able to access the implementations of the Shared module, the code needs to be compiled to native binaries using the Kotlin/Native compiler. The native binaries are bundled with a regular iOS framework. Then the Xcode project needs to know the path of that framework to be able to recognize it and use it like other third-party libraries. For each build of the iOS application, the project obtains the latest version of the framework. You can even link the Xcode project to the Gradle project and run the iOS app directly from Android Studio. If you want to know more about how the iOS part works, please check: Understand iOS project structure.
Here’s a diagram of a possible use case. The `LoginDataValidator` class and the `LoginAuthenticator` class can be used in both apps, sharing the same logic, coded only once. The `LoginAuthenticator` accepts a network interface as dependency, exemplifying the access of platform-specific APIs. External dependencies are also possible to integrate but only Objective-C dependencies and Swift dependencies if their APIs are exported to Objective-C with the `@objc` attribute. Pure Swift dependencies are not yet supported.
You can try to put this into practice using the oficial tutorial: Create your first cross-platform mobile app.
At Whitesmith, many projects can be started from scratch but others may not. Sometimes our clients already have validated products with a foundation of code that we need to evolve - by the way, check out some of our recent projects. That’s why we think solutions like KMM are important for existing mobile projects to reduce time spent writing and maintaining the same code for different platforms while retaining the flexibility and benefits of native programming.
Disclaimer: we never used KMM in production, mostly because it’s still in Alpha (it hasn’t reached a stable version yet). Even so, it looks promising and also evidently that the Kotlin team is fully committed to improving and evolving this technology.
Are you using Kotlin Platform in your team? Let us know how it’s going on twitter @whitesmithco.
Cover photo by Elaine Casap.