In this article, I will be discussing why I believe that Kotlin Multiplatform Mobile (KMM) is the future of cross-platform development from the viewpoint of an iOS Engineer.
This is a highly opinionated piece and I am very aware that some might have opposing opinions hence - hot take.
Hot Take (noun) - a piece of writing or speech, especially on the internet, giving someone's personal opinions about a topic, usually strong opinions that have not been carefully thought about and that many people are likely to disagree with Cambridge Dictionary
Let's talk about cross-platform development
Have you ever downloaded an app, opened it, browsed through it, and think something just does not feel right? Ever wondered why an app is not smooth and why some elements seem out of place? It could be that the app in question was developed using a cross-platform framework.
The promise of cross-platform development is "code once, deploy everywhere". Whilst frameworks like React-Native and Flutter do really well to deliver on this promise there are some caveats that raise concern. User Interface and latest platform features is an area in which I feel like these frameworks fall short. In practice, this may not be the fault of the framework but rather that of the engineer not taking the time to ensure that the final result conforms to specific platform standards.
To make a good cross-platform application one has to be conscious of platform differences and cater to them. iOS has its Human Interface Guidelines and Android has Material Design. For your app to feel at home on a device it is crucial that you adhere to the platform principles.
This leads to one have to write code for each platform somewhat creating a nuisance in the development process. "I was promised write once, deploy everywhere. But to make it look nice and work properly I still have to code twice?" Eventually, you could end up in the not ideal situation of having a codebase that has so many if or switch statements to cater for platform-specific code.
Kotlin Multiplatform Mobile - What is it?
Kotlin Multiplatform Mobile allows you to use a single codebase for the business logic of iOS and Android apps. You only need to write platform-specific code where it’s necessary, for example, to implement a native UI or when working with platform-specific APIs. - Kotlin.org
KMM is a relatively new framework (Alpha released Aug. 2020) to the cross-platform world that enables you to share common code between your Android and iOS Applications. This is done by creating a shared module written in Kotlin that is then compiled to bytecode and native iOS.
Unlike other cross-platform frameworks, KMM gives you the choice to decide what aspects of your code you want to share. You can decide to share as little code as possible or do the opposite. The shared code module can be included in existing projects using Cocoapods and SPM.
KMM does not currently have a UI component this allows you to keep your UI native. No need to learn a new language for UI. You also get the benefit of being able to use all the latest platform features like SwiftUI and JetPack Compose.
I must say that I have been thoroughly impressed with the documentation and resources the JetBrains team has been creating for KMM. Take a look at the Youtube Channel and the official documentation for yourself. ⭐️
My Key takeaways
- Flexibility, only share the code you want to share.
- Compiled to bytecode, does not increase binary size like other cross-platform frameworks.
- Native UI, you can use UIKit and SwiftUI.
Common iOS Engineer questions
1. Do I have to learn Kotlin?
Yes. To contribute to the shared code module you will have to learn some Kotlin. This might be a big learning curve but most of the things we as iOS Engineers love about Swift are also true for Kotlin. Type-safety, optionals, and clean syntax are all present in Kotlin.
2. Will I have to code in Android Studio?
Yes. No. Probably. To be a contributor on the app's shared code you will probably have to use Android studio but for all other native code, you can still use Xcode.
3. How will I debug Kotlin code?
With Android Studio.
4. Libraries, Cocoapods, and Swift Packages?
You can use libraries, Cocoapods, and packages as normal in your iOS project.
5. How does KMM handle concurrency?
Using Kotlin/Native. Please read this post from JetBrains on Kotlin/Native Memory Management Roadmap for a more detailed explanation.
More questions? See the Frequently Asked Questions page on Kotlin.org
Now let's be honest there is no such thing as a silver bullet for cross-platform development. Let's take a look at the cons:
1. No shared UI
While some may see this as a con I on the other hand see it like a pro. I don't want to have a shared UI. I would rather have two targets one in SwiftUI and the other in Jetpack Compose. KMM does not currently provide a way to have shared UI components. I hope they keep it this way so that apps can maintain their native feel. If you do want to have shared UI components then ReactNative or Flutter is best suited.
2. Production readiness
KMM is a fairly new player to the cross-platform world and is still considered to be in development. Some features are still in the alpha/experimental phase of development. I don't think that this is cause for alarm to not use it. There are teams from Netflix and CashApp already using KMM in production. The development team is very good at communicating the stability of components and there's a dedicated page for this.
3. New build tools
As an iOS developer, I have become accustomed to Xcode and its quirks like having to clear derived data religiously. Having to learn how to use Android Studio and Gradle will be a major hurdle. Shared code needs to be debugged in Android Studio. For an iOS Engineer, you will now need two IDEs to develop whilst Android Engineers can achieve everything from Android Studio. I think the new tools will be the biggest challenge for an iOS developer.
While there is support for networking using the Ktor library development for networking with GraphQL is still in the experimental phase. I could not find any solutions for networking with gRPC. These limitations write off KMM for teams that use these technologies.
Introducing KMM or any cross-platform framework introduces complexity to app development. There are more aspects involved in the development life cycle. KMM is still in its infancy there will definitely be some pains experienced. With the community still being small there might not be enough resources online to help resolve some of the issues one might run into.
With that said if introducing KMM to a team that already has experienced Android Engineers, I'm sure the learning curve will be accelerated as they can assist you with any errors you may encounter with Android studio or Gradle.
I know the chances of this happening are close to impossible but it would be really awesome if Xcode had a KMM plugin.
There are some other downsides to using KMM for app development but in the spirit of keeping this post short and concise, we won't discuss them. I have included a list of resources that go into detail about some of the other cons like ObjC-interoperability, memory handling, and multi-threading below.
An argument for shared code
Scenario: You work on a team with dedicated iOS and Android teams. You're working on a new feature. The feature's business logic is designed and ready for development. Both teams go their separate ways and code the feature. Time for testing comes and QA reports that the feature works fine on Android but there is something wrong on iOS or vice-versa.
This is a situation many have been in. Having a shared codebase may be the solution to the situation above. Having a single source of truth for an application's business logic introduces predictability and stability.
My favorite aspect about Kotlin Multiplatform Mobile is its flexibility. You only have to share what you decide to share. Rather than UI components being bridged, they remain native. Code is compiled to bytecode.
These reasons are why I think KMM should be welcomed by the iOS community. It creates an environment for collaboration between communities and introduces the challenge of learning a new language and tools. 🤖 🤝🍏
I develop to solve problems that help people. The people that download our apps usually don't care about the technology used to make the app. What users want are good products and if KMM provides teams a way to deliver earlier then it's a win. 🚀
I'm looking forward to all the new features in KMM and the growth of this community. Thanks for reading my hot take on why I as an iOS Engineer like KMM! What's your opinion on Kotlin Multiplatform Mobile?
- Kotlin Multiplatform Documentation
- The Dos and Dont's of Mobile Development with Kotlin Multiplatform
- Kotlin Multiplatform – A remedy for all mobile app development?
- Kotlin Multiplatform — A panacea for mobile app development?
- We asked an iOS developer his thoughts after working with Kotlin/Multiplatform Mobile