36

I have a webview with added javascript interface which works perfectly on most devices, except for those running Android 4.2.1.

I removed most of the code, and stayed with a very basic code:

this.webView.getSettings().setJavaScriptEnabled(true);
this.webView.setWebChromeClient(new WebChromeClient());
this.webView.addJavascriptInterface(new Object() {
    public void handshake() {
        Log.d("JS", "handshake no params");
    }

    public void handshake(String json) {
        Log.d("JS", "handshake with params: " + json);
    }
}, "Android");

In the javascript side of things the testing code looks like this:

Android.handshake();

But I get this in the logcat:

E/Web Console: Uncaught TypeError: Object [object Object] has no method 'handshake'

Again, this same exact thing works fine in devices who are have older android os (< 4.2.1) that I have tested (the minimal version being 2.3.3).

The strange thing is that if I start a completely new project, with nothing but a single Activity which has just a WebView, with the same code, everything works fine even for 4.2.1, but when it's part of my actual project, things break. There's nothing I'm doing with the webview which is not included in this code snippets I provided.

What is the most strange to me is that the javascript finds the Android object but it just does not have the requested method (handshake), how can that be?

Any help will be greatly appreciated since this has been driving me crazy for the past 2 weeks or so (it's a bug I go back to all the time, then give up, etc). Thanks.

2 Answers 2

96

From the Android 4.2 documentation:

Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the annotation, then the method will not accessible by your web page when running on Android 4.2 or higher.

1
  • Plus, you must set minSdkVersion >= 17, which is 4.2, Jelly bean MR1. Unless, it will do exactly what @Nitzan-Tomer described. The reason for this behavior is because of Android security issues.
    – Ch.Idea
    May 7, 2016 at 20:46
4

You have to annotate (@JavascriptInterface) methods in Java class that you want to make available to JavaScript.

public class JavaScriptInterface {
    Context context;

    JavaScriptInterface(Context c) {
        context = c;
    }

    @JavascriptInterface
    public void edit(String postid) {
        Log.d("myApp", "EDIT!");
        //do stuff
    }    
}

Its worked for me. Try out this.

2
  • You should take out the first '@JavascriptInterface' as it throws an error in Android Studio that it isn't applicable to the constructor. Nov 17, 2015 at 4:00
  • Thank You @AlexanderBolinsky
    – Raghu
    Nov 20, 2015 at 18:29

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.