Kotlin Multiplatform
Overview
Kotlin Multiplatform is a powerful framework that offers many advantages over native programming languages. It provides full interoperability between Android and iOS, as well as the ability to create multiplatform libraries and desktop applications for Windows, Linux, and macOS. Kotlin Multiplatform code can interoperate with native code, which means that it can access the specific features of each platform. This makes Kotlin Multiplatform a powerful tool for developing cross-platform applications.
What is Kotlin Multiplatform?
The Kotlin Multiplatform functionality introduced in 2017 with Kotlin 1.2 allows us to write a single codebase that can be compiled into multiple platforms, such as Android, iOS, macOS, Windows, and Linux.
Thus, it saves a lot of time and effort required to write and maintain code with the benefit of native programming i.e. our apps will be able to access all of the native APIs for each platform, and they will perform as well as native apps. Companies like Philips, Netflix, VMWare, etc. are using Kotlin Multiplatform in production.
It is based on the simple idea of "Write Once Run Anywhere" but on a much higher level where we target multiple platforms.
How Does Kotlin Multiplatform Work?
We can use Kotlin to maintain a single codebase for multiple application logics such as networking, data storage, data validation, computations, analytics, etc.
- The Kotlin Compiler first parses the kotlin code to generate Intermediate Representation or IR. IR is independent of the platform.
- The Kotlin compiler uses a set of heuristics to separate parts of IR that can be shared between platforms from the parts that need to be platform-specific.
- This IR is then compiled into native code for each of the platforms.
- This native code is then packaged into an application to be run on the target OS.
For instance, we have a program to be run on Android and iOS. The business logic will be the same for both, so it gets converted to the byte code. As Android has a virtual machine, byte code is easily executed in JVM, but in the case of iOS virtual machine is absent thus, the code gets converted to the native code to be executed on iOS.
Kotlin Multiplatform use cases
Android and iOS applications
Kotlin multiplatform can be used to build an application that is executable on Android as well as iOS. This common codebase for both platforms typically includes business logic, data access, and UI components used by both platforms.
JetBrains provides a Kotlin-based declarative UI framework that allows us to share UIs across Android and iOS.
Platform-specific codebases such as APIs are to be developed for both Android and iOS. This is used to take advantage of the specific features of each platform.
Multiplatform libraries
Kotlin Multiplatform libraries allow you to write a single library code that can be used in Android, iOS, web, and desktop applications.
This is done by sharing common code between platforms and providing platform-specific implementations for each platform. Kotlin Multiplatform libraries can be used as a dependency in other cross-platform projects, which saves time and effort when developing cross-platform applications.
Desktop applications
Kotlin Multiplatform desktop application development is also facilitated by the JetBrains Toolbox application. We can create our project with UIs that are sharable among Windows, macOS, and Linux.
Code sharing between platforms
Code sharing is as explained above. Expected and actual declarations is a mechanism used for accessing platform-specific API's from shared code. We can write generic code that can be used on all platforms as well as for specific platforms when needed.

Kotlin Common Code
Kotlin Multiplatform has a unique way to run common code on different platforms. Kotlin provides platform-specific compilers for the conversion of the code for the respective OS.
Each version is composed of platform-specific tools, libraries, and extensions to the Kotlin language.

These compilers include:
- Kotlin/JVM: This compiler compiles Kotlin code to Java bytecode, which can be run on the Java Virtual Machine (JVM).
- Kotlin/JavaScript: This compiler compiles Kotlin code to JavaScript, which can be run in a web browser or on a Node.js server.
- Kotlin/Native: This compiler compiles Kotlin code to native code for a specific platform, such as iOS, macOS, or Linux.

Multiplatform libraries can be used to improve the capabilities of the common code. Code can be built in shared parts in one area by providing APIs for common code which can later be decoded for target platforms.
These libraries are not only platform specific, they are version specific, for example, different Java versions such as 6 and 8 might require different compilers but the same Kotlin code can be written for both. Different compilers can be used later to run on different platforms.
How Does Multiplatform Differ From Cross Platform?
| Multiplatform | Cross Platform |
|---|---|
| A Kotlin multiplatform system supports platform-specific as well as common code shared by various platforms. | Here, platform-specific code is not allowed. Common code is only written and is executed on all supported platforms. |
| More features can be added as we are using Native code functionality. | Fewer features as it needs to be portable for all platforms. |
| Application is more flexible and developers have more control over performance. | Limited control over the performance and features of the application. |
| Can be as fast as Native code. | Can be slower than native code. |
Advantages of Kotlin Multiplatform
- Community Support: Although Kotlin is the official language for Android as announced by Google, many companies are using Kotlin. Thus, many users started working on the language and it also is backed by Java so it has a huge community support.
- Quick and Clean code: It supports "Write Once Write Anywhere" not only for methods but also for multiple platforms. Kotlin Multiplatform code can interoperate with native code, which allows you to take advantage of the specific features of each platform and also there is no lagging behind the native platforms.
- Fast Development: As code sharing is possible a lot of time and effort are saved in the development process. Features like null safety, IDE support, ans interoperability with Java, make it fast Netflix flexible.
- One Language, Multiple platforms: No new programming language is to be introduced to make the app work in Android as well as iOS Kotlin is enough for the code base.
Companies Opting for Kotlin Multiplatform
Some of the well-known companies that are using Kotlin multiplatform functionality are:
- Netflix: The biggest video streaming service also uses Kotlin Multiplatform currently. Due to poor connectivity issues some calculations are to be performed on the front end and thus to meet these requirements and avoid code repetition, Kotlin Multiplatform was chosen.
- VMware: VMware facilitates managing and delivering applications across multiple devices securely. Thus, Kotlin stood as the perfect match for the requirements.
- Philips: Kotlin Multiplatform makes it easier for Philips to implement new features faster and also fosters and enhances the interaction between iOS and Android developers.
- Realm: Realm is a mobile database that allows developers to store data locally on their Android and iOS devices. It is a popular choice for mobile developers because it is fast, efficient, and easy to use. Realm uses Kotlin Multiplatform to build its mobile database, which allows developers to store data locally on their Android and iOS devices.
- Spotify: Spotify uses Kotlin Multiplatform to build their mobile app, which allows users to listen to music and podcasts on their Android and iOS devices.
Conclusion
- Kotlin Multiplatform feature lets the user write business logic, data storage, data validation, computations, etc. as common code and platform-specific code to make use of platform-specific APIs.
- This code is converted to Intermediate Representation which can be used to generate native code for the respective OS.
- Kotlin multiplatform is used by various companies like Netflix, Spotify, VMware, AutoDesk, etc. as it provides development of an application that works on both Android as well as iOS, a desktop application for Windows, Linux, and macOS, and development of multiplatform libraries.
- Kotlin has huge community support, it facilitates quick and clear code and flexible and fast development.