Android proguard Javascript 接口问题

发布于 2024-10-24 03:38:58 字数 1024 浏览 1 评论 0原文

使用 proguard 进行混淆后,我的项目因 javascriptinterface 失败

这里是包含一些 proguard 配置建议的链接,但在我的情况下它不起作用

http://groups.google.com/group/android-developers/browse_thread/thread/f889e846fbf7ec3f?pli=1

因此来自 Javascript 的调用松散绑定到相关的Java方法

我的proguard配置关于

-keep public class com.trans_code.android.JavascriptCallback 
-keep public class * implements com.trans_code.android.JavascriptCallback 
-keepclassmembers class * implements com.trans_code.android.JavascriptCallback { 
    <methods>; 
} 
-keepclassmembers class * implements JavascriptCallback { 
    void on*(***);
} 
-keep public class com.trans_code.** {
  public protected *;
}

-keepclasseswithmembernames class com.MyActivity$JavascriptInterface

-keepclasseswithmembernames class com.MyActivity$JavascriptInterface {
    public protected *;
}

如果有人知道如何配置proguard以让它过滤掉相关的方法和类,这将对我有很大帮助

My project after obfuscation with proguard fail with javascriptinterface

Here is the link with some suggestions for proguard configuration but it dosn't work in my case

http://groups.google.com/group/android-developers/browse_thread/thread/f889e846fbf7ec3f?pli=1

So the calls from Javascript loose binding to the associated Java methods

My proguard configuration regarding that

-keep public class com.trans_code.android.JavascriptCallback 
-keep public class * implements com.trans_code.android.JavascriptCallback 
-keepclassmembers class * implements com.trans_code.android.JavascriptCallback { 
    <methods>; 
} 
-keepclassmembers class * implements JavascriptCallback { 
    void on*(***);
} 
-keep public class com.trans_code.** {
  public protected *;
}

-keepclasseswithmembernames class com.MyActivity$JavascriptInterface

-keepclasseswithmembernames class com.MyActivity$JavascriptInterface {
    public protected *;
}

if anyone knows how to configure proguard to have it filter out related methods and classes that will help me a lot

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

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

发布评论

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

评论(4

墨落成白 2024-10-31 03:38:58

该原始线程中的类名称特定于该用户 Java 类,而不是所有 javascript 接口通用的。您实现的 javascript 接口只是一个简单的基类。

您需要更改它们以匹配您的接口类的名称。

例如,基于原始线程中的示例,示例代码 WebViewDemo 的正确配置将是:

-keep public class com.google.android.webviewdemo.WebViewDemo.DemoJavaScriptInterface
-keep public class * implements com.google.android.webviewdemo.WebViewDemo.DemoJavaScriptInterface 
-keepclassmembers class * implements com.google.android.webviewdemo.WebViewDemo.DemoJavaScriptInterface { 
    <methods>; 
} 

由于绑定的工作方式,真正需要做的就是保留将从 javascript 调用的内部方法混淆名称,但保持类名不混淆也没什么坏处。

The class names from that original thread are specific to that users Java classes, and not generic to to all javascript interfaces. The javascript interface you implement is just a simple base class.

You need to change them to match the name of your interface class.

For example the correct configuration, based on the example from the original thread, for the sample code WebViewDemo would be:

-keep public class com.google.android.webviewdemo.WebViewDemo.DemoJavaScriptInterface
-keep public class * implements com.google.android.webviewdemo.WebViewDemo.DemoJavaScriptInterface 
-keepclassmembers class * implements com.google.android.webviewdemo.WebViewDemo.DemoJavaScriptInterface { 
    <methods>; 
} 

Due to the way the bindings work all that really needs to be done is to keep the inner methods that will be called from javascript from having the names obfuscated, but keeping the class name from obfuscation doesn't hurt.

合久必婚 2024-10-31 03:38:58

在我的项目中,javascript 接口是一个内部类。所以目前的答案对我不起作用。您需要在类之间使用 $ 。如果您的接口是内部类,请使用此代码

-keep public class com.myapp.MyActivity$MyJavaScriptInterface
-keepclassmembers class com.myapp.MyActivity$MyJavaScriptInterface {
   public *;
}
-keep public class * implements com.myapp.MyActivity$MyJavaScriptInterface

对于 API17+ 添加以下行。否则你的代码将无法在 Android 4.2+ 中运行

-keepattributes JavascriptInterface

类似问题:
Android Proguard Javascript 接口失败
Javascript 界面不适用于 android 4.2

In my project javascript interface is an inner class. So present answers doesn't work for me. You need to use $ between classes. Use this code if you interface is an inner class

-keep public class com.myapp.MyActivity$MyJavaScriptInterface
-keepclassmembers class com.myapp.MyActivity$MyJavaScriptInterface {
   public *;
}
-keep public class * implements com.myapp.MyActivity$MyJavaScriptInterface

For API17+ add the following line. Else your code won't work in Android 4.2+

-keepattributes JavascriptInterface

Similar questions:
Android Proguard Javascript Interface Fail
Javascript interface not working with android 4.2

盛夏已如深秋| 2024-10-31 03:38:58

这对我来说已经足够了:

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

它还有一个额外的好处,那就是可以与您制作的任何类一起用作 Javascript 接口。

工作原理:它会导致所有标记有 @JavascriptInterface 的方法及其原始名称被保留。它不会自动保留该类,但由于您必须在代码中实例化该类才能使用它,所以这不是问题。

关于 trante 的答案中关于在 API 17+ 上保留注释本身的注释:我在 KitKat 上进行了测试并且它有效,所以看起来这也得到了处理。

This was enough for me:

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

It has the added benefit of working with any class you ever make to serve as a Javascript interface.

How it works: It causes all methods tagged with @JavascriptInterface to be kept, with their original name. It will not automatically keep the class, but since you have to instantiate the class in your code to use it, that is not a problem.

Regarding the note in trante's answer about preserving the annotation itself on API 17+: I tested on KitKat and it worked, so it seems that is taken care of as well.

甩你一脸翔 2024-10-31 03:38:58

我这样解决了这个问题:

-keep public class com.myapp.JavaScriptInterface 
-keepclassmembers class com.myapp.JavaScriptInterface { 
    <fields>;
    <methods>;
}

JavaScriptInterface是我的应用程序中的一个类(而不是一个接口)。这就是为什么我没有使用这部分:

* Implements

I solved this problem so:

-keep public class com.myapp.JavaScriptInterface 
-keepclassmembers class com.myapp.JavaScriptInterface { 
    <fields>;
    <methods>;
}

JavaScriptInterface is a class (not an interface) in my app. That's why I didn't use this part:

* implements.

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