AAPT2

    Android Gradle Plugin 3.0.0 and higher enable AAPT2 by default, and you typicallywon't need to invoke yourself. However, if you prefer to use yourterminal and your own build system over Android Studio, you can use AAPT2 fromthe 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 26.0.2 and higher.

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

    Once you have downloaded the SDK Build Tools, you can find AAPT2 located inandroid_sdk/build-tools/version/.Because a newer revision of the Android SDK Build Tools is not released veryoften, the version of AAPT2 included in your SDK Build Tools might not be thelatest. To get the latest version of AAPT2, read .

    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 ofresources by enabling incremental compilation. This is accomplished by breakingresource processing into two steps:

    • Compile: compiles resource files into binary formats.
    • : merges all compiled files and packages them to a single package.
      This division helps improve performance for incremental builds. For example, ifthere are changes in a single file, you need to recompile only that file.

    To get the newest version of AAPT2 that's not bundled in the build-tools,you can download AAPT2 from Google's Maven repository as follows:

    • Navigate to com.android.tools.build > aapt2 in therepository index.
    • Copy the name of the latest version of AAPT2.
    • Insert the version name you copied into the following URL and specify yourtarget operating system: | linux | osx]_.jar

    For example, to download version 3.2.0-alpha18-4804415 for Windows, youwould 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.

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

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

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

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

    The general syntax for using compile is as follows:

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

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

    1. aapt2 compile project_root/module_root/src/main/res/values-en/
    2. strings.xml -o compiled/
    3. aapt2 compile project_root/module_root/src/main/res/drawable
    4. /myImage.png -o compiled/

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

    Compile options

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

    In the link phase, AAPT2 merges all the intermediate files generated from thecompilation phase such as resource tables, binary XML files, and processed PNGfiles and packages them into a single APK. Additionally, other auxiliary fileslike 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 AndroidGradle Plugin to build your app from the command line,you can use other command line tools, such as tocompile Java bytecode into DEX bytecode and apksignerto sign your APK.

    Link syntax

    The general syntax for using link is as follows:

    1. aapt2 link path-to-input-files [options] -o
    2. outputdirectory/outputfilename.apk --manifest AndroidManifest.xml

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

    1. aapt2 link -o output.apk
    2. -I android_sdk/platforms/android_version/android.jar
    3. compiled/res/values_values.arsc.flat
    4. compiled/res/drawable_Image.flat --manifest /path/to/AndroidManifest.xml -v

    You can use the following options with the link command:

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

    1. aapt2 dump output.apk

    Dump syntax

    The general syntax for using dump is as follows:

    Dump options

    You can use the following options with dump:

    Prior to AAPT2, AAPT was the default version of Android Asset Packaging Tooland it has now been deprecated. Although AAPT2 should immediately work witholder projects, this section describes some behavior changes that you should beaware of.

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

    1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    2. package="com.example.myname.myapplication">
    3. <application
    4. ...
    5. <activity android:name=".MainActivity">
    6. <intent-filter>
    7. <action android:name="android.intent.action.MAIN" />
    8. <category android:name="android.intent.category.LAUNCHER" />
    9. </intent-filter>
    10. <action android:name="android.intent.action.CUSTOM" />
    11. </application>
    12. </manifest>

    Previous versions of AAPT would simply ignore the misplaced <action> tag.However, with AAPT2, you get the following error:

    1. AndroidManifest.xml:15: error: unknown element <action> found.

    Declaration of resources

    You can no longer indicate the type of a resource from the name attribute.For example, the following sample incorrectly declares an attr resource item:

    1. <style name="foo" parent="bar">
    2. <item name="attr/my_attr">@color/pink</item>
    3. </style>

    Declaring a resource type this way results in the following build error:

    1. Error: style attribute 'attr/attr/my_attr (aka my.package:attr/attr/my_attr)'
    2. not found.

    To resolve this error, explicitly declare the type using type="attr":

    1. <style name="foo" parent="bar">
    2. <item type="attr" name="my_attr">@color/pink</item>
    3. </style>

    Additionally, when declaring a <style> element, its parent must also bestyle resource type. Otherwise, you get an error similar to the following:

    Android namespace with ForegroundLinearLayout

    includes threeattributes:foregroundInsidePadding,, andandroid:foregroundGravity.Note that foregroundInsidePadding is not included in the android namespace,unlike the other two attributes.

    In previous versions of AAPT, the compiler would silently ignoreforegroundInsidePadding attributes when you define it with the androidnamespace. When using AAPT2, the compiler catches this early and throws thefollowing build error:

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

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

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

    1. <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    2. ...
    3. <!-- Note the missing '@' symbol when specifying the resource type. -->
    4. <item name="colorPrimary">color/colorPrimary</item>
    5. </style>

    When building the module, AAPT2 throws the following build error:

    1. ERROR: expected color but got (raw string) color/colorPrimary

    Additionally, consider if you incorrectly include the symbol when accessing aresource from the android namespace, as shown below:

    1. ...
    2. <!-- When referencing resources from the 'android' namespace, omit the '@' symbol. -->
    3. <item name="@android:windowEnterAnimation"/>

    When building the module, AAPT2 throws the following build error:

    1. Error: style attribute '@android:attr/windowEnterAnimation' not found

    Incorrect configuration of libraries

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

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