blob: 9e8e11337b73a8f3f0b20faf0ee9f7616b75b65b [file] [log] [blame]
Scott Main65e62f42010-09-20 12:46:34 -07001page.title=Debugging Web Apps
2@jd:body
3
4<div id="qv-wrapper">
5<div id="qv">
6<h2>Quickview</h2>
7<ul>
8 <li>You can debug your web app using console methods in JavaScript</li>
9 <li>If debugging in a custom WebView, you need to implement a callback method to handle debug
10messages</li>
11</ul>
12
13<h2>In this document</h2>
14<ol>
15 <li><a href="#Browser">Using Console APIs in the Android Browser</a></li>
16 <li><a href="#WebView">Using Console APIs in WebView</a></li>
17</ol>
18
19<h2>See also</h2>
20<ol>
Scott Main5342f652013-09-10 10:54:46 -070021 <li><a class="external-link"
22href="https://developers.google.com/chrome-developer-tools/docs/remote-debugging">Remote
23Debugging on Android</a></li>
Scott Main50e990c2012-06-21 17:14:39 -070024 <li><a href="{@docRoot}tools/debugging/index.html">Debugging</a></li>
Scott Main65e62f42010-09-20 12:46:34 -070025</ol>
26
27</div>
28</div>
29
Scott Main5342f652013-09-10 10:54:46 -070030<p>If you are testing your web app with a device running Android 4.4 or higher,
31you can remotely debug your web pages in {@link android.webkit.WebView} with
32Chrome Developer Tools, while continuing to support older versions of Android.
33For more information, see <a class="external-link"
34href="https://developers.google.com/chrome-developer-tools/docs/remote-debugging">Remote
35Debugging on Android</a>.</p>
36
37<p>If you don't have a device running Android 4.4 or higher, you can debug your JavaScript using the
38{@code console} JavaScript APIs and view the output messages to logcat. If you're familiar with
Scott Main65e62f42010-09-20 12:46:34 -070039debugging web pages with Firebug or Web Inspector, then you're probably familiar
40with using {@code console} (such as {@code console.log()}). Android's WebKit framework supports most
41of the same APIs, so you can receive logs from your web page when debugging in Android's Browser
Scott Main5342f652013-09-10 10:54:46 -070042or in your own {@link android.webkit.WebView}. This document describes how to use the
43console APIs for debugging.</p>
Scott Main65e62f42010-09-20 12:46:34 -070044
45
46<h2 id="Browser">Using Console APIs in the Android Browser</h2>
47
48<div class="sidebox-wrapper">
49<div class="sidebox">
50 <h2>Logcat</h2>
51 <p>Logcat is a tool that dumps a log of system messages. The messages include a stack trace when
52the device throws an error, as well as log messages written from your application and
53those written using JavaScript {@code console} APIs.</p>
54 <p>To run logcat and view messages, execute
55{@code adb logcat} from your Android SDK {@code tools/} directory, or, from DDMS, select
Trevor Johns682c24e2016-04-12 10:13:47 -070056<strong>Device > Run logcat</strong>.</p>
Scott Main50e990c2012-06-21 17:14:39 -070057 <p>See <a href="{@docRoot}tools/debugging/debugging-log.html">Debugging</a>
Robert Ly1bcc56d2011-01-05 00:34:26 -080058 for more information about <codelogcat</code>.</p>
Scott Main65e62f42010-09-20 12:46:34 -070059</div>
60</div>
61
62<p>When you call a {@code console} function (in the DOM's {@code window.console} object),
63the output appears in logcat. For example, if your web page executes the following
64JavaScript:</p>
65<pre>
66console.log("Hello World");
67</pre>
68<p>Then the logcat message looks something like this:</p>
69<pre class="no-pretty-print">
70Console: Hello World http://www.example.com/hello.html :82
71</pre>
72
73<p>The format of the message might appear different depending on which version of Android you're
74using. On Android 2.1 and higher, console messages from the Android Browser
75are tagged with the name "browser". On Android 1.6 and lower, Android Browser
76messages are tagged with the name "WebCore".</p>
77
78<p>Android's WebKit does not implement all of the console APIs available in other desktop browsers.
79You can, however, use the basic text logging functions:</p>
80<ul>
81 <li>{@code console.log(String)}</li>
82 <li>{@code console.info(String)}</li>
83 <li>{@code console.warn(String)}</li>
84 <li>{@code console.error(String)}</li>
85</ul>
86
87<p>Other console functions don't raise errors, but might not behave the same as what you
88expect from other web browsers.</p>
89
90
91
92<h2 id="WebView">Using Console APIs in WebView</h2>
93
Scott Main5342f652013-09-10 10:54:46 -070094<p>All the console APIs shown above are also
Mark Luc4a01392016-07-18 10:42:11 -070095supported when debugging in {@link android.webkit.WebView}.
Scott Main5342f652013-09-10 10:54:46 -070096If you're targeting Android 2.1 (API level 7) and higher, you must
Scott Main65e62f42010-09-20 12:46:34 -070097provide a {@link android.webkit.WebChromeClient}
98that implements the {@link android.webkit.WebChromeClient#onConsoleMessage(String,int,String)
Scott Main5342f652013-09-10 10:54:46 -070099onConsoleMessage()} method in order for console messages to appear in logcat.
100Then, apply the {@link android.webkit.WebChromeClient} to your {@link
Scott Main65e62f42010-09-20 12:46:34 -0700101android.webkit.WebView} with {@link android.webkit.WebView#setWebChromeClient(WebChromeClient)
102setWebChromeClient()}.
103
Scott Main5342f652013-09-10 10:54:46 -0700104<p>For example, to support API level 7, this is how your code for {@link
Scott Main65e62f42010-09-20 12:46:34 -0700105android.webkit.WebChromeClient#onConsoleMessage(String,int,String)} might look:</p>
106
107<pre>
108WebView myWebView = (WebView) findViewById(R.id.webview);
109myWebView.setWebChromeClient(new WebChromeClient() {
110 public void onConsoleMessage(String message, int lineNumber, String sourceID) {
111 Log.d("MyApplication", message + " -- From line "
112 + lineNumber + " of "
113 + sourceID);
114 }
115});
116</pre>
117
Scott Main5342f652013-09-10 10:54:46 -0700118<p>However, if your lowest supported version is API level 8 or higher, you should instead
119implement {@link android.webkit.WebChromeClient#onConsoleMessage(ConsoleMessage)}. For example:</p>
Scott Main65e62f42010-09-20 12:46:34 -0700120
121<pre>
122WebView myWebView = (WebView) findViewById(R.id.webview);
123myWebView.setWebChromeClient(new WebChromeClient() {
124 public boolean onConsoleMessage(ConsoleMessage cm) {
125 Log.d("MyApplication", cm.{@link android.webkit.ConsoleMessage#message()} + " -- From line "
126 + cm.{@link android.webkit.ConsoleMessage#lineNumber()} + " of "
127 + cm.{@link android.webkit.ConsoleMessage#sourceId()} );
128 return true;
129 }
130});
131</pre>
132
133<p>The {@link android.webkit.ConsoleMessage} also includes a {@link
Scott Main5342f652013-09-10 10:54:46 -0700134android.webkit.ConsoleMessage.MessageLevel MessageLevel} object to indicate the type of console
135message being delivered. You can query the message level with {@link
Scott Main65e62f42010-09-20 12:46:34 -0700136android.webkit.ConsoleMessage#messageLevel()} to determine the severity of the message, then
137use the appropriate {@link android.util.Log} method or take other appropriate actions.</p>
138
139<p>Whether you're using {@link
140android.webkit.WebChromeClient#onConsoleMessage(String,int,String)} or {@link
141android.webkit.WebChromeClient#onConsoleMessage(ConsoleMessage)}, when you execute a console method
142in your web page, Android calls the appropriate {@link
143android.webkit.WebChromeClient#onConsoleMessage(String,int,String)
144onConsoleMessage()} method so you can report the error. For example, with the example code above,
145a logcat message is printed that looks like this:</p>
146
147<pre class="no-pretty-print">
148Hello World -- From line 82 of http://www.example.com/hello.html
149</pre>
150
151
152
153
154
155