android 4.2对于addJavascriptInterface,有了安全限制

class JsObject {
   @JavascriptInterface
    public String toString() { return "injectedObject"; }
}
webView.addJavascriptInterface(new JsObject(), "injectedObject");
webView.loadData("", "text/html", null);
webView.loadUrl("javascript:alert(injectedObject.toString())");

注意上面加粗的@JavascriptInterface一定要添加。

另外,要是有混淆编译时候,别忘记加上下面:

-keep public class com.yourfullpackagename.Yourapp$JavaScriptInterface
-keepclassmembers class com.yourfullpackagename.Yourapp$JavaScriptInterface {*;}
-keepattributes com.yourfullpackagename.Yourapp$JavaScriptInterface
-keepattributes *Annotation*

你可能感兴趣的:(JavaScript,android)