Proguard阻止了WebView中的Javascript工作

时间:2013-07-23 16:33:19

标签: android-webview proguard

我有一个类JSBridge(一个内部类),它是一个javascript界面​​:

private class JsBridge implements JsCallback {

    /**
     * @param handlerName method required
     * @param jsonData    data passed through from javascript
     * @param jsCallback  A callback to trigger when handler specified by handlername has finished, could be null
     */
    @JavascriptInterface
    public void callHandler(final String handlerName, final String jsonData, final String jsCallback) {
        Log.d(App.TAG, "Bridge call from JS,  received " + handlerName);
    }

    @JavascriptInterface
    public void onPageLoad(final String pageName) {
        Log.d(App.TAG, "Bridge call from JS,  received onPageLoad - we have the page name " + pageName);
    }

这一点很好,直到我使用proguard进行发布构建。我已经尝试了一些其他的SO答案,并在我的proguard文件中添加了以下行,但它没有帮助。结果是调试版本我得到了回调,发布版本我得不到回调。

-keep public class * implements com.mixcloud.player.view.JsCallback

-keepclassmembers class * implements com.mixcloud.player.view.JsCallback {
    <methods>;
}
-keep public class * implements com.mixcloud.player.view.JsCallback

-keepattributes *Annotation*
-keepattributes JavascriptInterface
-keep public class com.mixcloud.player.view.JSRefreshWebView
-keep public class com.mixcloud.player.view.JSRefreshWebView$JsBridge
-keep public class * implements com.mixcloud.player.view.JSRefreshWebView$JsBridge
-keepclassmembers class * implements com.mixcloud.player.view.JSRefreshWebView$JsBridge {
    <methods>;
}

1 个答案:

答案 0 :(得分:18)

如果您的Javascript接口方法使用@JavascriptInterface注释,则可以使用

保留它们
-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}