AAPT2

AAPT2 (Android Asset Packaging Tool) is a build tool that Android Studio and
Android Gradle Plugin use to compile and package your app’s resources.
AAPT2 parses, indexes, and compiles the resources into a binary format that is
optimized for the Android platform.

Android Gradle Plugin 3.0.0 and higher enable AAPT2 by default, and you typically
won't need to invoke aapt2 yourself. However, if you prefer to use your
terminal and your own build system over Android Studio, you can use AAPT2 from
the command line. You can also debug build errors related to AAPT2 from the command line.
To do so, you can find AAPT2 as a standalone tool in Android SDK Build Tools 26.0.2 and higher.

To download Android SDK Build Tools from the command line, use sdkmanager and run the command:

sdkmanager "build-tools;build-tools-version"

Once you have downloaded the SDK Build Tools, you can find AAPT2 located in
android_sdk/build-tools/version/.
Because a newer revision of the Android SDK Build Tools is not released very
often, the version of AAPT2 included in your SDK Build Tools might not be the
latest. To get the latest version of AAPT2, read Download AAPT2 from Google Maven.

To use AAPT2 from the command line on Linux or Mac, run the aapt2 command.
On Windows, run the aapt2.exe command. AAPT2 supports faster compilation of
resources by enabling incremental compilation. This is accomplished by breaking
resource processing into two steps:

Insert the version name you copied into the following URL and specify your
target operating system:
https://dl.google.com/dl/android/maven2/com/android/tools/build/aapt2/aapt2-version/aapt2-aapt2-version-[windows | linux | osx].jar

For example, to download version 3.2.0-alpha18-4804415 for Windows, you
would use:
https://dl.google.com/dl/android/maven2/com/android/tools/build/aapt2/3.2.0-alpha18-4804415/aapt2-3.2.0-alpha18-4804415-windows.jar

Navigate to the URL in a browser—AAPT2 should begin downloading shortly.

Unpackage the JAR file you just downloaded.
The JAR file should contain anaapt2 executable and some libraries that the
executable depends on.

Compile

AAPT2 supports compilation of all Android resource types,
such as drawables and XML files. When you invoke AAPT2 for compilation, you
should pass a single resource file as an input per invocation. AAPT2 then parses
the file and generates an intermediate binary file with a .flat extension.

Although you can pass resource directories containing more than one resource
files to AAPT2 using the --dir flag, you do not gain the benefits of
incremental resource compilation when doing so. That is, when passing whole
directories, AAPT2 recompiles all files in the directory even when only one
resource has changed.

The output file types can differ based on the input you provide for compilation.
This is explained in the table below:

Input

Output

XML resource files, such as
String and Style,
which are located in the res/values/ directory.

Resource table with *.asrc.flat as its extension.

All other resource files.

All files other than the files under res/values/ directory are
converted to binary XML files with *.flat extensions.
Additionally all PNG files are crunched by default and adopt *.png.flat
extensions. If you choose not to compress PNGs, you can use the
--no-crunch option during compilation.

The files AAPT2 outputs are not executables and you must later include these
binary files as input in the link phase to generate an APK. However, the
generated APK file is not an executable that you can deploy on an Android
device right away, as it does not contain DEX files (compiled bytecode) and is
not signed.

Compile syntax

The general syntax for using compile is as follows:

aapt2 compile path-to-input-files [options] -o output-directory/

Note: For resource files, the path to input files must match the following
structure:
path/resource-type[-config]/file

In the following example, AAPT2 compiles resource files named values.xml and
myImage.png individually:

As shown in the table above, the name of the output file depends on the input
file name and the name of its parent directory (the resource type and
configuration). For the example above with strings.xml as input, aapt2
automatically names the output file as values-en_strings.arsc.flat.
On the other hand, the file name for the compiled drawable file stored in
the drawable directory will be drawable_img.png.flat.

Compile options

There are several options that you can use with the compile command, as shown
in the table below:

Option

Description

-o path

Specifies the output path for the compiled resource(s).

This is a required flag because you must specify a path to a
directory where AAPT2 can output and store the compiled resources.

--dir directory

Specifies the directory to scan for resources.

Although you can use this flag to compile multiple resource files
with one command, it disables the benefits of incremental compilation and
thus, should not be used for large projects.

--pseudo-localize

Generates pseudo-localized
versions of default strings, such as en-XA and en-XB.

--no-crunch

Disables PNG processing.

Use this option if you have already processed the PNG files, or if you
are creating debug builds that do not require file size reduction.
Enabling this option results in a faster execution, but increases the output
file size.

--legacy

Treats errors that are permissible when using earlier versions of AAPT as
warnings.

This flag should be used for unexpected compile time errors. To resolve
known behavior changes that you might get while using AAPT2, read
Behavior changes in AAPT2.

-v

Enable verbose logging.

Link

In the link phase, AAPT2 merges all the intermediate files generated from the
compilation phase such as resource tables, binary XML files, and processed PNG
files and packages them into a single APK. Additionally, other auxiliary files
like R.java and ProGuard rules files can be generated during this phase.
However, the generated APK does not contain DEX bytecode and is unsigned.
That is, you can't deploy this APK to a device. If you're not using the Android
Gradle Plugin to build your app from the command line,
you can use other command line tools, such as d8 to
compile Java bytecode into DEX bytecode and apksigner
to sign your APK.

Link syntax

In the following example, AAPT2 merges the two intermediate files -
drawable_Image.flat and values_values.arsc.flat, and the AndroidManifest.xml
file. AAPT2 links the result against android.jar file which holds the
resources defined in the android package:

Pass individual .flat file to link, using overlay semantics
without using the <add-resource> tag.
flag.

When you a provide a resource file that overlays (extends or modifies)
an existing file, the last conflicting resource given is used.

--package-id package-id

Specifies the package ID to use for your app.

The package ID that you specify must be greater than or equal to 0x7f
unless used in combination with --allow-reserved-package-id.

--allow-reserved-package-id

Allows the use of a reserved package ID.

Reserved package IDs are IDs that are normally assigned to shared
libraries and are in the range from 0x02 to 0x7e inclusive. By using
--allow-reserved-package-id, you can assign IDs that fall
in the range of reserved package IDs.

This should only be used for packages with a min-sdk version of 26 or lower.

--java directory

Specifies the directory in which to generate R.java.

--proguard proguard_options

Generates output file for ProGuard rules.

--proguard-conditional-keep-rules

Generates output file for ProGuard rules for the main dex.

--no-auto-version

Disables automatic style and layout SDK versioning.

--no-version-vectors

Disables automatic versioning of vector drawables.
Use this only when building your APK with the Vector Drawable Library.

--no-version-transitions

Disables automatic versioning of transition resources.
Use this only when building your APK with Transition Support library.

Enables encoding of sparse entries using a binary search tree.
This is useful for optimization of APK size, but at the cost of resource
retrieval performance.

-z

Requires localization of strings marked 'suggested'.

-c config

Provides a list of configurations separated by commas.

For example, if you have dependencies on the support library (which
contains translations for multiple languages), you can filter resources
just for the given language configuration, like English or Spanish.

You must define the language configuration by a two-letter ISO 639-1
language code, optionally followed by a two letter ISO 3166-1-alpha-2
region code preceded by lowercase 'r' (for example, en-rUS).

--preferred-density density

Allows AAPT2 to select the closest matching density and strip
out all others.

There are several pixel density qualifiers available to use in your app,
such as ldpi, hdpi, and xhdpi. When you specify a preferred density, AAPT2
selects and stores the closest matching density in the resource table and
removes all others.

Splits resources based on a set of configurations to generate a different
version of the APK.

You must specify the path to the output APK along with the set of
configurations.

-v

Enables increased verbosity of the output.

Dump

dump is used for printing resource and manifest information about the APK
generated from the link command. You can print the information to your console
using dumpas shown below:

aapt2 dump output.apk

Dump syntax

The general syntax for using dump is as follows:

aapt2 dump filename.apk[options]

Dump options

You can use the following options with dump:

Option

Description

--no-values

Suppresses the output of values when displaying resource.

--file file

Specifies a file as an argument to be dumped from the APK.

-v

Increases verbosity of the output.

Behavior changes when using AAPT2

Prior to AAPT2, AAPT was the default version of Android Asset Packaging Tool
and it has now been deprecated. Although AAPT2 should immediately work with
older projects, this section describes some behavior changes that you should be
aware of.

Element hierarchies in the Android manifest

In previous versions of AAPT, elements nested in incorrect nodes in the Android
manifest were either ignored or resulted in a warning. For example, consider the
following sample:

In previous versions of AAPT, the compiler would silently ignore
foregroundInsidePadding attributes when you define it with the android
namespace. When using AAPT2, the compiler catches this early and throws the
following build error:

Error: (...) resource android:attr/foregroundInsidePadding is private

To resolve this issue, simply replace android:foregroundInsidePadding with
foregroundInsidePadding.

Incorrect use of @ resource reference symbols

AAPT2 throws build errors when you omit or incorrectly place resource
reference symbols (@). For example, consider if you omit the symbol when
specifying a style attribute, as shown below:

Incorrect configuration of libraries

If your app has a dependency on a third party library that was built using older
versions of the Android SDK Build Tools, your
app might crash at runtime without displaying any errors or warnings. This crash
might occur because, during the library's creation, the R.java fields are
declared final and, as a result, all of the resource IDs are inlined in the
library's classes.

AAPT2 relies on being able to re-assign IDs to library resources when building
your app. If the library assumes the IDs to be final and has them inlined in
the library dex, there will be a runtime mismatch.

To resolve this error, contact the library author to rebuild the library
using the latest version of the Android SDK Build Tools and republish the library.

Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.