Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScriptInterface methods not recognizable (Android webview)

I have in my 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. So I updated the proguard rules file :

-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

like image 370
BVtp Avatar asked Feb 18 '26 18:02

BVtp


1 Answers

Interface class

public class JavaScriptInterface
    {
        Context mContext;

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

In proGuard.pro file

-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.

like image 75
Pavya Avatar answered Feb 21 '26 07:02

Pavya