1

Since upgrading my nexus 4 to Android kitkat, my application stopped working. I was using javascript methods in webview but now my methods are not called. This is how i setup webview and load my javascripts:

 mWebview.getSettings().setJavaScriptEnabled(true);
 JavaScriptInterface jsi = new JavaScriptInterface(mCtx);
 mWebview.addJavascriptInterface(jsi, ANDROID_BRIDGE);
 mWebview.setWebViewClient(new WebClient());
 mWebview.setWebChromeClient(new OwnChromeClient());
 mWebview.setWebContentsDebuggingEnabled(true);
 mWebview.loadUrl("about:blank"); //load dummy url to onPageFinished called in webViewClient

and my webClient:

public class WebClient extends WebViewClient {


        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            Log.e(WebClient.class.getSimpleName(), description + " code "
                    + errorCode);
        }

        /**
         * Load javascript into webview
         */
        @Override
        public void onPageFinished(WebView view, String url) {
            ECalcMarblesWrapper wrapper = new ECalcMarblesWrapper(mCtx);
            String javascripts = wrapper.getJavascripts();

            String content = String.format(jsHtmlContainer, javascripts,
                    jsHelpFunctions);

            view.loadUrl("javascript: " + content);
        }
    }

all loading methods are called but when i call any of javascript methods, this error is printed out to console:

Uncaught ReferenceError: initForm is not defined

where initForm is my javascript method.

On api 18 application is doing just fine, i have javascript annotation in my methods

1
  • Hey did you find any solution for Kitkat version. I am facing the same issue. Thank you Commented Jul 7, 2016 at 5:38

2 Answers 2

4

If the target version of the application is set to 17 or higher you need to add annotation @JavascriptInterface above each method you want to export to the web view.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but i have that, on android api 18 application is doing just fine
0

everything changes in Android kitkat for webview and javascript. you need to use evaluateJavascript()

check here: https://developer.android.com/guide/webapps/migrating.html and here: https://developer.android.com/reference/android/webkit/WebView.html#evaluateJavascript(java.lang.String, android.webkit.ValueCallback)

1 Comment

Thanks mate, i know that i should call javascript function differently in kitkat but it seems that problem is in loading that js functions from file to webview

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.