androidwebview

Get Javascript console when emulating WebView app


I'm pretty new to Android Dev, I'm trying to create a WebView based app. I got the basics, I emulate the app directly on my phone and wanted to know if there is a way to get a Javascript console like in Chrome PC version.

My device is running Android 4.2.1 API Level 17. I can't emulate app on my computer, it doesn't work.

I've already watched this topic on Stackoverflow, but hasn't helped me.

So, what I tried (examples from the Stackoverflow topic) :

Can someone help me? I really don't know how to get a console, and really need it...

Thanks!


Solution

  • The answer you are referring to is for Android Browser app, not for WebView component.

    In order to get output from console.log() family of functions, you need to set a WebChromeClient for your WebView and override onConsoleMessage() (doc) method, like this:

    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
            android.util.Log.d("WebView", consoleMessage.message());
            return true;
        }
    });