Android/Gradle错误-error: < item> inner element must either be a resource reference or empty.

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 an attr 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

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

seesaw_swing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值