Kotlin Multiplatform, from the compiler up · check

Know Kotlin/Native under the hood already?

One question for each of the 9 puzzles in this module. A right answer marks that puzzle as passed, so you can jump ahead. A wrong one tells you which puzzle to start with. Nothing is sent anywhere: it's saved in this browser.

Puzzle 23: From IR to machine code

You change one line in commonMain, then run the app from Xcode with a debug build for the simulator. Which stages run for your shared module?

Right. Compile, then link.

Not quite. Only the link stage writes a binary; the framework isn't updated without it. Learn it in puzzle 23 →

Not quite. The klib holds your module's code as IR, so a changed line means a new klib first. Learn it in puzzle 23 →

Puzzle 24: "One binary: the linker"

Your shared module has a function that no Kotlin code calls, and it isn't in the API Swift can see. You build a release framework. Is the function in it?

Right. Release: unreached and not exported, so dropped before LLVM.

Not quite. A release build follows calls from the API and drops what nothing reaches. Learn it in puzzle 24 →

Not quite. It runs in Kotlin/Native's back end on the IR, before LLVM; the linker's trim comes later and is smaller. Learn it in puzzle 24 →

Puzzle 25: Static or dynamic

You set your framework's isStatic to true and rebuild the iOS app. On the phone, what does dyld load for your Kotlin code at launch?

Not quite. A static library isn't a separate file on the phone; it's inside the executable. Learn it in puzzle 25 →

Right. Static: copied in when the app is built.

Not quite. "Static" means joined at build time, not kept in memory. Learn it in puzzle 25 →

Puzzle 26: Framework or XCFramework

You try to merge the iosArm64 framework and the iosSimulatorArm64 framework into one universal framework with lipo. What happens?

Right. Same architecture, two platforms: lipo can't help.

Not quite. lipo can't put both in at all, since they share one architecture; and Apple says never to merge device and simulator builds anyway. Learn it in puzzle 26 →

Not quite. The halves are told apart by architecture, and these two have the same one. Learn it in puzzle 26 →

Puzzle 27: How Xcode builds your Kotlin

On a fresh checkout, the Run Script phase sits below Compile Sources. The developer presses Run. What happens?

Not quite. Build phases run in the listed order, top to bottom; the script would come after Compile Sources. Learn it in puzzle 27 →

Right. The framework must exist before Swift is compiled.

Not quite. The task links only one framework; the problem is order: Swift compiles before it exists. Learn it in puzzle 27 →

Puzzle 28: What has to match

Your build server runs Linux. Which of these can it still do for your iOS targets?

Right. Linux: klibs yes, Apple binaries no.

Not quite. It runs on Linux, and can produce klibs for Apple targets; only the link stage for them needs a Mac. Learn it in puzzle 28 →

Not quite. Final binaries for Apple targets can be built only on a Mac. Learn it in puzzle 28 →

Puzzle 29: Reading a crash from the field

The same crash happens twice. The first report says shared 0x104a3c1f4 0x104a20000 + 115188; the second, from a later launch, a different first number. Which number does the dSYM need to find the function?

Right. Offsets are fixed at build time; load addresses change at launch.

Not quite. The load address is where iOS put the binary this time; the function is found by the offset inside it. Learn it in puzzle 29 →

Not quite. That address is only valid for one launch: the load address changes, the offset doesn't. Learn it in puzzle 29 →

Puzzle 30: Where the build minutes go

An iOS developer runs assembleXCFramework after every Kotlin edit, then tries it in the simulator. Which change saves the most time?

Not quite. That's one more target to link, so it takes longer, not shorter. Learn it in puzzle 30 →

Right. Build only the binary you run.

Not quite. It skips reading the build files; every target is still linked, in debug and in release. Learn it in puzzle 30 →

Puzzle 31: Where the megabytes come from

After you export one more large module to Swift, the release framework grows by megabytes. Which earlier idea explains it?

Not quite. No klib reaches the app; the binary holds machine code, and exporting keeps all of the module's code reachable. Learn it in puzzle 31 →

Right. Exported API is a starting point, so it all stays.

Not quite. Export turns off dead code elimination for that module only; LLVM still optimizes everything. Learn it in puzzle 31 →

Back to the overview →