AAPT: error: invalid resource type 'attr' for parent of style, after updating gradle

147 Views Asked by At

After upgrading from Gradle wrapper 4.4.1 to 6.1.1, along with going from Android build tools 2.3.0 to 4.0.0, builds all of a sudden fail with the following error:

res/values/treeview_styles.xml:3:5-6:13: AAPT: error: invalid resource type 'attr' for parent of style.

The resource file in question looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style parent="@android:attr/listViewStyle" name="treeViewListStyle">
        <item name="android:background">@android:color/white</item>
        <item name="android:divider">@drawable/divider</item>
    </style>
</resources>

What is happening here, and how can I fix this?


Background information: The code comes from a tree list view control I am using in my app. I have simply copied the source tree into mine so that I could make some modifications.

The developer reference for android.R.attr mentions listViewStyle, stating it has been available since API 1, so I am wondering if it is a reference to that. On the other hand, my app uses appcompat-v7, and in my local SDK tree at extras/android/support/v7/appcompat/res/values/themes_base.xml I indeed found:

<style name="Base.Theme.AppCompat.CompactMenu" parent="">
    <item name="android:itemTextAppearance">?android:attr/textAppearanceMedium</item>
    <item name="android:listViewStyle">@style/Widget.AppCompat.ListView.Menu</item>
    <item name="android:windowAnimationStyle">@style/Animation.AppCompat.DropDownUp</item>
</style>

That looks as if android:listViewStyle is just an alias for @style/Widget.AppCompat.ListView.Menu. Indeed, using @style/Widget.AppCompat.ListView.Menu compiles and the UI looks OK at first glance.

Note that the toolchain upgrade included switching from AAPT to AAPT2. According to this question and its answers, AAPT2 is stricter about some things than its predecessor. In particular, parents of styles must be styles as well. That would at least explain the error message, we just need to hunt down the correct resource name.

1

There are 1 best solutions below

0
Evgenue Ozhgikihin On

ianhanniballake wrote:

The problem is parent="@android:attr/listViewStyle", so replacing that with an actual parent theme, and not something from an attribute, would indeed fix it. What theme are you using?

In addition i found that:

Behavior changes when using AAPT2:

Additionally, when declaring a 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