Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Library Declare-Styleable Runtime Error

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>
like image 383
Eliezer Avatar asked Nov 10 '22 23:11

Eliezer


1 Answers

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

like image 74
Eliezer Avatar answered Nov 14 '22 23:11

Eliezer