APK Structure & the ART Runtime
Android Internals · Mobile Internals track
7 min read
In this module
The APK Format
Android apps ship as APK (Android Package Kit) files. Like iOS IPAs, an APK is a ZIP archive at heart. The contents tell a different story, because Android's runtime model is a different beast altogether.
# Structure of an APK fileMyApp.apk (ZIP archive) classes.dex # Compiled DEX bytecode (app code) classes2.dex # Additional DEX file (multidex) AndroidManifest.xml # Binary XML: permissions, components res/ # Compiled resources (layouts, drawables) lib/ # Native libraries (.so files per ABI) arm64-v8a/ # 64-bit ARM native code armeabi-v7a/ # 32-bit ARM native code assets/ # Raw, uncompiled assets META-INF/ # v1 (JAR) signing information, if present MANIFEST.MF # Manifest of file hashes CERT.SF # Signed manifest CERT.RSA # Signing certificate resources.arsc # Compiled resource tableMETA-INF/ holds only the oldest signature scheme, v1 (JAR signing), which signs individual entries and is kept today mainly for very old devices. Since Android 7.0 the important signatures live in the APK Signing Block, a binary block inserted just before the ZIP central directory that a ZIP tool won't list: v2 signs the whole file, v3 (Android 9) adds key rotation, and v4 (Android 11) adds a separate .idsig file for incremental installs. Use apksigner verify -v to see which schemes an APK carries.
The star of the show is classes.dex, the compiled application code in DEX (Dalvik Executable) format. Where iOS ships native Mach-O binaries straight to the device, Android ships bytecode and lets the phone finish the job.
The DEX Format
DEX is register-based bytecode, where the JVM is stack-based. Register-based code expresses the same operation in fewer instructions, which matters when you're squeezing the runtime onto a phone with finite battery.
// Java source codepublic int add(int a, int b) { return a + b;}
// JVM bytecode (stack-based) - 4 instructions// iload_1 ; push a onto stack// iload_2 ; push b onto stack// iadd ; pop both, push sum// ireturn ; return top of stack
// DEX bytecode (register-based) - 2 instructions// add-int v0, v1, v2 ; v0 = v1 + v2// return v0 ; return v0DEX has one famous limitation: a single file can reference at most 65,536 methods, because the method reference index is a 16-bit unsigned integer. Large apps blow past this and have to use multidex, splitting their code across classes.dex, classes2.dex, and so on.
DEX (Dalvik Executable) is Android's register-based bytecode. Java/Kotlin source compiles to .class files, which the D8 compiler then turns into DEX. DEX is denser than JVM bytecode, uses register operands so the instruction count drops, and caps out at 64K method references per file. ART finishes the job on-device, lowering DEX to native code.
The Compilation Pipeline
Java/Kotlin source goes through a small assembly line before any of it runs on a phone.
- Source to bytecode:
javacorkotlincproduces standard JVM.classfiles. - Class to DEX: D8 (the successor to dx) turns
.classfiles into DEX bytecode, desugaring along the way so modern Java features still run on older Android. - R8 optimisation: release builds pipe through R8, the heir to ProGuard. It shrinks dead code, renames symbols, and squeezes out a bit more performance.
- Packaging: DEX files, resources, manifest, and native libraries get zipped into an APK.
- On-device compilation: ART takes over and compiles DEX into native code (see below).
ART: The Android Runtime
ART took over from Dalvik in Android 5.0. The headline feature, added in 7.0, is its hybrid compilation strategy — a compromise between install speed, storage, and runtime performance that took Google several attempts to get right.
The Three-Phase Approach
Without a profile, ART does not AOT-compile your app at install time. Many installs from Play do arrive with one, though: Play can ship a cloud profile aggregated from other users, and apps can bundle a baseline profile, and dex2oat compiles the listed code during install. The phases below are the fallback when neither exists, and they still run afterwards to refine things:
Phase 1 — Interpret and JIT. The app boots into the interpreter. As methods get called, the JIT compiles the hot ones to native code in memory. Install is fast, performance is acceptable straight away.
Phase 2 — Profile collection. While you use the app, ART writes a profile: which methods are hot, which classes load at startup, which code paths actually run. The profile sits on disk.
Phase 3 — AOT compilation. When the phone is idle and charging, dex2oat reads the profile and runs ahead-of-time compilation, producing an OAT file (an ELF shared library full of native code) for the hot code only. Everything cold stays as DEX bytecode and stays small.
Laid out over an app's first few days, the phases look like this:
# Profile-guided compilation artefacts/data/misc/profiles/cur/0/com.example.app/primary.prof/data/app/~~<random>/com.example.app-<random>/oat/arm64/ base.odex # OAT file (AOT code) base.vdex # verified DEX# /data/dalvik-cache/ now mostly holds boot and system artefacts;# older releases kept app OAT files there tooSince 7.0, ART uses a three-phase strategy: (1) interpret and JIT-compile hot methods at runtime, (2) record an execution profile, (3) AOT-compile the profiled code while the phone charges overnight. Result: snappy install, modest storage, and tuned native code where it matters.
Security Implications of ART
ART's compilation model has a few interesting consequences for security:
- DEX decompiles cleanly. jadx will turn most DEX back into readable Java. R8 obfuscation slows you down. It does not stop you.
- JIT needs code it can write and then run. The JIT emits code into a buffer and then executes it. Recent ART avoids a single writable-and-executable mapping by mapping the code cache twice, a writable view for the compiler and a separate executable view for running it, and SELinux gates who may create executable memory at all. Still, any process that generates code at runtime offers a target that a fully AOT-compiled one doesn't.
- Profiles leak usage patterns. The on-disk profiles reveal which features a user exercises most. Forensically interesting; privacy-relevant.
The AndroidManifest.xml
The manifest is where an app spells out who it is. Inside the APK it lives as binary XML, not anything you can grep with a regular text editor. It carries:
- The app's package name and version
- All components (activities, services, broadcast receivers, content providers)
- Required permissions
- Minimum and target SDK versions
- Intent filters that define how the app responds to system events
For security work, the manifest is a treasure map of declared attack surface. Exported components, requested permissions, services that talk to other apps — they're all listed in one file.