18

I have used aapt p to package resources and generate R.java.

But when I upgraded to Android 24, I found aapt2.exe.

Should I use aapt2.exe? How do I use it? I could not find any documentation about it.

Generic Bot
  • 309
  • 1
  • 4
  • 8
chenie
  • 195
  • 1
  • 1
  • 5
  • 1
    In your gradle.properties file, you add the following line: `android.enableAapt2=true` [Source](https://developer.android.com/studio/preview/features/new-android-plugin.html) – afathman May 18 '17 at 20:02
  • The option `android.enableAapt2` is deprecated and should not be used anymore. Use `android.enableAapt2=true` to remove this warning. **It will be removed at the end of 2018.** – Non Sensei Apr 20 '18 at 08:26

3 Answers3

31

There are some big differences between how AAPT and AAPT2 work.

Compile and link

The main idea behind AAPT2, apart from new features, is that it divides the 'package' step into two: 'compile' and 'link'. It improves performance, since if only one file changes, you only need to recompile that one file and link all the intermediate files with the 'link' command.

More restrictive

AAPT2 tries to catch most bugs as early as possible. That's why when switching from AAPT to AAPT2 you might encounter many errors stating that some elements are nested incorrectly or that some references are incorrect. For more info on the new restrictiveness look at Android Studio 3.0 documentation.

Usage

Android Studio 3.0 comes with AAPT2 enabled by default (with Android Gradle Plugin 3.0.0). But if you want to use AAPT2 in your own script, you will need to change the way you process your resources. For the 'package' command with AAPT you would pass the resource directory with the -S. With AAPT2 you need to compile each resource first with the 'compile command and only then pass all the compiled files with the -R flag.
For example:

aapt package -S app/src/main/res/ ...

Instead use:

aapt2 compile -o compiled/res/ app/src/main/res/values/values.xml
aapt2 compile -o compiled/res/ app/src/main/res/drawable/myImage.png --no-crunch
...
aapt2 link -R compiled/res/values_values.arsc.flat -R compiled/res/drawable_myImage.flat ...

Flags

There are more differences in flag usage, for example the both '--pseudo-localize' and '--no-crunch' flags are used per file during the 'compile' step. For full information about AAPT2 flags type:

aapt2 compile -h
aapt2 link -h
Izabela Orlowska
  • 7,431
  • 2
  • 20
  • 33
  • 1
    I tried the way you mentioned. But I still get errors like `error: resource mipmap/ic_launcher (aka packageNamet:mipmap/ic_launcher) not found`. etc. Even though those files are present in the compiled res folder. – jgm Nov 30 '17 at 15:41
  • `aapt2 link --manifest /home/test/src/main/AndroidManifest.xml -I /home/Android/Sdk/platforms/android-27/android.jar -R /home/test/build/intermediates/res/merged/debug/* -o file.zip` This is the command that I am using. res/merged/debug folder already contains all the flat files created by Android Studio build process. I am just trying to package as zip file. Am I doing something wrong ? @izabela-orlowska – jgm Nov 30 '17 at 22:25
  • 1
    Hm, it looks correct from what I can see. Could you provide a repro case? Perhaps you're missing some dependencies. AAPT2 is much stricter when it comes to resolving resources than AAPT, which would let more things slide but that resulted in possible runtime issues. Also, if these are created by the gradle build from Android Studio, does the task processDevDebugResources complete successfully? If so, you can see the full command with which gradle called AAPT2 by adding the --debug flag to the gradle command. @jgm – Izabela Orlowska Dec 01 '17 at 22:37
  • How do you invoke this from gradle? I need to post-process classfiles after they have been scanned, to produce a raw resource, and then I need to invoke aapt2 to convert the resource into a .flat file (since aapt2 has already been run on other resources). I can't figure out how to invoke aapt2 in a gradle rule. – Luke Hutchison Oct 11 '18 at 04:02
  • @LukeHutchison You can add the files as an input to the merge resources task. – Izabela Orlowska Oct 14 '18 at 10:43
  • @IzabelaOrlowska this is what I came up with -- is this the right or the best way to do this? https://github.com/classgraph/classgraph/wiki/Build-Time-Scanning#gradle-code – Luke Hutchison Oct 14 '18 at 12:30
10

Note: treat this as an addition to Izabela's answer

The authors of the Overlay Manager Service in Android O presented their work and talk about AAPT2 in their slides (see slides 12-14 for context). The official documentation for this tool is now here (courtesy @Shrijana's answer)

Also, when in doubt, look at the source code: https://android.googlesource.com/platform/frameworks/base/+/android-7.0.0_r7/tools/aapt2.

scorpiodawg
  • 5,612
  • 3
  • 42
  • 62
3

In addition to the above given answers, the documentation for AAPT2 is finally out. You can find the documentation here. If you find any errors, please report a bug against those errors.