8

I have a class JSBridge (an inner class) which is a javascript interface:

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);
    }

This works fine until I do a release build with proguard. I've tried following some other SO answers and have added the following lines to my proguard file, but it has not helped. The result is the debug version I get the callbacks, the release version I get no callbacks.

-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>;
}
serenskye
  • 3,467
  • 5
  • 35
  • 51

1 Answers1

19

If your Javascript interface methods are annotated with @JavascriptInterface, you can preserve them with

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}
Eric Lafortune
  • 45,150
  • 8
  • 114
  • 106
  • Ahh yes I think i saw this in the documentation, the confusion was, i thought I Was supposed to place the package with my actual javascript interface class. This worked first time! – serenskye Jul 29 '13 at 09:24
  • I have a weird case, I exported my app using above configurations and it worked in android 2.3.7 but not on nexus 5 (Android 4.2) – lightsaber Mar 02 '14 at 07:13