How does the Kotlin Multiplatform work?

How does the Kotlin Multiplatform work?

The Kotlin Multiplatform feature is very promising. And, in this blog, we are going to learn how does the Kotlin Multiplatform work? When we know how it works, we can get the most of it. I love investing my time in learning these hard things the hard way and then sharing it with all of you. Let's get started.

When I say Kotlin Multiplatform feature is very promising, there is a strong reason behind it, Kotlin Multiplatform let us reuse the business logic and does not impose any restrictions on how we build our App's UI. So, the appearance, behavior and the most important one, the performance, all remain native. Native UI helps in providing the best user-experience to the customers.

How does the Kotlin Multiplatform work?

When we consider the Kotlin Multiplatform just for the two platforms which are Android and iOS, we write the business logic code once which runs on both the platforms.

How does it work?

First, we write the shared code in Kotlin. For Android, it gets converted to the Java bytecode that can be executed in the JVM. So, Android already having the Virtual Machine(VM) is easily capable of running it.

How does the Kotlin Multiplatform work?

But what about the iOS, how can it run Kotlin? Can we do it without the Virtual Machine(VM)? Have you ever wondered how does it work?

For iOS, the Kotlin code must be converted to the source code(native code executable on iOS) that goes inside the framework. That source code works on iOS.

How does the Kotlin code get converted to the source code that is capable of running on iOS?

Here comes the Kotlin Compiler into the picture. It has two parts as follows:

  • Frontend - It converts the Kotlin Code into the IR (Intermediate Representation). That IR is capable of getting converted into the native code that is machine-executable using the backend which is described below.
  • Backend - It converts the IR into the native code that is machine-executable. This is possible because of the Kotlin/Native Infrastructure built by the JetBrains. Basically, for Android, it converts the IR to the Java Bytecode and for iOS, it converts the IR to the iOS native machine-executable code.
How does the Kotlin Multiplatform work?

It is important to note that we are able to run the shared business logic code written in Kotlin on a platform like iOS without the Virtual Machine(VM).

Here, Kotlin/Native plays an important role. What exactly is Kotlin/Native?

From the official documentation:

Kotlin/Native is an LLVM backend for the Kotlin compiler, runtime implementation, and native code generation facility using the LLVM toolchain.
Kotlin/Native is primarily designed to allow compilation for platforms where virtual machines are not desirable or possible (such as iOS or embedded targets), or where a developer is willing to produce a reasonably-sized self-contained program without the need to ship an additional execution runtime.

Now, the next question arises in our mind, what is LLVM?

We can say that LLVM is a library for programmatically creating the machine native code from the Intermediate Representation(IR).

We first create the Intermediate Representation(IR) which is needed by the LLVM and then LLVM does its magic.


Kotlin Code > Intermediate Representation(IR) > Machine-Executable Native Code.

This way, the Kotlin Multiplatform works.

If you want to get started with Kotlin MultiPlatform, check here.

Want to learn advanced concepts in Android? Apply Now: MindOrks Android Online Course and Learn Advanced Android

Also, Let’s become friends on Twitter, Linkedin, Github, Quora, and Facebook.


Team MindOrks.