Proguard为何无法使用Android WebView? [英] Proguard Breaks Android WebView, Why?

查看:96
本文介绍了Proguard为何无法使用Android WebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动中有一个WebView,使用Proguard进行混淆似乎破坏了我的WebView,我不明白为什么.

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

代码非常简单,我的res/raw目录中有HTML文件,下面的代码可以在调试时很好地加载它.

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

一旦我创建了要发布的apk,通过proguard运行它不起作用,我就会得到无法加载的页面.

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

我还没有添加任何内容到proguard配置文件中.

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

推荐答案

Proguard对目录进行模糊处理,因此,如果您要查找android_res/raw,则可能不再称呼它!

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

您可以在项目中的proguard.cfg文件中添加规则,以使其跳过某些文件.但是在这种情况下,将原始资源移动到资产文件夹即可解决问题.

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.

问题在于Webkit FileLoader将尝试使用反射来加载R $ drawable类.如果您没有在proguard.cfg文件中添加任何保留规则,则该类将被重命名,因此Webkit将无法加载您的资源. (摘自防止Proguard删除特定的可绘制对象).

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

这就是为什么Android使用R类命名系统获取资源-唯一查询ID而不是按文件位置引用文件的原因

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

通过将文件放置到资产文件夹中,您将绕过R类引用系统,并且一切正常.

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

您应将您的website.html文件移动到资产文件夹中,然后调用:

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

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

如上面的链接所建议,应该可以将以下规则添加到Proguard.cfg文件中,以防止混淆资源位置:

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!

希望这会有所帮助

这篇关于Proguard为何无法使用Android WebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆