我实现了一个使用JavascriptInterface的Webview。它没有混淆时工作正常,但Proguard立即活跃,它不起作用。我看过其他答案,但我仍然无法正常工作。
某些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>;
}
有人知道可能出现什么问题吗? 提前致谢
答案 0 :(得分:37)
如果他们没有包含拼写错误,那么您的配置都可以正常工作:
ProGuard需要完全限定名称:
NonObfuscateable
- &gt; com.project.NonObfuscateable
编译类使用'$'作为内部类的分隔符:
com.project.Activity_Webview.JavaScriptInterface
- &gt; com.project.Activity_Webview$JavaScriptInterface
在控制台日志中,ProGuard打印出有关此类可疑错字的注释。
用于保留带注释的Javascript接口方法的更通用的解决方案:
-keepclassmembers class * {
@android.webkit.JavascriptInterface <methods>;
}
答案 1 :(得分:3)
在我的情况下,仅使用代码:
proguard.cfg:
-dontwarn
-keepattributes Signature
-keepattributes SetJavaScriptEnabled
-keepattributes JavascriptInterface
-keepattributes InlinedApi
-keepattributes SourceFile,LineNumberTable
-keepattributes *Annotation*
-keepclassmembers class * {
@android.webkit.JavascriptInterface <methods>;
}
-keepclassmembers class * {
@android.webkit.JavascriptInterface <methods>;
}
-keepclassmembers class **.*$MyJavascriptInterface {
*;
}
-keepclassmembers class **.*$JavaScriptInterface {
*;
}
-keep public class **.*$MyJavascriptInteface
-keep public class **.*$JavaScriptInterface
Java代码:
@SuppressLint("SetJavaScriptEnabled")
public class ActivityWebView extends Activity {
...
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJavascriptInterface(MyActivity.this), "MyJSI");
....
public class MyJavaScriptInterface {
Context context;
MyJavascriptInterface(Context context) {
this.context = context;
}
@JavascriptInterface
@SuppressWarnings("unused")
public void myjavascriptfunction() {
...
}
}
...
}
答案 2 :(得分:1)
如果您正在使用混淆,除了Eric Lafortune's answer之外,您还需要:
-keepattributes JavascriptInterface
http://proguard.sourceforge.net/manual/usage.html#obfuscationoptions