Android Proguard Javascript 接口失败 [英] Android Proguard Javascript Interface Fail
问题描述
我在我的项目中使用了一段代码,如下所述
I use in my project a piece of code as described here
http://lexandera.com/2009/01/提取-html-from-a-webview/
我创建了 .apk 文件,将其安装在我的设备上并且它可以正常工作.如果我尝试使用混淆与 proguard 项目失败,则无法达到 MyJavaScriptInterface 的 showHTML(String html) 方法.
I create the .apk file, install it on my device and it correctly works. If I try to use the obfuscation with proguard the project fails, the method showHTML(String html) of MyJavaScriptInterface is not reached.
我的 proguard 配置
My proguard configuration regarding that
-keep public class com.mypackage.MyClass.MyJavaScriptInterface
-keep public class * implements com.mypackage.MyClass.MyJavaScriptInterface
-keepclassmembers class * implements com.mypackage.MyClass.MyJavaScriptInterface {
<methods>;
}
根据这个答案Android proguard Javascript 接口问题.
已解决.
按照 Eric 的建议,我像这样更改了 Proguard 配置文件:
As Eric suggested, I changed the Proguard configuration file like this:
-keep public class com.mypackage.MyClass$MyJavaScriptInterface
-keep public class * implements com.mypackage.MyClass$MyJavaScriptInterface
-keepclassmembers class com.mypackage.MyClass$MyJavaScriptInterface {
<methods>;
}
现在我的项目完美运行.
Now my project works perfectly.
对于 API 17+,您还需要保留 @JavascriptInterface 注释:
For API 17+ you also need to preserve the @JavascriptInterface annotations:
-keepattributes JavascriptInterface
http://developer.android.com/reference/android/webkit/JavascriptInterface.html
推荐答案
如果 MyJavaScriptInterface 是 MyClass 的内部类,ProGuard 需要一个完全限定的名称 com.mypackage.MyClass$MyJavaScriptInterface
.带有 $
的命名约定用于 ProGuard 操作的已编译类文件.请注意,ProGuard 在配置中提到了它在输入 jar 中找不到的类名,这表明这些名称可能拼写错误.
If MyJavaScriptInterface is an inner class of MyClass, ProGuard expects a fully qualified name com.mypackage.MyClass$MyJavaScriptInterface
. The naming convention with $
is used in the compiled class files on which ProGuard operates. Note that ProGuard mentions class names in the configuration that it can't find in the input jar, suggesting that these names may have been misspelled.
这篇关于Android Proguard Javascript 接口失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!