Android/Gradle编译错误:
在升级老android app时,遇到一个奇怪的gradle编译问题,错误日志中仅仅提示了出错原因,没有给出行数;
error: <item> inner element must either be a resource reference or empty.
发现原来是AAPT2 加强了对资源文件的检查,导致资源文件过时;使用android SDK中aapt2 Daemon mode可以检查资源文件;
aapt2 daemon &
aapt2 compile ./app/src/main/res/values/ids.xml -o ./tmp/
./app/src/main/res/values/ids.xml:23: error: <item> inner element must either be a resource reference or empty.
<!- error line-->
<item type="id" name="split">false</item>
<!- correct to-->
<item type="id" name="split" />
官方文档也列举了其他AAPT2可能出现的问题(https://developer.android.com/tools/aapt2):
Declaration of resources
You can no longer indicate the type of a resource from the
name
attribute. The following example incorrectly declares anattr
resource item:<style name="childStyle" parent="parentStyle"> <item name="attr/my_attr">@color/pink</item> </style>
Declaring a resource type this way results in the following build error:
Error: style attribute 'attr/attr/my_attr (aka my.package:attr/attr/my_attr)' not found.
To resolve this error, explicitly declare the type using
type="attr"
:<style name="childStyle" parent="parentStyle"> <item type="attr" name="my_attr">@color/pink</item> </style>
Additionally, when declaring a
<style>
element, its parent must also be a style resource type. Otherwise, you get an error similar to the following:Error: (...) invalid resource type 'attr' for parent of style
Incorrect use of @ resource reference symbols
AAPT2 throws build errors when you omit or incorrectly place resource reference symbols (
@
). For example, if you omit the symbol when specifying a style attribute:<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> ... <!-- Note the missing '@' symbol when specifying the resource type. --> <item name="colorPrimary">color/colorPrimary</item> </style>
When building the module, AAPT2 throws the following build error:
ERROR: expected color but got (raw string) color/colorPrimary
Additionally, if you incorrectly include the symbol when accessing a resource from the
android
namespace:... <!-- When referencing resources from the 'android' namespace, omit the '@' symbol. --> <item name="@android:windowEnterAnimation"/>
When building the module, AAPT2 throws the following build error:
Error: style attribute '@android:attr/windowEnterAnimation' not found