如何为javascript接口配置proguard? [英] How to configure proguard for javascript interface?
问题描述
我实现了一个使用 JavascriptInterface 的 Webview.不混淆时它工作正常,但一旦 Proguard 处于活动状态,它就不起作用.我在这里查看了其他答案,但我仍然无法使其正常工作.
I have a implemented a Webview which takes use of JavascriptInterface. It's working fine when not obfuscating, but at once Proguard is active, it does not work. I've looked here at other answers, but i still can't get it working.
一些 WebView 类:
public class Activity_Webview {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JavaScriptInterface (), "HTMLOUT");
webView.setWebViewClient(mWebViewClient);
}
public class JavaScriptInterface implements NonObfuscateable{
@JavascriptInterface
public void processHTML(String html) {
handleFinishFromWebView(html);
}
}
我在 Proguard 中的尝试:
-keep public class * implements com.project.NonObfuscateable
-keepclassmembers class * implements NonObfuscateable {
public void processHTML(java.lang.String);
}
我也试过这个(当没有实现 NonObfuscateable 接口时:
-keep public class com.project.Activity_Webview.JavaScriptInterface
-keep public class * implements com.project.Activity_Webview.JavaScriptInterface
-keepclassmembers class * implements com.project.Activity_Webview.JavaScriptInterface {
<fields>;
<methods>;
}
有没有人知道什么可能是错的?提前致谢
Does anybody have an idea of what could be wrong? Thanks in advance
推荐答案
如果您的两个配置没有包含拼写错误,它们都可以正常工作:
Both your configurations could have worked if they hadn't contained typos:
ProGuard 需要完全限定名称:
ProGuard requires fully qualified names:
NonObfuscateable
-> com.project.NonObfuscateable
已编译的类使用$"作为内部类的分隔符:
Compiled classes use '$' as a separator for inner classes:
com.project.Activity_Webview.JavaScriptInterface
-> com.project.Activity_Webview$JavaScriptInterface
在控制台日志中,ProGuard 会打印出有关此类疑似错别字的注释.
In the console log, ProGuard prints out notes about such suspected typos.
保持带注释的 Javascript 接口方法的更通用的解决方案:
A more general solution for keeping annotated Javascript interface methods:
-keepclassmembers class * {
@android.webkit.JavascriptInterface <methods>;
}
这篇关于如何为javascript接口配置proguard?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!