Troubleshooting WebView by printing console messages or by remote debugging
suggest changePrinting WebView console messages to logcat
To handle console messages from a web page you can override onConsoleMessage in WebChromeClient:
final class ChromeClient extends WebChromeClient {
@Override
public boolean onConsoleMessage(ConsoleMessage msg) {
Log.d(
"WebView",
String.format("%s %s:%d", msg.message(), msg.sourceId(), msg.lineNumber())
);
return true;
}
}
And set it in your activity or fragment:
webView.setWebChromeClient(new ChromeClient());
So this sample page:
<html>
<head>
<script type="text/javascript">
console.log('test message');
</script>
</head>
<body>
</body>
</html>
will write this line to logcat:
WebView: test message sample.html:4
console.info(), console.warn(), and console.error() are also delivered to this callback.
Remote debugging Android devices with Chrome
You can remote-debug a WebView-based application from your desktop Chrome. Call WebView.setWebContentsDebuggingEnabled(true) in the app. USB debugging alone does not expose the WebView.
Enable USB debugging on your Android device
On your Android device, open up Settings, find the Developer options section, and enable USB debugging.
Connect and discover your Android device
Open the following page in Chrome: chrome://inspect/#devices
From the Inspect Devices dialog, select your device and press inspect. A new instance of Chrome’s DevTools opens up on your development machine.
A more detailed guideline and description of DevTools can be found on developers.google.com