JavaScript interface cannot find function

I have a class JavaScriptInterfacethat has one function. The function is annotated with help @JavascriptInterface, and I believe I configured it correctly.

public class JavaScriptInterface {

    ...

    @JavascriptInterface
    public void processBody(String uri, String body) {
        Log.d(TAG, "Process body");
        ...
    }
}

Then I set JavaScriptEnabled to WebViewand try to call the function:

final WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JavaScriptInterface(), "java");

webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        Log.d(TAG, "Page finished");
        callJavaScript();
    }
});

The page finishes loading, then I tried various methods to call the JavaScript function from mine JavaScriptInterface. Using one of these two methods:

view.evaluateJavascript(javascript);

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

And one of these javascript lines:

"(function() { window.java.processBody('" + url + "', document.body.innerHTML); })()"

"(function() { processBody('" + url + "', document.body.innerHTML); })()"

"window.java.processBody('" + url + "', document.body.innerHTML);"

"processBody('" + url + "', document.body.innerHTML);"

Every time I get an error:

I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: processBody is not a function", source: (1)

Although the error message never appears on older devices (pre KitKat), the function is simply not called.

This method also worked previously, and I do not believe that I changed any code for this action. This morning I found out about this error.

+2
1

, , ProGuard , Java. - Java, JS, , .

, ProGuard, Java, . proguard javascript?

+4

Source: https://habr.com/ru/post/1623946/


All Articles