Android AAR unable to resolve field in styleable

By : jvincek
Source: Stackoverflow.com
Question!

I have an "android-commons" library of sorts which gets built into an aar (it's not a jar). One of the things it contains is a custom implementation of "Typeface" views (most of the classes of TextView, e.g. TypefaceTextView, TypefaceButton etc.). To acomplish this, I've declared the following in my attrs.xml (in /res/values):

<attr name="fontName" format="string" />
<declare-styleable name="CustomFont">
    <attr name="fontName" />
</declare-styleable>

I could then declare a custom style which specified the font name of a .ttf file within my assets, which the View would parse and set the custom typeface.

// excerpt from TypefaceTextView

public TypefaceTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setCustomFont(context, attrs);
}

private void setCustomFont(Context context, AttributeSet attrs) {
    if (!isInEditMode()) {
        Typefaces.setCustomFont(context, attrs, this);
    }
}

The Typefaces class is a cache for Typefaces and contains utility methods for setting the Typeface to a TextView:

// excerpt from Typefaces

public static void setCustomFont(Context context, AttributeSet attrs, TextView view) {
    String assetPath = getCustomFontPath(context, attrs);
    setCustomFont(context, assetPath, view); // a method that sets the TF to the view
}

public static String getCustomFontPath(Context context, AttributeSet attrs) {
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFont);
    String assetPath = a.getString(R.styleable.CustomFont_fontName);
    a.recycle();
    return assetPath;
}

Up until now (before Android Studio), for lack of time to modularize this, I had this côde in a few of my projects and it always worked correctly, I could specify fontName in a style which I applied to my Views and all was well.

<style name="TestStyle">
    <item name="fontName">ApexNew-Bold.ttf</item>
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">#ffffff</item>
</style>

Now, after a switch to AS, gradle and continuous integration, I put all of this into the mentioned "commons" library which gets built into an aar file. When I add a dependency to the aar, I see all of the Typeface classes and can use them as usual (in code or in layouts). The project builds also, but when running it I get the following exception when inflating a layout that has a TypefaceTextView in it:

unable to resolve static field 2955 (CustomFont) in Lmy/package/name/widgets/R$styleable
.....
android.view.InflateException: Binary XML file line #11: Error inflating class my.package.name.widgets.typeface.TypefaceTextView

The error is pretty clear, in that it can't find the styleable, but what I can't figure out is why that happens. Here is a tree of the unpacked aar:

AAR
│   AndroidManifest.xml
│   classes.jar
│   R.txt
│
├───aidl
├───assets
└───res
    └───values
            values.xml

R.txt contains the following:

int attr fontName 0x7f010000
int[] styleable CustomFont { 0x7f010000 }
int styleable CustomFont_fontName 0

/res/values/values.xml contains the following:

"1.0" encoding="utf-8"?>
<resources>

    <!-- From: file:/C:/Users/vjosip/AndroidStudioProjects/AndroidCommons/widgets/src/main/res/values/attrs.xml -->
    <eat-comment />

    <attr name="fontName" format="string" />

    <declare-styleable name="CustomFont">
        <attr name="fontName" />
    </declare-styleable>

</resources>

Can somebody give an insight as to what could be happening here? I have tried cleaning the project, rebuilding it on a few different machines (including the jenkins CI server) - so far the outcome is the same.

By : jvincek


Answers

I just now imported from Idea to Android Studio and found the obtainStyledAttributes does not find all attributes (runtime). Compiling the same code in Idea, the attributes are ok again. I think you could face the same problem.

Issue has been solved Oct 10, 2014: Issue 74140: Using package name with upper case letters causes class loading issues. https://code.google.com/p/android/issues/detail?id=74140



This video can help you solving your question :)
By: admin