Android Library Declare-Styleable Runtime Error

By : Eliezer
Source: Stackoverflow.com
Question!

I have a library with some widgets in maven central. When I add that library as a dependency (from maven central) to a project using gradle, I get:

java.lang.NoClassDefFoundError: com.my.pacakge.R$styleable

If I manually download the .aar and include that in the project, everything works fine. I tried using Android Studio's code completion to see if the library's R was included. When I use the maven dependency, typing com.my.pacakge.R returns no results, but when I use a local .aar it returns the R for the library.

Here's the library code:

// widget constructor
public ForegroundImageView(Context context, AttributeSet attrs) {
    super(context, attrs);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ForegroundImageView);
    Drawable foreground = a.getDrawable(R.styleable.ForegroundImageView_android_foreground);
    if (foreground != null) {
        setForeground(foreground);
    }
    a.recycle();
}

// attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ForegroundImageView">
        <attr name="android:foreground"/>
    </declare-styleable>
</resources>
By : Eliezer


Answers

The issue is that the dependency was getting published with publishNonDefault true.

By : Eliezer


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