P5music
P5music

Reputation: 3337

JavaScript injection not working on Android Chromium WebView when target is Android 3.1

My Android app injects JS code into a WebView. It is targeted Android 3.1 but I am experiencing issue with Android 6 and 7 because of the WebView now being chromium inside so it is not the old WebView.

Nevertheless the issue is very strange so I do not think it is because of the quirksmode my app forces to Android 6 and 7.

My app uses all the useful calls to load the JS interface that creates a binding between the Java code and the JS code, and it works on previous versions. After those instructions (addJavascriptInterface() method) it performs a "fake" loading to ensure the JS interface is loaded:

webView.loadData("", "text/html", null);

and when the "page" is loaded, it performs the loading of the real url, that is a local file (file:///...); when this is loaded too a script is injected and executed.

What I experience is that I get the following error on the log cat continuously:

Cannot call determinedVisibility() - never saw a connection for the pid:

and I tried the various suggestions found on SO about this, but no success:

it's the "data:text/html" url that keeps being loaded again and again and I can stop it only loading an empty url but then the JS interface is lost.

What have I to do to keep the JS interface and block the infinite loading loop?

Upvotes: 0

Views: 677

Answers (1)

P5music
P5music

Reputation: 3337

This kind of issue was fixed by changing the first "fake" call that was intended to effectively add the JS interface (it's needed) to the WebView.

The above mentioned method, with this instruction:

webView.loadData("", "text/html", null);

worked only with versions older than Android 6.x (it may depend on the devices, so it could fail also on 4.4 and 5.x, but my app worked on both).

So the right method working on all versions is to load the target file twice (performing a new loading when the first one has completed),

or it is also possible to load another file (like a simple HTML with no content in the body) first and then load the target file (when the first loading has completed).

Upvotes: 0

Related Questions