Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access raw resource when applicationId is built modified

I have modified my app so I can have both debug and production build on single mobile device as separate applications:

android { buildTypes { debug {
        applicationIdSuffix ".debug"
        resValue 'string', 'app_name', 'LeliMath DEV'
}

I realized that WebView stopped working on loading files located in res/raw folder. I get just simple error file not found. Production build works fine.

    WebView webview = new WebView(this);
    setContentView(webview);
    webview.loadUrl("file:///android_res/raw/changelog.html");

Android monitor says:

AndroidProtocolHandler: Unable to open resource URL: file:///android_res/raw/changelog.html
java.lang.ClassNotFoundException: 
Didn't find class "lelisoft.com.lelimath.debug.R$raw" on path: 
DexPathList[[zip file "/data/app/lelisoft.com.lelimath.debug-1/base.apk"],
nativeLibraryDirectories=[/data/app/lelisoft.com.lelimath.debug-1/lib/arm64, 
/system/lib64, /vendor/lib64]]                                                                                       
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
like image 650
Leos Literak Avatar asked Sep 03 '26 12:09

Leos Literak


1 Answers

You could try these steps:

  1. Create new R.java file under different flavor directory. In your case create this file inside this package lelisoft.com.lelimath.debug inside your debug flavor directory.
  2. Duplicate your normal R.java content or just pick what do you need and set it with the value from your normal R.java. For example:
package lelisoft.com.lelimath.debug;
public final class R {
   public static final class raw {
      public static final String changelog = lelisoft.com.lelimath.R.raw.changelog;
   }
}
  1. Last, add this line to your proguard:

-keep class lelisoft.com.lelimath.debug.R$raw {*;}

You could repeat those steps to any flavor you have. Hope it fix your problem.

like image 165
Reza Avatar answered Sep 05 '26 03:09

Reza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!