0

My Android app features an activity where a script is injected into a WebView after a XHTML page is loaded from disk (not the internet).

The app was originally targeted at API level 12 and compiled to the same SDK level, but I had to update it and new rules made me target API level 26.

I also compiled to various SDK level like 17, 19 because I had to use the @JavascriptInterface annotation on public methods of an object that allows the script to call methods that are in Java code.

What I experience is that the WebView is stuck because an error occurs "Uncaught SyntaxError: Unexpected end of input" (no further information with Chrome remote debugging) and this just happens if I test the app on an Android 7 device, while the app works on a Android 4 device even if its API level is not targeted or compiled to.

I read many answers about syntax errors leading to this kind of error, or commented lines in the code, but it is very strange because the old device has no problems and the app works very well.

I tried many combinations in the gradle file.

Is there any other possibility to consider to have my app working?

P5music
  • 3,197
  • 2
  • 32
  • 81
  • Welcome, to improve your experience on Stack Overflow please read [how to ask](https://stackoverflow.com/help/how-to-ask) an [On Topic question](https://stackoverflow.com/help/on-topic), and the [Question Check list](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) and [the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and if not already done, [take the tour](https://stackoverflow.com/tour). – Bsquare ℬℬ Dec 03 '18 at 13:25
  • @Bsquare I have to say something here, I had to delete many questions that in hindsight should provide useful information for a broad audience if just they were considered legitimate. When a difficult question is asked here many hurry up to close or undervote. Example: I asked if javascript can have problems with characters like = + and so on in string methods. Answer is "yes if they are in regex expressions"...but the question was on hold and I decided to delete. – P5music Dec 03 '18 at 13:34
  • I don't want your question to be closed, just improved. How do you want us to help without any piece of source code? – Bsquare ℬℬ Dec 03 '18 at 13:37
  • @Bsquare I think experienced developers can have some clue and then provide an answer or ask me something further. Sometimes source code is not useful (it works on Android4 and yields syntax error on Android7). – P5music Dec 03 '18 at 13:53

1 Answers1

0

Many reasons can lead to errors like that or similar (often the error message is not helpful or meaningful). First of all, if you can have minimum SDK = API level 19 (KITKAT, Android 4.4) or above, use

webView.evaluateJavascript(String script,new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String s) {

                        }
                    });

instead of

webView.loadUrl("javascript: doSomething();");

Android versions from KITKAT above (4.4) have the Chrome WebView instead of the old Android WebView, and the new one has problems with loadUrl for JavaScript injection, it is more strict in evaluating JavaScript (or it has some bugs), in fact.

With loadUrl (and in general) be careful about:

-Using double quotes instead of single quotes marks for string literals delimiters (but maybe not in some special cases),

example:

ch=""\"; //it is good
ch='"'; // it is bad

-When injecting code into an event listener using double quotes as delimiter in source code lines,

example:

var string="parameter";
element.setAttribute("onclick","doSomething('"+stringVar+"');return false;"); // good

-Using curly brakets after "if" clause, not a single direct statement, example:

if (i>0) {doSomething(i);} // good 

if (i>0) doSomething(i); // bad

-Being careful with regular expressions (some characters can be hurtful for the regular expression itself, the result, and maybe the JavaScript interpreter/compiler).

P5music
  • 3,197
  • 2
  • 32
  • 81