7

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

SMR
  • 6,628
  • 2
  • 35
  • 56
user2234
  • 1,282
  • 1
  • 21
  • 44
  • What do you mean by unable to load? Can you provide the code that you use to load the html file? – Calvin Mar 15 '12 at 12:21
  • 1
    AFAIK, ProGuard does not obfuscate resources. What is your evidence that this problem relates to ProGuard? How are you trying to "load" this data? What is your **precise** error (including stack trace if it is a runtime exception)? – CommonsWare Mar 15 '12 at 12:24
  • I am loading the url this way webView.loadUrl("file:///android_res/raw/help.html"); I get "Web page not loaded" when I create Obfuscate build. Else it works without the Obfuscate build. – user2234 Mar 16 '12 at 07:15
  • Possible duplicate of [Prevent Proguard to remove specific drawables](http://stackoverflow.com/questions/6280188/prevent-proguard-to-remove-specific-drawables) – rds Apr 07 '16 at 10:04
  • Please see the first answer here: https://stackoverflow.com/questions/6280188/prevent-proguard-to-remove-specific-drawables This worked for me. – Matt Holgate Mar 28 '12 at 17:03

3 Answers3

11

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... ;).

Phil_R
  • 176
  • 2
  • 7
5

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

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
vladZams
  • 737
  • 6
  • 9
  • The proguard rule only works with keep, if I add the rule with keepnames the webview can't find the html. example: -keep class com.example.appname.R$raw { *; } – norbDEV Oct 25 '19 at 13:16
2

Create a file in raw folder

keep.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@raw/*">
</resources>

That's it! It will keep all the files in raw folder, If you want to restrict it to only one particular file you can use

tools:keep="@raw/fileName"

Note that you should not add file extension in the end so it shouldn't be filename.html

beginner
  • 2,366
  • 4
  • 29
  • 53