28

我已经实现了一个使用 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>;
}

有人知道可能出了什么问题吗?提前致谢

4

3 回答 3

48

如果它们不包含拼写错误,您的两种配置都可以工作:

  • ProGuard 需要完全限定的名称:

    NonObfuscateable->com.project.NonObfuscateable

  • 编译的类使用 '$' 作为内部类的分隔符:

    com.project.Activity_Webview.JavaScriptInterface->com.project.Activity_Webview$JavaScriptInterface

在控制台日志中,ProGuard 会打印出有关此类可疑拼写错误的注释。

保留带注释的 Javascript 接口方法的更通用的解决方案:

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}
于 2013-07-14T07:52:38.020 回答
4

就我而言,仅工作代码:

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() {
            ...
        }

    }
    ...
}
于 2015-12-26T15:00:47.577 回答
3

如果您使用混淆,除了Eric Lafortune 的回答,您还需要:

-keepattributes JavascriptInterface

http://proguard.sourceforge.net/manual/usage.html#obfuscationoptions

于 2015-01-19T21:57:27.127 回答