android – Get Javascript console when emulating WebView app-ThrowExceptions

Exception or error:

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) :

  • Remote debugging from my device to my computer : I can’t cause my device is running Android 4.2.1 and need, at least, Android 4.4+.

  • JavaScript Console : I can’t access and didn’t implemented a browser bar into my WebView app (and don’t want to implement one…)

  • Original Answer : I put some console.log('test') and console.error('test') on a window.onload function to see if I can find them on the logcat and ADB logs but I can’t find them, even if I put adb logcat to search them.

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

Thanks!

How to solve:

The answer you a 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;
    }
});

Leave a Reply

Your email address will not be published. Required fields are marked *