18

我习惯于aapt p打包资源并生成R.java.

但是当我升级到 Android 24 时,我发现aapt2.exe.

我应该使用aapt2.exe吗?我该如何使用它?我找不到任何关于它的文档。

4

3 回答 3

30

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
于 2017-10-30T14:16:38.883 回答
10

注意:将此视为伊莎贝拉答案的补充

Android O 中的 Overlay Manager Service 的作者在他们的幻灯片中展示了他们的工作并讨论了 AAPT2 (有关上下文,请参见幻灯片 12-14)。这个工具的官方文档现在在这里(礼貌@Shrijana 的回答

此外,如有疑问,请查看源代码:https ://android.googlesource.com/platform/frameworks/base/+/android-7.0.0_r7/tools/aapt2 。

于 2018-02-15T23:16:26.837 回答
3

除了上面给出的答案,AAPT2 的文档终于出来了。您可以在此处找到文档。如果您发现任何错误,请针对这些错误报告错误。

于 2018-08-13T04:10:45.900 回答