I have some html files in res/raw
which I open in WebView
. But after obfuscation they are unable to load.
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... ;).
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
R
other than raw
. 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With