How does the Kotlin Multiplatform work?

  • Amit Shekhar
    Amit Shekhar
    Published on
How does the Kotlin Multiplatform work?

I am Amit Shekhar, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.

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

When I say the 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 most importantly, the performance, all remain native. Native UI helps in providing the best user experience to the customers.

kotlin mpp shared code

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 has a Virtual Machine(VM) that is easily capable of running it.

jar framework mpp

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

For iOS, 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 JetBrains.For Android, it converts the IR to Java Bytecode and for iOS, it converts IR to the iOS native machine-executable code.

kotlin native ir llvm

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.

Watch the video format: How does the Kotlin Multiplatform work?

Master Kotlin Coroutines from here: Mastering Kotlin Coroutines

That's it for now.


Amit Shekhar

You can connect with me on:

Read all of my high-quality blogs here.