Search code examples
androidandroid-studioandroid-libraryaar

Can't find resource identifiers from AAR in XML


I developed a custom Preference for an application. It has two custom attributes. I'm trying to move the Preference to a library project to reuse in other projects. I moved the Java file along with the corresponding layout and attr XML files. Here's the attr file in the library project:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="NumberPickerPreference" >
        <attr name="minValue" format="integer" />
        <attr name="maxValue" format="integer" />
    </declare-styleable>
</resources>

Here's the manifest:

<manifest package="com.mydomain.androiduilibrary">
</manifest>

An odd-looking manifest for sure. When I generated the library project the only thing Android Studio put in the manifest was the SDK info, which then generated a warning about it already being specified in the build.gradle file, so I deleted the entry in the manifest.

I generate the AAR file using the bundleRelease task and get a file named androiduilibrary-release.aar. I then go to the project that needs to use this Preference and select "New Module | Import .JAR or .AAR Package", navigate to the AAR, and it's now in my project as a module entitled androiduilibrary-release. I make a module dependency in the Project Structure dialog, and can now reference the Preference in Java code. So far, so good.

However, when I attempt to reference the Preference in XML, I get the following error on my custom attributes: "Error:(9) No resource identifier found for attribute 'minValue' in package 'com.mydomain.androiduilibrary'". Here's the XML:

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:title="@string/location_service_settings_preference_header_title"
    xmlns:custom="http://schemas.android.com/apk/res/com.mydomain.androiduilibrary">

<com.mydomain.androiduilibrary.NumberPickerPreference
    android:title="@string/update_interval_preference_title"
    android:key="@string/update_interval_preference_key"
    android:defaultValue="@integer/update_interval_preference_default"
    android:summary="@string/update_interval_preference_summary"
    custom:minValue="@integer/update_interval_preference_min"
    custom:maxValue="@integer/update_interval_preference_max"
    android:dialogMessage="@string/update_interval_preference_dialog_message"
    android:dialogTitle="@string/update_interval_preference_title" />

I can look inside the AAR and see that the values are populating in R.txt:

int attr maxValue 0x7f010001
int attr minValue 0x7f010000

And that its manifest is being created correctly:

<?xml version="1.0" encoding="UTF-8"?>

<manifest android:versionName="1.0" android:versionCode="1"
    xmlns:android="http://schemas.android.com/apk/res/android" package="com.mydomain.androiduilibrary">

    <uses-sdk android:targetSdkVersion="21" android:minSdkVersion="11"/>

</manifest>

I also get a warning when calling out the custom preference: "Element com.mydomain.androiduilibrary.NumberPickerPreference is not allowed here" so I'm guessing that will be a problem too.

Is there a step I'm missing that will allow me to reference elements from the AAR in my XML? I'm using Android Studio 1.0.2. Thanks in advance.


Solution

  • Solved it from this post, down at the bottom where it talks about external libraries. Essentially, when referencing AAR elements in code you do so using the package specified in the AAR's manifest, but when referencing AAR elements in XML you cite the namespace of the project (not the AAR).