Kotlin Multiplatform, from the compiler up · Start here · 1 of 10

One Kotlin, many machines

Your app's rules are written twice, once in Kotlin for Android and once in Swift for iOS. Here's how one Kotlin file ends up running on both, even though an iPhone has no JVM.

The problem

Your login rules, your price calculation, your sync logic: in most teams they're written twice, in Kotlin for Android and in Swift for iOS. Every bug gets fixed twice, and the two copies drift apart. Kotlin runs on the JVM, and an iPhone has no JVM. So how can one Kotlin file run on both?

See it

One Kotlin file goes into the compiler; the front end checks it; one back end makes bytecode for Android; Kotlin/Native makes native code for the iPhone, which has no virtual machine Login.kt front end back endbytecodeAndroid Kotlin/Nativenative codeiPhone no virtual machine
  1. One Kotlin file, with the rules your app follows on every platform.

    Step 1 of 4
  2. The compiler turns it into something a machine can run. Its front end reads and checks your code, the same for every platform you build for.

    Step 2 of 4
  3. Then a back end for each target, each platform you build for. For Android: bytecode, instructions for a virtual machine. Predict: what does an iPhone, with no virtual machine, need?

    Step 3 of 4
  4. Native code: machine code for its own CPU, made by Kotlin/Native. Same Kotlin, same checks, two kinds of output.

    Step 4 of 4

The building block

A compiler turns your source code into something a machine can run. Kotlin's compiler works in two halves. The front end reads your code and checks it: types, nullability, every rule of the language. It's the same whatever you build for. Then a back end turns the checked code into output for one kind of machine.

Each platform you build for is a target: Android, the JVM (servers and desktop apps run on it), iOS, the web. Each target has its own back end.

For Android, the back end produces bytecode: instructions for a virtual machine, not for any one CPU (on the phone, it's turned into the DEX that ART runs; ART is Android's own virtual machine, a close cousin of the JVM, which is why Android and the JVM are separate targets). An iPhone has no virtual machine to run bytecode. So for iOS there's Kotlin/Native: a back end, built on a widely used compiler toolkit, that produces native code, machine code for the iPhone's own CPU, which runs directly, with nothing in between.

Same Kotlin, same checks, two kinds of output. That's what makes the rest of this course possible.

The analogy

One recipe, two kitchens. A head chef reads and checks the recipe once (the front end). Then each kitchen's cook rewrites it for their own equipment (a back end): one for a kitchen with a helper who reads instructions aloud (a virtual machine), one for a kitchen where the cook works the stove directly (native code).

Check yourself

Your shared Kotlin already runs on Android and iOS. You add a desktop target. What happens to the shared code?

Not quite. The shared Kotlin is compiled again, by another back end. Nothing is rewritten.

Right. One front end, one back end per target.

Not quite. There's one front end for every target; only the back end differs.

Why can't an iPhone just run the Android bytecode?

Right. Bytecode needs a virtual machine; native code needs only the CPU.

Not quite. Speed isn't the issue: bytecode needs a virtual machine, and iOS doesn't have one.

What's the difference between bytecode and native code?

Right. What runs it: a virtual machine, or the CPU itself.

Not quite. They're different instructions for different machines, not a smaller copy.

Not quite. Both come from your Kotlin; they differ in what runs them.

Sources

  • Kotlin docs: Kotlin/Native overview ("Kotlin/Native is a technology for compiling Kotlin code to native binaries that can run without a virtual machine. Kotlin/Native includes an LLVM-based backend for the Kotlin compiler… primarily designed to allow compilation for platforms on which virtual machines are not desirable or possible, such as embedded devices or iOS")
  • Kotlin docs: The basics of Kotlin Multiplatform project structure ("Targets define the platforms to which Kotlin compiles the common code. These could be, for example, the JVM, JS, Android, iOS, or Linux.")

What you now know

The Kotlin compiler has one front end that reads your code and one back end per target. For Android it produces bytecode for a virtual machine; for iOS, Kotlin/Native produces native code that runs directly on the CPU.