Can't find R when I change packageNameSuffix

151 Views Asked by At

I use gradle to build app. And I add a suffix to the packageName of my debug version. Just as following:

buildTypes {
    debug {
        packageNameSuffix ".debug"
    }
}

However, one of the libs I use can't work with this.

I think the lib uses code like this to get the R class:

drawable = Class.forName(this.context.getPackageName() + ".R$drawable");

And it throws java.lang.IllegalArgumentException: ResClass is not initialized.

The correct package for R is com.xxx.R$drawable. Since I add a suffix to the package, when the lib want to get the class using reflection it gets com.xxx.debug.R$drawable.

Is there any way to fix it? BTW I can't modify the code of the lib because it is a jar file.

1

There are 1 best solutions below

0
On

Not sure if it can help you, I have seen a similar problem in different circumstances.

The R class is just a class, for example, when the R package name is different from the current app package name, import com.xxx.yyy.R helps.

Probably you can create a missing class as an ancestor of the class with the correct package name. This, of course, will break your non-debug build, so you will have to add this class for debug builds and remove it for non-debug ones.