Proguard breaks Android WebView, why?

I have a WebView in my work, and using Proguard for obfuscation seems to violate my WebView, and I don’t understand why.

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

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

As soon as I create an apk for release by running it through proguard, it doesn’t work, I just can’t load the page.

I have not added anything to the proguard configuration file yet.

+6
android webview proguard
Aug 24 2018-12-12T00:
source share
4 answers

Proguard obfuscates directories, so if you are looking for android_res / raw, this is probably not called that anymore!

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

The problem is that Webkit FileLoader will try to load your R $ drawable class using reflection. If you do not add the keep rule to the proguard.cfg file, this class will be renamed, so Webkit will not be able to load your resource. ( Adapted from Prohibit Proguard from deleting certain drawings ).

That's why Android uses the R class naming system for resources - a unique search identifier instead of linking to files by their location

By placing the file in the resource folder, you bypass the R class reference system, and everything should work fine.

You must transfer the website.html file to the resources folder and call:

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

As suggested by the link above, it should be possible to add the following rule to your Proguard.cfg file so that the resource location is missed instead:

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

Calm down that obfuscation works the way it does for some reason!

Hope this helps

+9
Aug 24 2018-12-12T00:
source share

Just to clarify the debate on assets and assets. The folder in the project directory should be named "assets" according to google documents (see below), while to access them you should use "file: /// android_asset /"

assets / This is empty. You can use it to store raw asset files. The files you save here are compiled into an .apk as-is file and the original file name is saved. You can navigate this directory just like a regular file system using a URI and read files as a stream of bytes using AssetManager. For example, this is a good place for textures and game data.

Please note that I cannot add a comment, so I posted this as an answer.

+1
Aug 24 '14 at 9:24
source share

Just update, after you brought up this post on a later question.

In Android Studio (at least 1.0.1) there is no difference in the standard level of obfuscation provided in the release build if you use assets or res for your media. android_res/raw or android_asset . And they both still called it.

I ran apktool on both assemblies, amazed by the latter, using android_res/raw , being bigger. The size was solely caused by my media. Both were hardly confused at all, according to other apkas in the app store. Resources and xml were not confused anywhere. The only difficulty that reverse engineering would have to do is convert baksmali to Java. I saw other apk's getting better confused, but mine kept the original class names that I gave them, although they are broken into several parts.

I am new to pro-guard, I prefer C ++, but I understand that it is used by default for releases.

0
Feb 27 '15 at 19:26
source share

Upload source folder files to webview:

 myWebView.loadUrl("file:///android_assets/myfile.html"); 
-one
Aug 24 '12 at 15:20
source share



All Articles