Mobile Development 25 min read

Understanding AAPT2: Android Resource Compilation and the FLAT File Format

AAPT2, the default Android resource compiler, splits resource processing into compile and link stages, converting XML, PNG, and other files into binary FLAT containers that embed both raw data and metadata, enabling incremental builds, configurable options, and efficient merging into the final APK’s resources.arsc and R.java.

vivo Internet Technology
vivo Internet Technology
vivo Internet Technology
Understanding AAPT2: Android Resource Compilation and the FLAT File Format

AAPT2 is the default Android resource compiler introduced in Android Studio 3.0, replacing the older AAPT. It splits the resource processing into two stages: compile and link. During the compile phase, individual resource files (XML, PNG, etc.) are converted into binary FLAT files that contain both the original resource data and metadata such as file path, type, and configuration. This enables incremental builds because only changed resources need recompilation.

The compile command is invoked via aapt2 compile path-to-input-files [options] -o output-directory/ . It accepts options like --dir for scanning a directory, --zip for a ZIP archive, -o for output path, and switches such as --no-crunch (disable PNG processing), --legacy (treat old errors as warnings), and -v (verbose logging).

Internally, the compile workflow loads input resources via a file collection (directory or ZIP), determines the output archive type (directory or ZIP), and then processes each resource through one of four compile functions: CompileFile (raw copy), CompileTable (values XML → ARSC), CompileXml (general XML → protobuf), and CompilePng (PNG optimization and 9‑patch handling). Each function builds a ResourceFile or XmlResource object, serializes it to protobuf, and writes the header and data using WriteHeaderAndDataToWriter , which wraps an IArchiveWriter with a ContainerWriter to produce the FLAT container format.

The FLAT file format begins with a magic number ( 0x54504141 ), version, and entry count, followed for each entry by a header (resource type, sizes) and the protobuf‑serialized resource metadata plus the raw resource data. For XML resources, the DOM tree is flattened into XmlNode protobuf; for values resources, a ResourceTable protobuf is used; for PNGs, optional crunching and nine‑patch border removal are applied before writing.

By encoding both metadata and raw content, FLAT files allow the subsequent link step to merge them into the final APK’s resources.arsc and R.java, while supporting fast incremental builds and smaller binary resource blobs.

incremental buildandroidBuild Toolsresource compilationAAPT2FLAT file
vivo Internet Technology
Written by

vivo Internet Technology

Sharing practical vivo Internet technology insights and salon events, plus the latest industry news and hot conferences.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.