Android: Can't find .html file in res / raw after obfuscation
I faced this same problem. I have a help html file in raw and after obfuscation. I run my application and I get an error that the file was not 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 problem 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 that doesn't get in the way of this issue. Adding the second line completely fixed the problem. I don't think the first line is still needed, but I haven't tested it yet, and since it currently works ...;).
source to share
Suggested in answer to this question -
-keep class **.R$*
is not the most elegant solution as it instructs ProGuard to keep all classes R
regardless of which 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: class "my.app.package.R $ raw" could not be found in path: DexPathList [ [...
The maximum restriction instruction I added to my file proguard-rules.pro
:
-keepnames class my.app.package.R$raw { public static <fields>; }
Obviously, since the class R
only contains fields and all those fields are of type public static
, in practice there shouldn't be any difference between the above and
-keepnames class my.app.package.R$raw { *; }
However, here I am
- DO NOT disable compression and obfuscation for all other inner classes in
R
exceptraw
. targeting
R
in one specific package only.This approach should be better in case you have more than one module in your project that provides its own resources that may not be needed for one specific APK you are building (having, say, more than one android_application module - APK sources - in your project).
To understand the difference between -keepnames
and -keep
, refer to the following.
Distinguishing Between Various ProGuard Directives -keep
- Tuesday May 29th 04:10:50 AM MSK 2018
source to share