java Android Proguard Javascript 接口失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6271485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Proguard Javascript Interface Fail
提问by Denis
I use in my project a piece of code as described here
我在我的项目中使用了一段代码,如下所述
http://lexandera.com/2009/01/extracting-html-from-a-webview/
http://lexandera.com/2009/01/extracting-html-from-a-webview/
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.
我创建了 .apk 文件,将它安装在我的设备上并且它可以正常工作。如果我尝试使用混淆与 proguard 项目失败,则无法达到 MyJavaScriptInterface 的 showHTML(String html) 方法。
My proguard configuration regarding that
我的 proguard 配置
-keep public class com.mypackage.MyClass.MyJavaScriptInterface
-keep public class * implements com.mypackage.MyClass.MyJavaScriptInterface
-keepclassmembers class * implements com.mypackage.MyClass.MyJavaScriptInterface {
<methods>;
}
according to this this answer Android proguard Javascript Interface problem.
根据this answer Android proguard Javascript Interface problem。
SOLVED.
解决了。
As Eric suggested, I changed the Proguard configuration file like this:
正如 Eric 建议的那样,我像这样更改了 Proguard 配置文件:
-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.
现在我的项目完美运行。
For API 17+ you also need to preserve the @JavascriptInterface annotations:
对于 API 17+,您还需要保留 @JavascriptInterface 注释:
-keepattributes JavascriptInterface
http://developer.android.com/reference/android/webkit/JavascriptInterface.html
http://developer.android.com/reference/android/webkit/JavascriptInterface.html
采纳答案by Eric Lafortune
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.
如果 MyJavaScriptInterface 是 MyClass 的内部类,ProGuard 需要一个完全限定的 name com.mypackage.MyClass$MyJavaScriptInterface
。命名约定$
用于 ProGuard 运行的编译类文件。请注意,ProGuard 在配置中提到了在输入 jar 中找不到的类名,这表明这些名称可能拼写错误。
回答by Sangoku
-keepclassmembers class com.mypackage.MyClass$JavaScriptInterface {
public *;
}
Use only this. It works for me.
只用这个。这个对我有用。
回答by Nantha kumar
Those Who are laze to provide the entire package path.
那些懒得提供整个包路径的人。
-keepclassmembers class **.*$PaymentJavaScriptInterface{
public *;
}
回答by Darpan
As suggested by edit in question, out of those suggestions, only using
正如所讨论的编辑所建议的那样,在这些建议中,仅使用
-keepclassmembers class com.mypackage.MyClass$MyJavaScriptInterface {
public *;
}
-keepclassmembers class com.mypackage.MyClass$MyJavaScriptInterface {
public *;
}
with Important-
与重要-
For API 17+ to preserve @JavascriptInterface annotations:
对于 API 17+ 以保留 @JavascriptInterface 注释:
-keepattributes JavascriptInterface
-keepattributes JavascriptInterface
(Which was stopping my app to work on Marshmallow)
(这阻止了我的应用程序在棉花糖上工作)