简体   繁体   English

无法识别 JavaScriptInterface 方法(Android webview)

[英]JavaScriptInterface methods not recognizable (Android webview)

I have in my MainActivity:我在我的 MainActivity 中有:

 webView.addJavascriptInterface( new JavaScriptInterface( this ), "ajaxHandler" );
....
 public class JavaScriptInterface
    {
        Context mContext;

        JavaScriptInterface( Context c ) {
            mContext = c;
        }

        public void DoSomething( String dataToPrint )
        {
          .....
        }
}

I read that the problem might be proguard.我读到这个问题可能是proguard。 So I updated the proguard rules file :所以我更新了 proguard 规则文件:

-keepclassmembers class fqcn.of.javascript.interface.for.webview {
     public *;
 }

-keep public class com.example.testapp.JavaScriptInterface
-keep public class * implements com.example.testapp.JavaScriptInterface
-keepclassmembers class * implements com.example.testapp.MainActivity.JavaScriptInterface{
    public *;
}

It didn't help though... in the chrome debugger, since I put in console the ajaxHandler object and the DoSomething method, I can see the ajaxHandler object as Object {} but it's empty, and the method DoSomething is undefined虽然它没有帮助......在chrome调试器中,因为我在控制台中放置了ajaxHandler对象和DoSomething方法,我可以将ajaxHandler对象视为Object {}但它是空的,并且DoSomething方法undefined

Interface class接口类

public class JavaScriptInterface
    {
        Context mContext;

        JavaScriptInterface( Context c ) {
            mContext = c;
        }
        @JavascriptInterface //add this
        public void DoSomething( String dataToPrint )
        {
          .....
        }
}

In proGuard.pro file在 proGuard.pro 文件中

-keep public class com.example.testapp.MainActivity$JavaScriptInterface
-keep public class * implements com.example.testapp.MainActivity$JavaScriptInterface
-keepclassmembers class * implements com.example.testapp.MainActivity$JavaScriptInterface{
     <methods>;
}
-keepattributes *Annotation*

Use $ sign not .不使用$符号. to get inner interface class name.获取内部接口类名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM