Adopting Kotlin multiplatform: a personal experience in production code.
What is Kotlin Multiplatform?
A 'tool' that empowers app developers to share code across Android and iOS.
Kotlin Multiplatform became Stable and Ready for Production Nov 2023.
We incrementally adopted Kotlin multiplatform mobile in Nov 2022 with the following rules in mind:
Business logic after all, is just pure arithmetic and logic. Writing it more than once is nonsense.
Key benefits:
Can't say the same about other cross-platform solutions.
1- A cleaner architecture:
Enters the?Inward Dependency Rule:
Injecting is simply passing an instance of an object to a method instead of initiating it in the method body itself.
领英推荐
Inversion is simply injecting such dependency as an abstraction instead of the actual implementation. ie. declaring the type of the injected parameter as an interface instead of a class.
2. Eliminating some of the ‘it works on android but it doesn’t work on iOS kind of bugs.
Now any endpoint consumption related bug is likely to be common in both platforms, meaning that on an organizational level, it's easier to test, spot defects earlier, and fixing them once.
3. App development time is slashed by 25% to 30%.
With UI out of the equation; one would have expected time to be slashed by 50%, but you still need to write implementations twice for each platform whenever there's an abstraction of a platform dependency injected into the shared code. Code that depends on abstraction can be slower to write but it's more stable and easier to maintain long term. Also If your iOS code base contains Objective-c code (like ours) this can lead to mild challenges as well. Still absolutely worth it.
4. Less lines of code to maintain.
That's an obvious one, but It becomes even more beneficial if you're in the process of migrating some of your existing code from Objective-c to Swift. You can choose to migrate it to Kotlin instead and using it in Android. Or if you already have it implemented for Android you can choose to use it in iOS and get rid of Objective-C code straight away.
5. You're not introducing a new programming language to your Native App technology stack.
Android Apps already uses Kotlin, meaning that as a Native App team you already have the know how. You're not introducing TypeScript, C#, darts, or any other new language.
Can't say the same about other cross-platform solutions.
Down sides:
Summary:
Adopting Kotlin multiplatform in production has been a success story from a personal experience. It can also help getting the Android & iOS teams closer together without sacrificing any of the native development perks. If you're going to adopt it you need to do so responsibly. Stir away from sharing UI code. Threading mechanisms should be kept separate for each OS. If you manage to abstract running work in the background then you're good to go. Consider sharing http clients by depending on Ktor. Sharing Database solutions can be worth it if you have a lot of queries going on, can be justifiably skipped tho. The main downside has been debugging shared code from XCode -some plugins can help mitigate this-. Removing some autonomy from iOS developers. -Can't finish a feature end to end without a kotlin developer- which can be looked at as a perk or as a downside.
Over and out.