Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proguard Breaks Android WebView, Why?

I have a WebView in my activity, and using Proguard for obfuscation seems to break my WebView and i don't understand why.

The code is pretty simple, I have the HTML file in my res/raw directory, here is the code that loads it fine when debugging.

WebView mv = (WebView)findViewById(R.id.webView1);
mv.loadUrl("file:///android_res/raw/wesite.html");

As soon as I create the apk for release, running it through proguard it doesn't work, i just get the cannot load page.

I haven't added anything to the proguard config file as yet.

like image 914
Ne0 Avatar asked Aug 24 '12 15:08

Ne0


2 Answers

Proguard obfuscates directories so if you are looking for android_res/raw it is probably no longer called that!

You can add rules to the proguard.cfg file in your project that will make it skip certain files. But in this case, moving your raw resource to the assets folder will do the trick.

The problem is that the Webkit FileLoader will try and load your R$drawable class using reflection. If you do not add any keep rule to your proguard.cfg file that class will be renamed, hence Webkit will not be able to load your resource. (Taken from Prevent Proguard to remove specific drawables ).

This is why Android uses the R class naming system for resources - a uniquie lookup id instead of referencing the files by their location

By placing the file into the assets folder your are bypassing the R class referencing system and everything should work okay.

You should move your website.html file into the assets folder and call:

mv.loadUrl("file:///android_asset/wesite.html");

As is suggested at the link above, it should be possible to add the below rule to your Proguard.cfg file to stop the resources location being obfucated instead:

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

-keep class **.R$*

Bare in mind the obfuscation works the way it does for a reason!

Hope this helps

like image 195
biddulph.r Avatar answered Nov 15 '22 19:11

biddulph.r


Just to clarify the asset vs assets debate. The folder in the project directory should be called "assets" as per google docs (see below) whereas to access them you must use "file:///android_asset/"

assets/ This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.

Please note that I can't add any comment, that's why I posted this as answer.

like image 30
read_somewhere_else Avatar answered Nov 15 '22 19:11

read_somewhere_else