Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Format:DEX

From ReverseEngineering.wiki
Format
Platforms Android (Dalvik, ART)
Magic 64 65 78 0A 30 33 35 00 ("dex\n035\0")
Extensions .dex, classes.dex
Endianness little (endian_tag 0x12345678)
On this wiki Facts · Code · Notes · Topics

Dalvik Executable, the bytecode container of Android apps (classes.dex, classes2.dex ... in an APK): magic "dex\n035\0" (version 035, 037, 038, 039 ...), a 0x70-byte little-endian header with an Adler-32 checksum and SHA-1 signature, then index tables (strings, types, prototypes, fields, methods, class definitions) and a map list. DEX is the code container of Platform:Android apps.

Header (0x70 bytes)

magic[8] (dex\n + three ASCII digits + NUL), checksum u32 (Adler-32 of everything after this field), signature[20] (SHA-1 of everything after this field), file_size, header_size (0x70), endian_tag (0x12345678; 0x78563412 means byte-swapped, not used in practice), link_size, link_off, map_off, then (size, off) pairs for string_ids, type_ids, proto_ids, field_ids, method_ids, class_defs, and data_size, data_off.

Versions

The three digits are the format version. ART's StandardDexFile accepts six: 035, 037, 038, 039, 040, 041 (version 036 is deliberately skipped because of an old Dalvik bug that accepted 036 files). Per the comments in ART standard_dex_file.cc: 038 is Android O and beyond (adds invoke-polymorphic and invoke-custom), 039 Android P and beyond (const-method-handle, const-method-type), 040 Android Q (Android 10) and beyond, 041 Android V (Android 15) and beyond. 035 is the original; 037 first supports default and static interface methods (kDefaultMethodsVersion = 37, Android 7.0; also the first version enforcing class definition ordering). See Fact:DEX/enum/Standard DEX versions accepted by ART.

Version 041 (DEX container). The header grows from 0x70 to 0x78 bytes: container_size and header_offset follow data_off, so several DEX files can sit in one container, each header pointing at its own offset; data_off/data_size are then 0 (Fact:DEX/struct/DEX header version 41 container fields). A parser must check header_size (0x70 or 0x78) instead of assuming 0x70.

Compact DEX. ART also has an internal variant with magic cdex + 001\0 (Fact:DEX/magic/Compact DEX cdex magic): the header gains feature_flags and data_off/data_size, and code items use a preheader with flag bits for the register, in, out, try and instruction counts. Unverified: which Android releases actually write cdex files (the source read only shows the class exists); tools such as jadx and dexdump may not read it, so extract standard DEX from the VDEX/OAT first.

Unverified: d8/R8 choose the DEX version from the min-api setting; the marker string tells which tool wrote the file (Tool:R8).

Index tables

  • string_ids: u32 offset to string_data_item (ULEB128 UTF-16 length, then MUTF-8 bytes, NUL terminated). Sorted by code point.
  • type_ids: u32 string index of the descriptor (Ljava/lang/String;).
  • proto_ids (12 bytes): shorty_idx, return_type_idx, parameters_off.
  • field_ids (8 bytes): class_idx u16, type_idx u16, name_idx u32.
  • method_ids (8 bytes): class_idx u16, proto_idx u16, name_idx u32.
  • class_defs (32 bytes): class_idx, access_flags, superclass_idx, interfaces_off, source_file_idx, annotations_off, class_data_off, static_values_off. NO_INDEX is 0xFFFFFFFF.
  • code_item (16-byte header): registers_size, ins_size, outs_size, tries_size (u16 each), debug_info_off, insns_size (in 16-bit units), then the instructions.

Map list

At map_off: u32 size, then 12-byte items {type u16, unused u16, size u32, offset u32}. Types: 0x0000 header, 0x0001 string_id, 0x0002 type_id, 0x0003 proto_id, 0x0004 field_id, 0x0005 method_id, 0x0006 class_def, 0x0007 call_site_id, 0x0008 method_handle, 0x1000 map_list, 0x1001 type_list, 0x1002 annotation_set_ref_list, 0x1003 annotation_set, 0x2000 class_data, 0x2001 code, 0x2002 string_data, 0x2003 debug_info, 0x2004 annotation, 0x2005 encoded_array, 0x2006 annotations_directory.

Type 0xF000 is kDexTypeHiddenapiClassData (see #Hidden API flags). Source: ART dex_file.h lines 194-214 (types listed above match, with 0x0007 call_site_id and 0x0008 method_handle).

Reversing notes

Method and field indices in instructions refer to the method_ids/field_ids tables; names come from the string pool, so obfuscators (ProGuard/R8) rename strings but keep the structure. After install, ART compiles the DEX into Format:OAT (an ELF) with a Format:VDEX verifier cache. After patching a DEX, recompute the SHA-1 signature first (over bytes 32 to the end, stored at offset 12), then the Adler-32 checksum (over bytes 12 to the end, stored at offset 8), because the checksum covers the signature field. Libraries: LIEF (Apache-2.0) parses DEX, OAT, VDEX and ART.

Decompilers

See Technique:Choosing a decompiler; Tool:JADX reads APK, dex, aab and smali and decodes resources.

Hidden API flags

Android's non-SDK-interface restrictions are stored in the DEX itself, not in the app: platform DEX files (boot class path jars, framework) carry an extra map item of type 0xF000 hiddenapi_class_data. Layout: u32 size, then one u32 offset per class def (from the start of the item; 0 means the class has no class data), each pointing at a ULEB128 stream with one flag per field and method in class_data order (Fact:DEX/struct/HiddenapiClassData item). The low 4 bits of a flag are the list: 0 sdk, 1 unsupported, 2 blocked, 3 to 7 max-target-O/P/Q/R/S; 0xF invalid; bit 4 marks core-platform API and bit 5 test API (Fact:DEX/enum/Hidden API flag values). A max-target-X member stays reachable for apps whose targetSdkVersion is X's release or lower. Source: AOSP libartbase/base/hiddenapi_flags.h and libdexfile/dex/dex_file_structs.h. App DEX files normally have no such section. Unverified: the exact value that newer Android releases add after S.

See also

Code & repositories

  • https://github.com/lief-project/LIEF @ 2795838f5f33efbc10f071d194c44197a86a36d8 · license: Apache-2.0 (declared by the contributor for the files used; GitHub reports Apache-2.0 (read from LICENSE; GitHub's API was busy) for the repository)
    LIEF: parser and rewriter for ELF, PE, Mach-O, DEX, OAT, VDEX and ART; src/DEX, src/OAT, src/VDEX, src/ART Structures.hpp give the header layouts per Android release
    Tested on: source read, not run: source read only · checked 2026-10-18
  • https://github.com/skylot/jadx @ c4c45fde27b4f3386dc2ff016ff187229a60a716 · license: Apache-2.0 (read from LICENSE; GitHub's API was busy)
    JADX dex to Java decompiler
    Tested on: source read, not run: not run; README read · checked 2026-10-18