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, with the rules your app follows on every platform.
Step 1 of 4The 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 4Then 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 4Native 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
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.
Answer every question above correctly to unlock the next puzzle.
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.