Android from the foundation · Puzzle 2 of 6
ART is what runs your code
Your Kotlin ships as DEX bytecode. ART, inside each app's process, reads it line by line, compiles hot code while it runs, and the system compiles more of it ahead of time later.
The problem
In puzzle 1, each app became its own house. But what actually runs inside the house? Android runs on many kinds of CPU, yet you upload one APK, and machine code for one CPU won't run on another. So the APK must hold something more general, and someone in the house has to translate it.
See it
You ship one APK for every kind of phone, so it can't hold machine code for one CPU. It holds DEX: instructions for Android's runtime, ART, a translator that lives inside each app's process.
Step 1 of 5At first, ART reads the DEX line by line (interpreting): it works, but it's slow.
Step 2 of 5Code that runs often is "hot". ART compiles it to machine code right now, while the app runs (JIT), and writes it in a notebook: the profile.
Step 3 of 5Later, about once a day while the phone is idle and charging, the system's
dex2oattool compiles the profiled code ahead of time (AOT). Next start, it's already fast.Step 4 of 5A Baseline Profile is that notebook written in advance and shipped inside the APK, so the important code is compiled before your first launch.
Step 5 of 5
The building block
Your Kotlin compiles to DEX bytecode: compact instructions that aren't tied to any CPU. ART (the Android Runtime) runs inside your app's process and turns them into work. Each piece of code can be in one of three states:
- interpreted: read one instruction at a time (slow, works immediately);
- JIT-compiled: compiled to machine code now, while the app runs, because it's hot;
- AOT-compiled: compiled ahead of time, later, by the system's
dex2oattool, using the profile ART wrote.
ART also runs your app's garbage collector, which frees objects you no longer use. That's for another module.
The analogy
A translator lives in your house. The first time, they read your recipe word by word. Recipes you ask for often, they translate on the spot and note in a notebook. Once a day, at night, a professional translates everything in the notebook properly.
Check yourself
Sources
What you now know
JIT = compile hot code now, while the app runs. AOT = compile it ahead of time, later, from the profile. A Baseline Profile moves "later" to "before the first launch".
Answer every question above correctly to unlock the next puzzle.