summaryrefslogtreecommitdiffstats
path: root/docs/html/tools/help/adb.jd
blob: d44d54be04083c66da4a4f3698f48203dcfe25b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
page.title=Android Debug Bridge
parent.title=Tools
parent.link=index.html
@jd:body

<div id="qv-wrapper">
<div id="qv">
  <h2>ADB quickview</h2>
  <ul>
<li>Manage the state of an emulator or device</li>
<li>Run shell commands on a device</li>
<li>Manage port forwarding on an emulator or device</li>
<li>Copy files to/from an emulator or device</li>
  </ul>

  <h2>In this document</h2>
  <ol>
<li><a href="#issuingcommands">Issuing ADB Commands</a></li>
<li><a href="#devicestatus">Querying for Emulator/Device Instances</a></li>
<li><a href="#directingcommands">Directing Commands to a Specific Emulator/Device Instance</a></li>
<li><a href="#move">Installing an Application</a></li>
<li><a href="#forwardports">Forwarding Ports</a></li>
<li><a href="#copyfiles">Copying Files to or from an Emulator/Device Instance</a></li>
<li><a href="#commandsummary">Listing of adb Commands </a></li>
<li><a href="#shellcommands">Issuing Shell Commands</a></li>
<li><a href="#logcat">Enabling Logcat Logging</a></li>
<li><a href="#stopping">Stopping the adb Server</a></li>
  </ol>

  <h2>See also</h2>
  <ol>
    <li><a href="emulator.html">Emulator</a></li>
  </ol>

</div>
</div>

<p>Android Debug Bridge (adb) is a versatile command line tool that lets you communicate with an
emulator instance or connected Android-powered device. It is a client-server program that includes
three components: </p>

<ul>
  <li>A client, which runs on your development machine. You can invoke a client from a shell by issuing an adb command. Other Android tools such as the ADT plugin and DDMS also create adb clients. </li>
  <li>A server, which runs as a background process on your development machine. The server manages communication between the client and the adb daemon running on an emulator or device. </li>
  <li>A daemon, which runs as a background process on each emulator or device instance. </li>
</ul>

<p>You can find the {@code adb} tool in {@code &lt;sdk&gt;/platform-tools/}.</p>

<p>When you start an adb client, the client first checks whether there is an adb server process already running. If there isn't, it starts the server process. When the server starts, it binds to local TCP port 5037 and listens for commands sent from adb clients&mdash;all adb clients use port 5037 to communicate with the adb server. </p>

<p>The server then sets up connections to all running emulator/device instances. It locates emulator/device instances by scanning odd-numbered ports in the range 5555 to 5585, the range used by emulators/devices. Where the server finds an adb daemon, it sets up a connection to that port. Note that each emulator/device instance acquires a pair of sequential ports &mdash; an even-numbered port for console connections and an odd-numbered port for adb connections. For example: </p>

<p style="margin-left:2em">
Emulator 1, console: 5554<br/>
Emulator 1, adb: 5555<br>
Emulator 2, console: 5556<br>
Emulator 2, adb: 5557 ...
</p>

<p>As shown, the emulator instance connected to adb on port 5555 is the same as the instance whose console listens on port 5554. </p>

<p>Once the server has set up connections to all emulator instances, you can use adb commands to control and access those instances. Because the server manages connections to emulator/device instances and handles commands from multiple adb clients, you can control any emulator/device instance from any client (or from a script).</p>

<p>The sections below describe the commands that you can use to access adb capabilities and manage the state of an emulator/device. Note that if you are developing Android applications in Eclipse and have installed the ADT plugin, you do not need to access adb from the command line. The ADT plugin provides a transparent integration of adb into the Eclipse IDE. However, you can still use adb directly as necessary, such as for debugging.</p>

<a name="issuingcommands"></a>

<h2>Issuing adb Commands</h2>

<p>You can issue adb commands from a command line on your development machine or from a script. The usage is: </p>

    <pre>adb [-d|-e|-s &lt;serialNumber&gt;] &lt;command&gt; </pre>

<p>When you issue a command, the program invokes an adb client. The client is not specifically associated with any emulator instance, so if multiple emulators/devices are running, you need to use the <code>-d</code> option to specify the target instance to which the command should be directed. For more information about using this option, see <a href="#directingcommands">Directing Commands to a Specific Emulator/Device Instance</a>. </p>

<a name="devicestatus"></a>

<h2>Querying for Emulator/Device Instances</h2>

<p>Before issuing adb commands, it is helpful to know what emulator/device instances are connected to the adb server. You can generate a list of attached emulators/devices using the <code>devices</code> command: </p>

	<pre>adb devices</pre>

<p>In response, adb prints this status information for each instance:</p>

<ul>
	<li>Serial number &mdash; A string created by adb to uniquely identify an emulator/device instance by its 
        console port number. The format of the serial number is <code>&lt;type&gt;-&lt;consolePort&gt;</code>. 
        Here's an example serial number: <code>emulator-5554</code></li>
	<li>State &mdash; The connection state of the instance may be one of the following:
		<ul>
		<li><code>offline</code> &mdash; the instance is not connected to adb or is not responding.</li>
		<li><code>device</code> &mdash; the instance is now connected to the adb server. Note that this state does not 
                    imply that the Android system is fully booted and operational, since the instance connects to adb 
                    while the system is still booting. However, after boot-up, this is the normal operational state of 
                    an emulator/device instance.</li>
                <li><code>no device</code> &mdash; there is no emulator/device connected.
		</ul>
	</li>
</ul>

<p>The output for each instance is formatted like this: </p>

	<pre>[serialNumber] [state]</pre>

<p>Here's an example showing the <code>devices</code> command and its output:</p>

	<pre>$ adb devices
List of devices attached 
emulator-5554&nbsp;&nbsp;device
emulator-5556&nbsp;&nbsp;device
emulator-5558&nbsp;&nbsp;device</pre>



<a name="directingcommands"></a>

<h2>Directing Commands to a Specific Emulator/Device Instance</h2>

<p>If multiple emulator/device instances are running, you need to specify a target instance when issuing adb commands. To so so, use the <code>-s</code> option in the commands. The usage for the <code>-s</code> option is:</p>

    <pre>adb -s &lt;serialNumber&gt; &lt;command&gt; </pre>
	
<p>As shown, you specify the target instance for a command using its adb-assigned serial number. You can use the <code>devices</code> command to obtain the serial numbers of running emulator/device instances. </p>

<p>Here is an example: </p>

	<pre>adb -s emulator-5556 install helloWorld.apk</pre>

<p>Note that, if you issue a command without specifying a target emulator/device instance using <code>-s</code>, adb generates an error. 

<a name="move"></a>

<h2>Installing an Application</h2>
<p>You can use adb to copy an application from your development computer and install it on an emulator/device instance. To do so, use the <code>install</code> command. With the command, you must specify the path to the .apk file that you want to install:</p>

<pre>adb install &lt;path_to_apk&gt;</pre>

<p>For more information about how to create an .apk file that you can install on an emulator/device
instance, see <a href="{@docRoot}tools/building/index.html">Building and Running</a></p>

<p>Note that, if you are using the Eclipse IDE and have the ADT plugin installed, you do not need to use adb (or aapt) directly to install your application on the emulator/device. Instead, the ADT plugin handles the packaging and installation of the application for you. </p>


<a name="forwardports"></a>

<h2>Forwarding Ports</h2>

    <p>You can use the <code>forward</code> command to set up arbitrary port forwarding &mdash; forwarding of requests on a specific host port to a different port on an emulator/device instance. Here's how you would set up forwarding of host port 6100 to emulator/device port 7100:</p>
<pre>adb forward tcp:6100 tcp:7100</pre>
    <p>You can also use adb to set up forwarding to named abstract UNIX domain sockets, as illustrated here:</p>
<pre>adb forward tcp:6100 local:logd </pre>

<a name="copyfiles"></a>

<h2>Copying Files to or from an Emulator/Device Instance</h2>

<p>You can use the adb commands <code>pull</code> and <code>push</code> to copy files to and from an emulator/device instance's data file. Unlike the <code>install</code> command, which only copies an .apk file to a specific location, the <code>pull</code> and <code>push</code> commands let you copy arbitrary directories and files to any location in an emulator/device instance. </p>

<p>To copy a file or directory (recursively) <em>from</em> the emulator or device, use</p>
<pre>adb pull &lt;remote&gt; &lt;local&gt;</pre> 

<p>To copy a file or directory (recursively) <em>to</em> the emulator or device, use</p>
    <pre>adb push &lt;local&gt; &lt;remote&gt;</pre> 

<p>In the commands, <code>&lt;local&gt;</code> and <code>&lt;remote&gt;</code> refer to the paths to the target files/directory on your development machine (local) and on the emulator/device instance (remote).</p>

<p>Here's an example: </p>
<pre>adb push foo.txt /sdcard/foo.txt</pre>

<a name="commandsummary"></a>

<h2>Listing of adb Commands</h2>

<p>The table below lists all of the supported adb commands and explains their meaning and usage. </p>


<table>
<tr>
	<th>Category</th>
	<th>Command</th>
	<th>Description</th>
	<th>Comments</th>
</tr>

<tr>
<td rowspan="3">Options</td>
<td><code>-d</code></td>
<td>Direct an adb command to the only attached USB device.</td>
<td>Returns an error if more than one USB device is attached.</td>
</tr>

<tr>
<td><code>-e</code></td>
<td>Direct an adb command to the only running emulator instance.</td>
<td>Returns an error if more than one emulator instance is running. </td>
</tr>

<tr>
<td><code>-s&nbsp;&lt;serialNumber&gt;</code></td>
<td>Direct an adb command a specific emulator/device instance, referred to by its adb-assigned serial number (such as "emulator-5556").</td>
<td>If not specified, adb generates an error.</td>
</tr>

<tr>
<td rowspan="3">General</td>
<td><code>devices</code></td>
<td>Prints a list of all attached emulator/device instances.</td>
<td>See <a href="#devicestatus">Querying for Emulator/Device Instances</a> for more information.</td>
</tr>

<tr>
<td><code>help</code></td>
<td>Prints a list of supported adb commands.</td>
<td>&nbsp;</td>
</tr>

<tr>
<td><code>version</code></td>
<td>Prints the adb version number. </td>
<td>&nbsp;</td>
</tr>

<tr>
<td rowspan="3">Debug</td>
<td ><code>logcat&nbsp;[&lt;option&gt;] [&lt;filter-specs&gt;]</code></td>
<td>Prints log data to the screen. </td>
<td>&nbsp;</td>
</tr>

<tr>
<td><code>bugreport</code></td>
<td>Prints <code>dumpsys</code>, <code>dumpstate</code>, and <code>logcat</code> data to the screen, for the purposes of bug reporting. </td>
<td>&nbsp;</td>
</tr>

<tr>
<td><code>jdwp</code></td>
<td>Prints a list of available JDWP processes on a given device. </td>
<td>You can use the <code>forward jdwp:&lt;pid&gt;</code> port-forwarding specification to connect to a specific JDWP process. For example: <br>
    <code>adb forward tcp:8000 jdwp:472</code><br>
    <code>jdb -attach localhost:8000</code></p>
 </td>
</tr>

<tr>
<td rowspan=3">Data</td>
<td><code>install&nbsp;&lt;path-to-apk&gt;</code></td>
<td>Pushes an Android application (specified as a full path to an .apk file) to the data file of an emulator/device. </td>
<td>&nbsp;</td>
</tr>

<tr>
<td><code>pull&nbsp;&lt;remote&gt;&nbsp;&lt;local&gt;</code></td>
<td>Copies a specified file from an emulator/device instance to your development computer. </td>
<td>&nbsp;</td>
</tr>

<tr>
<td><code>push&nbsp;&lt;local&gt;&nbsp;&lt;remote&gt;</code></td>
<td>Copies a specified file from your development computer to an emulator/device instance. </td>
<td>&nbsp;</td>
</tr>

<tr>
<td rowspan="2">Ports and Networking</td>
<td><code>forward&nbsp;&lt;local&gt;&nbsp;&lt;remote&gt;</code></td>
<td>Forwards socket connections from a specified local port to a specified remote port on the emulator/device instance. </td>
<td>Port specifications can use these schemes: 
<ul><li><code>tcp:&lt;portnum&gt;</code></li>
<li><code>local:&lt;UNIX domain socket name&gt;</code></li>
<li><code>dev:&lt;character device name&gt;</code></li>
<li><code>jdwp:&lt;pid&gt;</code></li></ul>
</td>
</tr>

<tr>
<td><code>ppp&nbsp;&lt;tty&gt;&nbsp;[parm]...</code></td>
<td>Run PPP over USB.
<ul>
<li><code>&lt;tty&gt;</code> &mdash; the tty for PPP stream. For example <code>dev:/dev/omap_csmi_ttyl</code>. </li>
<li><code>[parm]... </code> &mdash; zero or more PPP/PPPD options, such as <code>defaultroute</code>, <code>local</code>, <code>notty</code>, etc.</li></ul>

<p>Note that you should not automatically start a PPP connection. </p></td>
<td></td>
</tr>

<tr>
<td rowspan="3">Scripting</td>
<td><code>get-serialno</code></td>
<td>Prints the adb instance serial number string.</td>
<td rowspan="2">See <a href="#devicestatus">Querying for Emulator/Device Instances</a> for more information. </td>
</tr>

<tr>
<td><code>get-state</code></td>
<td>Prints the adb state of an emulator/device instance.</td>
</td>
</tr>

<tr>
<td><code>wait-for-device</code></td>
<td>Blocks execution until the device is online &mdash; that is, until the instance state is <code>device</code>.</td>
<td>You can prepend this command to other adb commands, in which case adb will wait until the emulator/device instance is connected before issuing the other commands. Here's an example: 
<pre>adb wait-for-device shell getprop</pre>

Note that this command does <em>not</em> cause adb to wait until the entire system is fully booted. For that reason, you should not prepend it to other commands that require a fully booted system. As an example, the <code>install</code> requires the Android package manager, which is available only after the system is fully booted. A command such as 

<pre>adb wait-for-device install &lt;app&gt;.apk</pre>

would issue the <code>install</code> command as soon as the emulator or device instance connected to the adb server, but before the Android system was fully booted, so it would result in an error. </td>
</tr>



<tr>
<td rowspan="2">Server</td>
<td><code>start-server</code></td>
<td>Checks whether the adb server process is running and starts it, if not.</td>
<td>&nbsp;</td>
</tr>

<tr>
<td><code>kill-server</code></td>
<td>Terminates the adb server process.</td>
<td>&nbsp;</td>
</tr>



<tr>
<td rowspan="2">Shell</td>
<td><code>shell</code></td>
<td>Starts a remote shell in the target emulator/device instance.</td>
<td rowspan="2">See <a href="#shellcommands">Issuing Shell Commands</a> for more information. </td>
</tr>

<tr>
<td><code>shell&nbsp;[&lt;shellCommand&gt;]</code></td>
<td>Issues a shell command in the target emulator/device instance and then exits the remote shell.</td>
</tr>

</table>


<a name="shellcommands"></a>

<h2>Issuing Shell Commands</h2>

<p>Adb provides an ash shell that you can use to run a variety of commands on an emulator 
or device. The command binaries are stored in the file system of the emulator or device, 
in this location: </p>

<pre>/system/bin/...</pre>

<p>You can use the <code>shell</code> command to issue commands, with or without entering the adb remote shell on the emulator/device. </p>

<p>To issue a single command without entering a remote shell, use the <code>shell</code> command like this: </p>

	<pre>adb [-d|-e|-s {&lt;serialNumber&gt;}] shell &lt;shellCommand&gt;</pre>
	
<p>To drop into a remote shell on a emulator/device instance, use the <code>shell</code> command like this:</p>

	<pre>adb [-d|-e|-s {&lt;serialNumber&gt;}] shell</pre>

<p>When you are ready to exit the remote shell, use <code>CTRL+D</code> or <code>exit</code> to end the shell session. </p>

<p>The sections below provide more information about shell commands that you can use.</p>
      
<a name="sqlite" id="sqlite"></a>

<h3>Examining sqlite3 Databases from a Remote Shell</h3>

<p>From an adb remote shell, you can use the 
<a href="http://www.sqlite.org/sqlite.html">sqlite3</a> command-line program to 
manage SQLite databases created by Android applications. The 
<code>sqlite3</code> tool includes many useful commands, such as 
<code>.dump</code> to print out the contents of a table and 
<code>.schema</code> to print the SQL CREATE statement for an existing table. 
The tool also gives you the ability to execute SQLite commands on the fly.</p>

<p>To use <code>sqlite3</code>, enter a remote shell on the emulator instance, as described above, then invoke the tool using the <code>sqlite3</code> command. Optionally, when invoking <code>sqlite3</code> you can specify the full path to the database you want to explore. Emulator/device instances store SQLite3 databases in the folder <code><span chatdir="1"><span chatindex="259474B4B070F261">/data/data/<em>&lt;package_name&gt;</em>/databases</span></span>/</code>. </p>

<p>Here's an example: </p>

<pre>$ adb -s emulator-5554 shell
# sqlite3 /data/data/com.example.google.rss.rssexample/databases/rssitems.db
SQLite version 3.3.12
Enter &quot;.help&quot; for instructions
<em>.... enter commands, then quit...</em>
sqlite&gt; .exit </pre>

<p>Once you've invoked <code>sqlite3</code>, you can issue <code>sqlite3</code> commands in the shell. To exit and return to the adb remote shell, use <code>exit</code> or <code>CTRL+D</code>.


<a name="monkey"></a>

<h3>UI/Application Exerciser Monkey</h3>

<p>The Monkey is a program that runs on your emulator or device and generates pseudo-random
streams of user events such as clicks, touches, or gestures, as well as a number of system-level 
events.  You can use the Monkey to stress-test applications that you are developing,
in a random yet repeatable manner.</p>

<p>The simplest way to use the monkey is with the following command, which will launch your
application and send 500 pseudo-random events to it.</p>

<pre>$ adb shell monkey -v -p your.package.name 500</pre>

<p>For more information about command options for Monkey, see the complete 
<a href="{@docRoot}tools/help/monkey.html" title="monkey">UI/Application Exerciser Monkey</a> documentation page.</p>


<a name="othershellcommands"></a>

<h3>Other Shell Commands</h3>

<p>The table below lists several of the adb shell commands available. For a complete list of commands and programs, start an emulator instance and use the <code>adb -help</code> command. </p>

<pre>adb shell ls /system/bin</pre>

<p>Help is available for most of the commands. </p>

<table>
<tr>
	<th>Shell Command</th>
	<th>Description</th>
	<th>Comments</th>
</tr>

<tr>
<td><code>dumpsys</code></td>
<td>Dumps system data to the screen.</td>
<td rowspan=4">The <a href="{@docRoot}tools/debugging/ddms.html">Dalvik Debug Monitor Server</a> 
(DDMS) tool offers integrated debug environment that you may find easier to use.</td>
</tr>

<tr>
<td><code>dumpstate</code></td>
<td>Dumps state to a file.</td>
</tr>

<tr>
<td><code>logcat&nbsp;[&lt;option&gt;]...&nbsp;[&lt;filter-spec&gt;]...</code></td>
<td>Enables radio logging and prints output to the screen. </td>
</tr>

<tr>
<td><code>dmesg</code></td>
<td>Prints kernel debugging messages to the screen. </td>
</tr>

<tr>
<td><code>start</code></td>
<td>Starts (restarts) an emulator/device instance.</td>
<td>&nbsp;</td>
</tr>

<tr>
<td><code>stop</code></td>
<td>Stops execution of an emulator/device instance.</td>
<td>&nbsp;</td>
</tr>

</table>
<a name="stdout"></a>
<a name="usinglogcat"></a>
<a name="outputformat"></a>
<a name="filteringoutput"></a>
<a name="stdout"></a>
<a name="logcatoptions"></a>
<a name="logcat"></a>

<h2>Enabling logcat Logging</h2>

<p>The Android logging system provides a mechanism for collecting and viewing system debug output. Logs from various applications and portions of the system are collected in a series of circular buffers, which then can be viewed and filtered by the <code>logcat</code> command.</p>

<p>You can use the <code>logcat</code> command to view and follow the contents of the system's log buffers. The general usage is:</p>

<pre>[adb] logcat [&lt;option&gt;] ... [&lt;filter-spec&gt;] ...</pre>

<p>You can use the <code>logcat</code> command from your development computer  or from a remote adb shell in an emulator/device instance. To view log output in your development computer, you use</p>

<pre>$ adb logcat</pre>

<p>and from a remote adb shell you use</p>

<pre># logcat</pre>

<p>See <a href="{@docRoot}tools/debugging/debugging-log.html">Reading and Writing Logs</a> for complete information about logcat commend options and filter specifications.</p>


<a name="stopping"></a>

<h2>Stopping the adb Server</h2>

<p>In some cases, you might need to terminate the adb server process and then restart it. For example, if adb does not respond to a command, you can terminate the server and restart it and that may resolve the problem. </p>

<p>To stop the adb server, use the <code>kill-server</code>. You can then restart the server by issuing any adb command. </p>