Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Cant find .html file in res/raw after obfuscation

I have some html files in res/raw which I open in WebView. But after obfuscation they are unable to load.

like image 676
nibz Avatar asked Mar 15 '12 12:03

nibz


2 Answers

I ran into exactly this same issue. I have my help html file in raw and after obfuscation I run my app and get an error that the file could not be found.

Here is my HelpActivity class:

public class HelpActivity extends BaseActivity
{
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      // requesting to turn the title OFF
      //requestWindowFeature(Window.FEATURE_NO_TITLE);
      // making it full screen
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

      setContentView(R.layout.help);
      setTitle(getString(R.string.help_title));
      WebView webView = (WebView) findViewById(R.id.webView);
      webView.loadUrl("file:///android_" + getString(R.raw.how_to_play_zeewee));
  }
}

I fixed this issue by adding the following to my proguard.cfg file:

-keepclassmembers class **.R$* {public static <fields>;}
-keep class **.R$*

You probably already have the first line, but this does not prevent the issue. Adding the second line eliminated the issue completly. I don't think the first line is still needed, but I have not tested that yet -- and since it currently works... ;).

like image 74
Phil_R Avatar answered Sep 26 '22 09:09

Phil_R


Suggested in an answer in this question -

-keep class **.R$*

isn't the most elegant solution since it instructs ProGuard to preserve all of the R classes regardless of the package they are in.

Having the same problem with WebView, the error I see in my Logcat:

... E/AndroidProtocolHandler: Unable to open resource URL:file:///android_res/raw/$MISSING_RESOURCE_NAME.css java.lang.ClassNotFoundException: Didn't find class "my.app.package.R$raw" on path: DexPathList[[...

The instruction with maximum restrictions I added to my proguard-rules.pro file:

-keepnames class my.app.package.R$raw { public static <fields>; }

Obviously Since R class contains only fields and all of these fields have public static type, in practice, there should no be the difference between the one above and

-keepnames class my.app.package.R$raw { *; } 

However, here I'm

  1. NOT disabling shrinking and obfuscation for all of the rest inner classes in R other than raw.
  2. targeting R in one specific package only.

    That approach should be better in case if you have more than one module in your project that provides its own resources that may not be needed for the one particular APK you are building (having, let's say, more than one android_application modules – APK sources – in your project as well).

To understand the difference between -keepnames and -keep, please, refer to the following.

Distinguishing between the different ProGuard -keep directives - Tue May 29 04:10:50 MSK 2018

like image 31
vladZams Avatar answered Sep 23 '22 09:09

vladZams