Javascript console.log() on HTC Android devices and adb logcat
In the default browser on Android 2.3.3 (and presumably from then on) you can simply use the built in javascript console:
- open the browser
- go to your webpage
- type
about:debug
in the address bar and hit enter - you'll see a section for debug options appear if you go into the browser app's settings
- tick "Show JavaScript Console" if not already enabled
- refresh your webpage
At the top, you'll see a bar labeled "JavaScript Console".
I have been using three different HTC phones, almost exclusively, and have never had this issue. Here are a few things to check:
- Make sure USB debugging is enabled.
- Make sure your Webview has a WebChromeClient set. The browser used in Webview does not implement console.log().
- It should be easy to see the output of adb logcat, but to make it easier, filter the output.
Turn on USB debugging:
- Disconnect your device from your computer.
- Go to Settings -> Applications -> Development -> Select "Enable USB Debugging"
- Plugin to computer. (Make sure you have the correct drivers installed to use ADB - more info here: http://developer.android.com/guide/developing/device.html)
Set a WebChromeClient that overrides onConsoleMessage():
//Set the output prefix so you can filter adb logcat results laterpublic static final String TAG = "Your-Application-Name";myWebView = (WebView) findViewById(R.id.webview);//Not going to have much luck running JS without this:myWebView.getSettings().setJavaScriptEnabled(true);//Override onConsoleMessage() to output to the Log.myWebView.setWebChromeClient(new WebChromeClient() { @Override public boolean onConsoleMessage(ConsoleMessage cm) { Log.d(TAG, cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId() ); return true; }});
More info on onConsoleMessage() here: http://developer.android.com/reference/android/webkit/WebChromeClient.html#onConsoleMessage(java.lang.String, int, java.lang.String)
More info on debugging in general here: http://developer.android.com/guide/webapps/debugging.html
Filter the output of adb logcat:
adb logcat tag-name:log-level *:S
tag-name matches the string specified in Log.xlog-level matches the log level you indicated when calling Log.x <---
Example relating to code above:
adb logcat Your-Application-Name:D *:S
This will show all d level logs for the matching tag, Your-Application-Name, and silence everything else.
More info on adb filtering here:http://developer.android.com/guide/developing/tools/adb.html#logcat
Hope it helps! I know it was a bit of summarizing of the other answers, but, the fact is, it takes all of the steps to make it work. :)
I had the same problem, I solve it by doing the following:
1/ when you initialize your webview, add a javascript bridge class:
appView.addJavascriptInterface(new JSBridge(), "JSBridge");
2/ create the class JSBridge with a log function
class JSBridge { public void log(String msg){ Log.d(msg); }}
3/ in your javascript you can now call
JSBridge.log("my log message");
Might be a bit overkill for your problem, but you can also do a LOT more with that solution