我有一个类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,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>; }
解决方法
如果您的Javascript接口方法使用@JavascriptInterface注释,则可以使用它们保存它们
-keepclassmembers class * { @android.webkit.JavascriptInterface <methods>; }