Android Proguard Javascript 接口失败

发布于 2024-11-14 18:36:01 字数 1455 浏览 3 评论 0原文

我在我的项目中使用了一段代码,如下所述

http:// /lexandera.com/2009/01/extracting-html-from-a-webview/

我创建了 .apk 文件,将其安装在我的设备上并且它可以正常工作。如果我尝试使用 proguard 进行混淆,则项目会失败,无法达到 MyJavaScriptInterface 的 showHTML(String html) 方法。

我的混淆器配置

-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 Interface Problem

已解决。

按照 Eric 的建议,我更改了 Proguard 配置文件,如下所示:

-keep public class com.mypackage.MyClass$MyJavaScriptInterface
-keep public class * implements com.mypackage.MyClass$MyJavaScriptInterface
-keepclassmembers class com.mypackage.MyClass$MyJavaScriptInterface { 
    <methods>; 
}

现在我的项目运行完美。

对于 API 17+,您还需要保留 @JavascriptInterface 注释:

-keepattributes JavascriptInterface

http:// developer.android.com/reference/android/webkit/JavascriptInterface.html

I use in my project a piece of code as described here

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.

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>; 
}

according to this this answer Android proguard Javascript Interface problem.

SOLVED.

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.

For API 17+ you also need to preserve the @JavascriptInterface annotations:

-keepattributes JavascriptInterface

http://developer.android.com/reference/android/webkit/JavascriptInterface.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

狠疯拽 2024-11-21 18:36:01

如果 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.

2024-11-21 18:36:01
    -keepclassmembers class com.mypackage.MyClass$JavaScriptInterface {    
public *;
     }

仅使用此。这对我有用。

    -keepclassmembers class com.mypackage.MyClass$JavaScriptInterface {    
public *;
     }

Use only this. It works for me.

姜生凉生 2024-11-21 18:36:01

那些懒惰提供整个包路径的人。

-keepclassmembers class **.*$PaymentJavaScriptInterface{
public *;
}

Those Who are laze to provide the entire package path.

-keepclassmembers class **.*$PaymentJavaScriptInterface{
public *;
}
傾旎 2024-11-21 18:36:01

正如相关编辑所建议的那样,
从这些建议中,
仅使用

-keepclassmembers class com.mypackage.MyClass$MyJavaScriptInterface {
民众 *;
}

重要 -

对于 API 17+ 保留 @JavascriptInterface 注释:

-keepattributes JavascriptInterface

(这阻止了我的应用程序在 Marshmallow 上运行)

As suggested by edit in question,
out of those suggestions,
only using

-keepclassmembers class com.mypackage.MyClass$MyJavaScriptInterface {
public *;
}

with Important -

For API 17+ to preserve @JavascriptInterface annotations:

-keepattributes JavascriptInterface

(Which was stopping my app to work on Marshmallow)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文