165e62f4f908394fc469cf535fef7c16035a428a2Scott Mainpage.title=Debugging Web Apps
265e62f4f908394fc469cf535fef7c16035a428a2Scott Main@jd:body
365e62f4f908394fc469cf535fef7c16035a428a2Scott Main
465e62f4f908394fc469cf535fef7c16035a428a2Scott Main<div id="qv-wrapper">
565e62f4f908394fc469cf535fef7c16035a428a2Scott Main<div id="qv">
665e62f4f908394fc469cf535fef7c16035a428a2Scott Main<h2>Quickview</h2>
765e62f4f908394fc469cf535fef7c16035a428a2Scott Main<ul>
865e62f4f908394fc469cf535fef7c16035a428a2Scott Main  <li>You can debug your web app using console methods in JavaScript</li>
965e62f4f908394fc469cf535fef7c16035a428a2Scott Main  <li>If debugging in a custom WebView, you need to implement a callback method to handle debug
1065e62f4f908394fc469cf535fef7c16035a428a2Scott Mainmessages</li>
1165e62f4f908394fc469cf535fef7c16035a428a2Scott Main</ul>
1265e62f4f908394fc469cf535fef7c16035a428a2Scott Main
1365e62f4f908394fc469cf535fef7c16035a428a2Scott Main<h2>In this document</h2>
1465e62f4f908394fc469cf535fef7c16035a428a2Scott Main<ol>
1565e62f4f908394fc469cf535fef7c16035a428a2Scott Main  <li><a href="#Browser">Using Console APIs in the Android Browser</a></li>
1665e62f4f908394fc469cf535fef7c16035a428a2Scott Main  <li><a href="#WebView">Using Console APIs in WebView</a></li>
1765e62f4f908394fc469cf535fef7c16035a428a2Scott Main</ol>
1865e62f4f908394fc469cf535fef7c16035a428a2Scott Main
1965e62f4f908394fc469cf535fef7c16035a428a2Scott Main<h2>See also</h2>
2065e62f4f908394fc469cf535fef7c16035a428a2Scott Main<ol>
215342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott Main  <li><a class="external-link"
225342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott Mainhref="https://developers.google.com/chrome-developer-tools/docs/remote-debugging">Remote
235342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott MainDebugging on Android</a></li>
2450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li><a href="{@docRoot}tools/debugging/index.html">Debugging</a></li>
2565e62f4f908394fc469cf535fef7c16035a428a2Scott Main</ol>
2665e62f4f908394fc469cf535fef7c16035a428a2Scott Main
2765e62f4f908394fc469cf535fef7c16035a428a2Scott Main</div>
2865e62f4f908394fc469cf535fef7c16035a428a2Scott Main</div>
2965e62f4f908394fc469cf535fef7c16035a428a2Scott Main
305342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott Main<p>If you are testing your web app with a device running Android 4.4 or higher,
315342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott Mainyou can remotely debug your web pages in {@link android.webkit.WebView} with
325342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott MainChrome Developer Tools, while continuing to support older versions of Android.
335342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott MainFor more information, see <a class="external-link"
345342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott Mainhref="https://developers.google.com/chrome-developer-tools/docs/remote-debugging">Remote
355342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott MainDebugging on Android</a>.</p>
365342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott Main
375342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott Main<p>If you don't have a device running Android 4.4 or higher, you can debug your JavaScript using the
385342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott Main{@code console} JavaScript APIs and view the output messages to logcat. If you're familiar with
3965e62f4f908394fc469cf535fef7c16035a428a2Scott Maindebugging web pages with Firebug or Web Inspector, then you're probably familiar
4065e62f4f908394fc469cf535fef7c16035a428a2Scott Mainwith using {@code console} (such as {@code console.log()}). Android's WebKit framework supports most
4165e62f4f908394fc469cf535fef7c16035a428a2Scott Mainof the same APIs, so you can receive logs from your web page when debugging in Android's Browser
425342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott Mainor in your own {@link android.webkit.WebView}. This document describes how to use the
435342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott Mainconsole APIs for debugging.</p>
4465e62f4f908394fc469cf535fef7c16035a428a2Scott Main
4565e62f4f908394fc469cf535fef7c16035a428a2Scott Main
4665e62f4f908394fc469cf535fef7c16035a428a2Scott Main<h2 id="Browser">Using Console APIs in the Android Browser</h2>
4765e62f4f908394fc469cf535fef7c16035a428a2Scott Main
4865e62f4f908394fc469cf535fef7c16035a428a2Scott Main<div class="sidebox-wrapper">
4965e62f4f908394fc469cf535fef7c16035a428a2Scott Main<div class="sidebox">
5065e62f4f908394fc469cf535fef7c16035a428a2Scott Main  <h2>Logcat</h2>
5165e62f4f908394fc469cf535fef7c16035a428a2Scott Main  <p>Logcat is a tool that dumps a log of system messages. The messages include a stack trace when
5265e62f4f908394fc469cf535fef7c16035a428a2Scott Mainthe device throws an error, as well as log messages written from your application and
5365e62f4f908394fc469cf535fef7c16035a428a2Scott Mainthose written using JavaScript {@code console} APIs.</p>
5465e62f4f908394fc469cf535fef7c16035a428a2Scott Main  <p>To run logcat and view messages, execute
5565e62f4f908394fc469cf535fef7c16035a428a2Scott Main{@code adb logcat} from your Android SDK {@code tools/} directory, or, from DDMS, select
56682c24e22811d4ee17ae1cd61bf255c3f7e722b7Trevor Johns<strong>Device > Run logcat</strong>.</p>
5750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <p>See <a href="{@docRoot}tools/debugging/debugging-log.html">Debugging</a>
581bcc56db0715b5452c9b003c275733c1275d9d8cRobert Ly  for more information about <codelogcat</code>.</p>
5965e62f4f908394fc469cf535fef7c16035a428a2Scott Main</div>
6065e62f4f908394fc469cf535fef7c16035a428a2Scott Main</div>
6165e62f4f908394fc469cf535fef7c16035a428a2Scott Main
6265e62f4f908394fc469cf535fef7c16035a428a2Scott Main<p>When you call a {@code console} function (in the DOM's {@code window.console} object),
6365e62f4f908394fc469cf535fef7c16035a428a2Scott Mainthe output appears in logcat. For example, if your web page executes the following
6465e62f4f908394fc469cf535fef7c16035a428a2Scott MainJavaScript:</p>
6565e62f4f908394fc469cf535fef7c16035a428a2Scott Main<pre>
6665e62f4f908394fc469cf535fef7c16035a428a2Scott Mainconsole.log("Hello World");
6765e62f4f908394fc469cf535fef7c16035a428a2Scott Main</pre>
6865e62f4f908394fc469cf535fef7c16035a428a2Scott Main<p>Then the logcat message looks something like this:</p>
6965e62f4f908394fc469cf535fef7c16035a428a2Scott Main<pre class="no-pretty-print">
7065e62f4f908394fc469cf535fef7c16035a428a2Scott MainConsole: Hello World http://www.example.com/hello.html :82
7165e62f4f908394fc469cf535fef7c16035a428a2Scott Main</pre>
7265e62f4f908394fc469cf535fef7c16035a428a2Scott Main
7365e62f4f908394fc469cf535fef7c16035a428a2Scott Main<p>The format of the message might appear different depending on which version of Android you're
7465e62f4f908394fc469cf535fef7c16035a428a2Scott Mainusing. On Android 2.1 and higher, console messages from the Android Browser
7565e62f4f908394fc469cf535fef7c16035a428a2Scott Mainare tagged with the name "browser". On Android 1.6 and lower, Android Browser
7665e62f4f908394fc469cf535fef7c16035a428a2Scott Mainmessages are tagged with the name "WebCore".</p>
7765e62f4f908394fc469cf535fef7c16035a428a2Scott Main
7865e62f4f908394fc469cf535fef7c16035a428a2Scott Main<p>Android's WebKit does not implement all of the console APIs available in other desktop browsers.
7965e62f4f908394fc469cf535fef7c16035a428a2Scott MainYou can, however, use the basic text logging functions:</p>
8065e62f4f908394fc469cf535fef7c16035a428a2Scott Main<ul>
8165e62f4f908394fc469cf535fef7c16035a428a2Scott Main  <li>{@code console.log(String)}</li>
8265e62f4f908394fc469cf535fef7c16035a428a2Scott Main  <li>{@code console.info(String)}</li>
8365e62f4f908394fc469cf535fef7c16035a428a2Scott Main  <li>{@code console.warn(String)}</li>
8465e62f4f908394fc469cf535fef7c16035a428a2Scott Main  <li>{@code console.error(String)}</li>
8565e62f4f908394fc469cf535fef7c16035a428a2Scott Main</ul>
8665e62f4f908394fc469cf535fef7c16035a428a2Scott Main
8765e62f4f908394fc469cf535fef7c16035a428a2Scott Main<p>Other console functions don't raise errors, but might not behave the same as what you
8865e62f4f908394fc469cf535fef7c16035a428a2Scott Mainexpect from other web browsers.</p>
8965e62f4f908394fc469cf535fef7c16035a428a2Scott Main
9065e62f4f908394fc469cf535fef7c16035a428a2Scott Main
9165e62f4f908394fc469cf535fef7c16035a428a2Scott Main
9265e62f4f908394fc469cf535fef7c16035a428a2Scott Main<h2 id="WebView">Using Console APIs in WebView</h2>
9365e62f4f908394fc469cf535fef7c16035a428a2Scott Main
945342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott Main<p>All the console APIs shown above are also
95c4a0139e26ca56cb0f635416ef04c79473146304Mark Lusupported when debugging in {@link android.webkit.WebView}.
965342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott MainIf you're targeting Android 2.1 (API level 7) and higher, you must
9765e62f4f908394fc469cf535fef7c16035a428a2Scott Mainprovide a {@link android.webkit.WebChromeClient}
9865e62f4f908394fc469cf535fef7c16035a428a2Scott Mainthat implements the {@link android.webkit.WebChromeClient#onConsoleMessage(String,int,String)
995342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott MainonConsoleMessage()} method in order for console messages to appear in logcat.
1005342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott MainThen, apply the {@link android.webkit.WebChromeClient} to your {@link
10165e62f4f908394fc469cf535fef7c16035a428a2Scott Mainandroid.webkit.WebView} with {@link android.webkit.WebView#setWebChromeClient(WebChromeClient)
10265e62f4f908394fc469cf535fef7c16035a428a2Scott MainsetWebChromeClient()}.
10365e62f4f908394fc469cf535fef7c16035a428a2Scott Main
1045342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott Main<p>For example, to support API level 7, this is how your code for {@link
10565e62f4f908394fc469cf535fef7c16035a428a2Scott Mainandroid.webkit.WebChromeClient#onConsoleMessage(String,int,String)} might look:</p>
10665e62f4f908394fc469cf535fef7c16035a428a2Scott Main
10765e62f4f908394fc469cf535fef7c16035a428a2Scott Main<pre>
10865e62f4f908394fc469cf535fef7c16035a428a2Scott MainWebView myWebView = (WebView) findViewById(R.id.webview);
10965e62f4f908394fc469cf535fef7c16035a428a2Scott MainmyWebView.setWebChromeClient(new WebChromeClient() {
11065e62f4f908394fc469cf535fef7c16035a428a2Scott Main  public void onConsoleMessage(String message, int lineNumber, String sourceID) {
11165e62f4f908394fc469cf535fef7c16035a428a2Scott Main    Log.d("MyApplication", message + " -- From line "
11265e62f4f908394fc469cf535fef7c16035a428a2Scott Main                         + lineNumber + " of "
11365e62f4f908394fc469cf535fef7c16035a428a2Scott Main                         + sourceID);
11465e62f4f908394fc469cf535fef7c16035a428a2Scott Main  }
11565e62f4f908394fc469cf535fef7c16035a428a2Scott Main});
11665e62f4f908394fc469cf535fef7c16035a428a2Scott Main</pre>
11765e62f4f908394fc469cf535fef7c16035a428a2Scott Main
1185342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott Main<p>However, if your lowest supported version is API level 8 or higher, you should instead
1195342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott Mainimplement {@link android.webkit.WebChromeClient#onConsoleMessage(ConsoleMessage)}. For example:</p>
12065e62f4f908394fc469cf535fef7c16035a428a2Scott Main
12165e62f4f908394fc469cf535fef7c16035a428a2Scott Main<pre>
12265e62f4f908394fc469cf535fef7c16035a428a2Scott MainWebView myWebView = (WebView) findViewById(R.id.webview);
12365e62f4f908394fc469cf535fef7c16035a428a2Scott MainmyWebView.setWebChromeClient(new WebChromeClient() {
12465e62f4f908394fc469cf535fef7c16035a428a2Scott Main  public boolean onConsoleMessage(ConsoleMessage cm) {
12565e62f4f908394fc469cf535fef7c16035a428a2Scott Main    Log.d("MyApplication", cm.{@link android.webkit.ConsoleMessage#message()} + " -- From line "
12665e62f4f908394fc469cf535fef7c16035a428a2Scott Main                         + cm.{@link android.webkit.ConsoleMessage#lineNumber()} + " of "
12765e62f4f908394fc469cf535fef7c16035a428a2Scott Main                         + cm.{@link android.webkit.ConsoleMessage#sourceId()} );
12865e62f4f908394fc469cf535fef7c16035a428a2Scott Main    return true;
12965e62f4f908394fc469cf535fef7c16035a428a2Scott Main  }
13065e62f4f908394fc469cf535fef7c16035a428a2Scott Main});
13165e62f4f908394fc469cf535fef7c16035a428a2Scott Main</pre>
13265e62f4f908394fc469cf535fef7c16035a428a2Scott Main
13365e62f4f908394fc469cf535fef7c16035a428a2Scott Main<p>The {@link android.webkit.ConsoleMessage} also includes a {@link
1345342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott Mainandroid.webkit.ConsoleMessage.MessageLevel MessageLevel} object to indicate the type of console
1355342f65aa81005fa6ba310d0d35ec41f3d0bff00Scott Mainmessage being delivered. You can query the message level with {@link
13665e62f4f908394fc469cf535fef7c16035a428a2Scott Mainandroid.webkit.ConsoleMessage#messageLevel()} to determine the severity of the message, then
13765e62f4f908394fc469cf535fef7c16035a428a2Scott Mainuse the appropriate {@link android.util.Log} method or take other appropriate actions.</p>
13865e62f4f908394fc469cf535fef7c16035a428a2Scott Main
13965e62f4f908394fc469cf535fef7c16035a428a2Scott Main<p>Whether you're using {@link
14065e62f4f908394fc469cf535fef7c16035a428a2Scott Mainandroid.webkit.WebChromeClient#onConsoleMessage(String,int,String)} or {@link
14165e62f4f908394fc469cf535fef7c16035a428a2Scott Mainandroid.webkit.WebChromeClient#onConsoleMessage(ConsoleMessage)}, when you execute a console method
14265e62f4f908394fc469cf535fef7c16035a428a2Scott Mainin your web page, Android calls the appropriate {@link
14365e62f4f908394fc469cf535fef7c16035a428a2Scott Mainandroid.webkit.WebChromeClient#onConsoleMessage(String,int,String)
14465e62f4f908394fc469cf535fef7c16035a428a2Scott MainonConsoleMessage()} method so you can report the error. For example, with the example code above,
14565e62f4f908394fc469cf535fef7c16035a428a2Scott Maina logcat message is printed that looks like this:</p>
14665e62f4f908394fc469cf535fef7c16035a428a2Scott Main
14765e62f4f908394fc469cf535fef7c16035a428a2Scott Main<pre class="no-pretty-print">
14865e62f4f908394fc469cf535fef7c16035a428a2Scott MainHello World -- From line 82 of http://www.example.com/hello.html
14965e62f4f908394fc469cf535fef7c16035a428a2Scott Main</pre>
15065e62f4f908394fc469cf535fef7c16035a428a2Scott Main
15165e62f4f908394fc469cf535fef7c16035a428a2Scott Main
15265e62f4f908394fc469cf535fef7c16035a428a2Scott Main
15365e62f4f908394fc469cf535fef7c16035a428a2Scott Main
15465e62f4f908394fc469cf535fef7c16035a428a2Scott Main
15565e62f4f908394fc469cf535fef7c16035a428a2Scott Main
156