Kotlin multi-platform development
4 min read

Kotlin as a programming language seems to be gaining a lot of popularity and Google’s adoption of this language seems to have paid off both for Google and the community at large. Kotlin’s syntax has been extremely good for App development and resembles Dart and Swift a lot. This makes the code readable and easier to develop. It overcame many of Java’s shortcomings such as verbose syntax, reliance on third party libraries for lot of common patterns, lack of null safety, lack of smart typecasting while retaining the best parts of Java and most importantly being fully compatible with Java libraries. This emergence of Kotlin is not paving way for even larger adoption.
Multiplatform development
Android and iOS have become two major operating systems. However, they are also an ecosystem. Android has Android automotive, Google TV etc. iOS has Carplay, Apple TV, iPads etc. Not to mention web also plays a big role for apps.
Any reasonable company needs to launch their apps both for Android and iOS. Some of them such as streaming platforms might have to also target TVs or even Auto screens. This makes the development process complicated and expensive. A solution to this was initially proposed using webview where the app is nothing but a wrapper around webapp. However, the inherent limitations of the browser made this sort of approach less performant. ReactNative and Flutter were two major frameworks that were developed by Meta and Google respective. ReactNative uses JSX where as Flutter uses Dart. Both approaches were relatively successful though they had their own limitations.
The way these two frameworks woks is by using extremely low level graphics APIs provided by the operating systems (which are also used by say games) and then render UI using it. This makes app much faster than alternatives such as webview. The framework however is generally very complex and will rely on plugin architecture to use the various features of the OS such as as Camera API.
The advantage of multiplatform frameworks is that it makes app development very simple for simple apps but as the apps get complex it might present some challenges.
Kotlin Multiplatform
Kotlin multiplatform is even different take from the approach Flutter or ReactNative take. Kotlin presents merely a language wrapper around the native UI of Android and iOS. The Kotlin code gets translated into native binaries for Android and iOS without any rendering engine. This means apps are far more performant than Flutter or ReactNative.
Of course, Android and iOS are very different and hence not all code can not be shared across both platforms. Kotlin/Native provides bidirectional interoperability with Swift and Objective-C, enabling seamless integration of Kotlin code into iOS projects. Similarly it provides seemless integration with Android’s SDK. Kotlin being a first class citizen for Android development has much better support for Android than iOS.
Compose multiplatform is another library that can be used too with Kotlin multiplatform. It is a separate project but is very complimentary.
Sharing business logic and not just code
While Flutter and ReactNative focused on allowing developers to have a single codebase, Kotlin Multiplatform has a slightly different objective. It recognizes the fact that Android and iOS might diverge from each other in many ways. It recognizes that engineers are willing to and even prefer to write OS specific code when the performance and efficient use of features matter the most but still do not want to duplicate the dumb parts.
This is where Kotlin multiplatform excels if you ask me. It splits the codebase into Android specific code, iOS specific code and common logic code. The common logic code is where you put all the core business logic, common algorithms and so on and in platform specific code you write more platform focused code. It is upto you to decide this boundary which gives immense flexibility.
It makes developers productivity gains as they do not have to learn multiple programming languages for most parts as single source of truth for business logic and efficient testing.
Choosing Kotlin multiplatform
Kotlin multiplatform all the benefits that are generally associated with Flutter and ReactNative but with better performance. There is some tradeoff as you have to still write decent amount of iOS and Android specific code but since the language is same, the productivity gains are significant.
However this is a new technology and is not fully mature, as adoption goes up and community support grows this will change but it also means betting a large project on this technology is risky. Also, iOS support is weaker than Android support. Given Apple’s closed nature this is likely to remain so always. if iOS is your primary target you might want to reconsider this technology.