Official is introduced: the developer. The android. Google. Cn/studio/comm…

AAPT2 (Android Resource Packaging Tool) is a build tool that is used by The Android Studio and Android Gradle plug-ins to compile and package an application’s resources. AAPT2 parse resources, index them, and compile them into binary formats optimized for the Android platform.

The Android Gradle plugin 3.0.0 and later enables AAPT2, AAPT2 addresses (/sdk_path/build-tools/version/ AAPT2) by default.

AAPT2 support faster resource compilation by enabling incremental compilation. This is achieved by splitting resource processing into two steps:

  • Compile: Compiles resource files to binary format.
  • Link: Merge all compiled files and package them into a package.

This split approach helps improve incremental compilation performance. For example, if a file changes, you only need to recompile the file.

compile

AAPT2 supports compiling all Android resource types, such as drawable objects and XML files. When AAPT2 is called for compilation, each call should pass a resource file as input. AAPT2 then parses this file and generates an intermediate binary with an extension of.flat.

The input The output
XML resource files (such as String and Style) in the res/values/ directory. Resource table with *.arsc.flat as extension.
All other resource files. All files except those in the res/values/ directory will be converted to binary XML files with the *.flat extension. Also, by default, all PNG files are compressed and have a *.png. Flat extension. If you choose not to compress PNG, you can use the — no-Crunch option during compilation.

The files AAPT2 output are not executable files, and you must add these binaries as input later in the link phase to generate the APK. However, the generated APK file is not an executable that can be deployed immediately on an Android device, because it contains no DEX file (compiled bytecode) and is unsigned.

Compile syntax: compile

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

Path-to-input-files must comply with path/resource-type[-config]/file.

  • Decompile\aapt\colors. XML: Decompile\aapt\colors. XML: Decompile\aapt\colors.
  • Correct examples: G: Decompile\aapt\res\values\colors.xml
.\aapt2.exe compile G:\Decompile\aapt\res\values\colors.xml -o G:\Decompile\aapt\
Copy the code

The values_colors.arsc.flat file is generated in the directory folder.

Compiler options

options instructions
-o path Specifies the output path of the compiled resource. This is a required flag because you must specify the path to the directory where AAPT2 can output and store the compiled resources.
–dir directory Specifies the directory in which to search for resources. Although you can use this flag to compile multiple resource files with a single command, you don’t get the benefit of incremental compilation, so it’s not recommended for large projects.
–pseudo-localize Generate pseudo-localized versions of default strings, such as EN-xa and EN-xb.
–no-crunch PNG processing is disabled.
–legacy Consider errors allowed when using earlier versions of AAPT as warnings.
-v Enable detailed logging.

link

During the linking phase, AAPT2 combine all the intermediate files (such as resource tables, binary XML files, and processed PNG files) generated during compilation and package them into an APK. In addition, other auxiliary files such as R. Java and ProGuard rule files are generated at this stage. However, the resulting APK does not contain DEX bytecode and is unsigned.

Link syntax: link

aapt2 link path-to-input-files [options] -o outputdirectory/outputfilename.apk --manifest AndroidManifest.xml
Copy the code

The sample

aapt2 link -o output.apk -I android_sdk/platforms/android_version/android.jar compiled/res/values_values.arsc.flat compiled/res/drawable_Image.flat --manifest /path/to/AndroidManifest.xml -v
Copy the code

Link options

options instructions
-o path Specifies the output path of the linked resource APK. Must be specified.
–manifest file Specify the path to the Android manifest file to build. Must be specified.
-I You must use this flag if you want to use an attribute with an Android namespace (for example, Android: ID) in a resource file.
–java directory Specify the directory in which R.Java will be generated.
–min-sdk-version min-sdk-version Set the default minimum SDK version to be used for Androidmanifest.xml.
–target-sdk-version target-sdk-version Set the default target SDK version to be used for Androidmanifest.xml.
–version-code version-code Specifies the version code in androidmanifest.xml to be injected when there is no version code (integer)
–compile-sdk-version-name compile-sdk-version-name Specifies the version name to be injected in androidmanifest.xml when there is no version name.
-v Can increase the level of detail of the output

The dump

Dump is used to output resource and manifest information about the APK generated from the link command. Use dump to output information to the console.

Dump the grammar

aapt2 dump [subcommand] filename.apk [options]
Copy the code

subcommand

options instructions
apc Print the contents of the AAPT2 Container (APC) generated fom compilation.
badging Prints the information extracted from the APK listing
configurations The configuration information used by APK is displayed
packagename Output package name
permissions The permission information used in APK is displayed
strings Enter information about the resource table constant pool
styleparents Print the parents of a style in an APK.
resources Output information about the resource table
xmlstrings Output compiled constant pool
xmltree Enter the compiled XML tree

Dump options

options instructions
–no-values Disallow output values while displaying resources.
–file file Specify the file as a parameter to dump from APK.
-v Increase the level of detail in the output.

At the end

Android APK file (1) Compilation and packaging process

Android APK file (2) decompression and decompilation

Android APK file (III. AAPT2 tool use)

Android APK file (4)