page.title=Debugging Web Apps @jd:body

Quickview

In this document

  1. Using Console APIs in the Android Browser
  2. Using Console APIs in WebView

See also

  1. Debugging

If you're developing a web application for Android, you can debug your JavaScript using the {@code console} JavaScript APIs, which output messages to logcat. If you're familiar with debugging web pages with Firebug or Web Inspector, then you're probably familiar with using {@code console} (such as {@code console.log()}). Android's WebKit framework supports most of the same APIs, so you can receive logs from your web page when debugging in Android's Browser or in your own {@link android.webkit.WebView}.

Using Console APIs in the Android Browser

When you call a {@code console} function (in the DOM's {@code window.console} object), the output appears in logcat. For example, if your web page executes the following JavaScript:

console.log("Hello World");

Then the logcat message looks something like this:

Console: Hello World http://www.example.com/hello.html :82

The format of the message might appear different depending on which version of Android you're using. On Android 2.1 and higher, console messages from the Android Browser are tagged with the name "browser". On Android 1.6 and lower, Android Browser messages are tagged with the name "WebCore".

Android's WebKit does not implement all of the console APIs available in other desktop browsers. You can, however, use the basic text logging functions:

Other console functions don't raise errors, but might not behave the same as what you expect from other web browsers.

Using Console APIs in WebView

If you've implemented a custom {@link android.webkit.WebView} in your application, all the same console APIs are supported when debugging your web page in WebView. On Android 1.6 and lower, console messages are automatically sent to logcat with the "WebCore" logging tag. If you're targeting Android 2.1 (API Level 7) or higher, then you must provide a {@link android.webkit.WebChromeClient} that implements the {@link android.webkit.WebChromeClient#onConsoleMessage(String,int,String) onConsoleMessage()} callback method, in order for console messages to appear in logcat.

Additionally, the {@link android.webkit.WebChromeClient#onConsoleMessage(String,int,String)} method introduced in API Level 7 has been deprecated in favor of {@link android.webkit.WebChromeClient#onConsoleMessage(ConsoleMessage)} in API Level 8.

Whether you're developing for Android 2.1 (API Level 7) or Android 2.2 (API Level 8 or greater), you must implement {@link android.webkit.WebChromeClient} and override the appropriate {@link android.webkit.WebChromeClient#onConsoleMessage(String,int,String) onConsoleMessage()} callback method. Then, apply the {@link android.webkit.WebChromeClient} to your {@link android.webkit.WebView} with {@link android.webkit.WebView#setWebChromeClient(WebChromeClient) setWebChromeClient()}.

Using API Level 7, this is how your code for {@link android.webkit.WebChromeClient#onConsoleMessage(String,int,String)} might look:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebChromeClient(new WebChromeClient() {
  public void onConsoleMessage(String message, int lineNumber, String sourceID) {
    Log.d("MyApplication", message + " -- From line "
                         + lineNumber + " of "
                         + sourceID);
  }
});

With API Level 8 or greater, your code for {@link android.webkit.WebChromeClient#onConsoleMessage(ConsoleMessage)} might look like this:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebChromeClient(new WebChromeClient() {
  public boolean onConsoleMessage(ConsoleMessage cm) {
    Log.d("MyApplication", cm.{@link android.webkit.ConsoleMessage#message()} + " -- From line "
                         + cm.{@link android.webkit.ConsoleMessage#lineNumber()} + " of "
                         + cm.{@link android.webkit.ConsoleMessage#sourceId()} );
    return true;
  }
});

The {@link android.webkit.ConsoleMessage} also includes a {@link android.webkit.ConsoleMessage.MessageLevel MessageLevel} to indicate the type of console message being delivered. You can query the message level with {@link android.webkit.ConsoleMessage#messageLevel()} to determine the severity of the message, then use the appropriate {@link android.util.Log} method or take other appropriate actions.

Whether you're using {@link android.webkit.WebChromeClient#onConsoleMessage(String,int,String)} or {@link android.webkit.WebChromeClient#onConsoleMessage(ConsoleMessage)}, when you execute a console method in your web page, Android calls the appropriate {@link android.webkit.WebChromeClient#onConsoleMessage(String,int,String) onConsoleMessage()} method so you can report the error. For example, with the example code above, a logcat message is printed that looks like this:

Hello World -- From line 82 of http://www.example.com/hello.html