Android proguard Javascript 接口问题
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该原始线程中的类名称特定于该用户 Java 类,而不是所有 javascript 接口通用的。您实现的 javascript 接口只是一个简单的基类。
您需要更改它们以匹配您的接口类的名称。
例如,基于原始线程中的示例,示例代码 WebViewDemo 的正确配置将是:
由于绑定的工作方式,真正需要做的就是保留将从 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:
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.
在我的项目中,javascript 接口是一个内部类。所以目前的答案对我不起作用。您需要在类之间使用
$
。如果您的接口是内部类,请使用此代码对于 API17+ 添加以下行。否则你的代码将无法在 Android 4.2+ 中运行
类似问题:
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 classFor API17+ add the following line. Else your code won't work in Android 4.2+
Similar questions:
Android Proguard Javascript Interface Fail
Javascript interface not working with android 4.2
这对我来说已经足够了:
它还有一个额外的好处,那就是可以与您制作的任何类一起用作 Javascript 接口。
工作原理:它会导致所有标记有
@JavascriptInterface
的方法及其原始名称被保留。它不会自动保留该类,但由于您必须在代码中实例化该类才能使用它,所以这不是问题。关于 trante 的答案中关于在 API 17+ 上保留注释本身的注释:我在 KitKat 上进行了测试并且它有效,所以看起来这也得到了处理。
This was enough for me:
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.
我这样解决了这个问题:
JavaScriptInterface是我的应用程序中的一个类(而不是一个接口)。这就是为什么我没有使用这部分:
* Implements
。I solved this problem so:
JavaScriptInterface is a class (not an interface) in my app. That's why I didn't use this part:
* implements
.