Introduction
A .dex file holds the compiled code of an app for
Android. Android apps are written in Java or
Kotlin and compiled to JVM .class
files first; the D8 tool then converts all the classes into Dalvik bytecode in one
.dex file. The name comes from Dalvik, Android’s original virtual machine;
its successor, the Android Runtime (ART), reads the same files and compiles them to
machine code on the device.
DEX files travel inside .apk packages
as classes.dex, with classes2.dex and so on when an app
refers to more than 65,536 methods, the most one file can index.
Structure
A DEX file starts with a 112-byte header (120 bytes from version 041). Its first
eight bytes are the magic dex\n and a three-digit version followed by a
zero byte, such as 035 or 039. Then come an Adler-32 checksum and a SHA-1
signature of the rest of the file, the file size, the header size, and an endian tag,
0x12345678, written little-endian.
The rest of the header gives the size and offset of each table. All the classes in the file share these tables, which is what makes a DEX file smaller than the class files it came from: strings, types, method prototypes, fields and methods are each stored once and referred to by index. A table of class definitions follows; each points into the data area, which holds the classes’ fields and methods, the bytecode, the strings and the annotations, and a map list that indexes every section.
Every field of the header, the versions, the data types and the map list are on the header and map page. The string, type, prototype, field, method and class tables, with the access flags, are on ID tables; class data, code items, exception handlers and debug information on class data and code; encoded values and annotations on values and annotations; and every Dalvik opcode and instruction format on instructions.
0x00magicdex\nand version, such as035\08 bytes0x08checksum (Adler-32) and signature (SHA-1)24 bytes0x20file_size, header_size, endian_tag12 bytes0x2Clink size and offset, map_off12 bytes0x38size and offset of string_ids, type_ids, proto_ids, field_ids, method_ids, class_defs and data56 bytes0x70ID tables, then class definitionsvaries…data: strings, code, annotations and the map listvaries
Not described
Unknown
- Reference: the base of branch offsets and the alignment of the hidden-API data are not stated in Android’s dex-format document; the pages say so.
Known, not yet covered
- Viewer: versions 035 to 041 are read: every section and item down to the byte, the disassembly of every method with its line numbers, the debug info, annotations, encoded arrays, call sites, method handles, hidden-API flags, and the checksum and SHA-1 checked. The container of version 041 (several logical files in one) is read but has not been tried on a real container file.
- Viewer: checks that ART’s
dex_file_verifier.ccmakes and the viewer does not: the type of each static field’s initial value against the field’s type, that a virtual method’s index differs from every direct method’s, that the members of aclass_data_item, annotations directory and its sets all belong to the class that owns them, the address and line rules of the debug-info state machine (addresses only increase, lines stay at 1 or above), that the number of debug-info parameter names fits the prototype, and that an exception handler address starts an instruction. ART’s class verifier (the types in each register, whether a branch lands on an instruction start) is a different step from the file verifier and is not done. The system annotations (Signature,InnerClass,EnclosingMethodand the rest) show as annotations, and their rules are not checked. - Viewer: Compact DEX (
cdex), and the dex files inside ODEX, VDEX and OAT files, are not read. A hidden-API flag value is named when it is one of 0 to 6, and any other value is shown as unknown.
Links
Software
- Android Studio – Google’s Android development environment, whose APK Analyzer shows the classes in a DEX file (Windows, macOS, Linux)
- jadx – decompiles DEX to Java (Windows, macOS, Linux)
- Apktool – unpacks and rebuilds APKs, turning DEX into smali (Windows, macOS, Linux)
- smali and baksmali – an assembler and disassembler for Dalvik bytecode (any system with Java)
- Ghidra – the NSA’s free reverse engineering suite (Windows, macOS, Linux)
Developers
- Androguard – reads DEX files and APKs (Python)
- dexlib2 – the library inside smali that reads and writes DEX files (Java)
Reference
- Dalvik executable format – the Android Open Source Project’s specification