1 page.title=Android Debug Bridge 2 parent.title=Tools 3 parent.link=index.html 4 @jd:body 5 6 <div id="qv-wrapper"> 7 <div id="qv"> 8 <h2>ADB quickview</h2> 9 <ul> 10 <li>Manage the state of an emulator or device</li> 11 <li>Run shell commands on a device</li> 12 <li>Manage port forwarding on an emulator or device</li> 13 <li>Copy files to/from an emulator or device</li> 14 </ul> 15 16 <h2>In this document</h2> 17 <ol> 18 <li><a href="#issuingcommands">Issuing ADB Commands</a></li> 19 <li><a href="#devicestatus">Querying for Emulator/Device Instances</a></li> 20 <li><a href="#directingcommands">Directing Commands to a Specific Emulator/Device Instance</a></li> 21 <li><a href="#move">Installing an Application</a></li> 22 <li><a href="#forwardports">Forwarding Ports</a></li> 23 <li><a href="#copyfiles">Copying Files to or from an Emulator/Device Instance</a></li> 24 <li><a href="#commandsummary">Listing of adb Commands </a></li> 25 <li><a href="#shellcommands">Issuing Shell Commands</a></li> 26 <li><a href="#logcat">Enabling Logcat Logging</a></li> 27 <li><a href="#stopping">Stopping the adb Server</a></li> 28 </ol> 29 30 <h2>See also</h2> 31 <ol> 32 <li><a href="emulator.html">Emulator</a></li> 33 </ol> 34 35 </div> 36 </div> 37 38 <p>Android Debug Bridge (adb) is a versatile command line tool that lets you communicate with an 39 emulator instance or connected Android-powered device. It is a client-server program that includes 40 three components: </p> 41 42 <ul> 43 <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> 44 <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> 45 <li>A daemon, which runs as a background process on each emulator or device instance. </li> 46 </ul> 47 48 <p>You can find the {@code adb} tool in {@code <sdk>/platform-tools/}.</p> 49 50 <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—all adb clients use port 5037 to communicate with the adb server. </p> 51 52 <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 — an even-numbered port for console connections and an odd-numbered port for adb connections. For example: </p> 53 54 <p style="margin-left:2em"> 55 Emulator 1, console: 5554<br/> 56 Emulator 1, adb: 5555<br> 57 Emulator 2, console: 5556<br> 58 Emulator 2, adb: 5557 ... 59 </p> 60 61 <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> 62 63 <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> 64 65 <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> 66 67 <a name="issuingcommands"></a> 68 69 <h2>Issuing adb Commands</h2> 70 71 <p>You can issue adb commands from a command line on your development machine or from a script. The usage is: </p> 72 73 <pre>adb [-d|-e|-s <serialNumber>] <command> </pre> 74 75 <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> 76 77 <a name="devicestatus"></a> 78 79 <h2>Querying for Emulator/Device Instances</h2> 80 81 <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> 82 83 <pre>adb devices</pre> 84 85 <p>In response, adb prints this status information for each instance:</p> 86 87 <ul> 88 <li>Serial number — A string created by adb to uniquely identify an emulator/device instance by its 89 console port number. The format of the serial number is <code><type>-<consolePort></code>. 90 Here's an example serial number: <code>emulator-5554</code></li> 91 <li>State — The connection state of the instance. Three states are supported: 92 <ul> 93 <li><code>offline</code> — the instance is not connected to adb or is not responding.</li> 94 <li><code>device</code> — the instance is now connected to the adb server. Note that this state does not 95 imply that the Android system is fully booted and operational, since the instance connects to adb 96 while the system is still booting. However, after boot-up, this is the normal operational state of 97 an emulator/device instance.</li> 98 </ul> 99 </li> 100 </ul> 101 102 <p>The output for each instance is formatted like this: </p> 103 104 <pre>[serialNumber] [state]</pre> 105 106 <p>Here's an example showing the <code>devices</code> command and its output:</p> 107 108 <pre>$ adb devices 109 List of devices attached 110 emulator-5554 device 111 emulator-5556 device 112 emulator-5558 device</pre> 113 114 <p>If there is no emulator/device running, adb returns <code>no device</code>.</p> 115 116 117 <a name="directingcommands"></a> 118 119 <h2>Directing Commands to a Specific Emulator/Device Instance</h2> 120 121 <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> 122 123 <pre>adb -s <serialNumber> <command> </pre> 124 125 <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> 126 127 <p>Here is an example: </p> 128 129 <pre>adb -s emulator-5556 install helloWorld.apk</pre> 130 131 <p>Note that, if you issue a command without specifying a target emulator/device instance using <code>-s</code>, adb generates an error. 132 133 <a name="move"></a> 134 135 <h2>Installing an Application</h2> 136 <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> 137 138 <pre>adb install <path_to_apk></pre> 139 140 <p>For more information about how to create an .apk file that you can install on an emulator/device 141 instance, see <a href="{@docRoot}tools/building/index.html">Building and Running</a></p> 142 143 <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> 144 145 146 <a name="forwardports"></a> 147 148 <h2>Forwarding Ports</h2> 149 150 <p>You can use the <code>forward</code> command to set up arbitrary port forwarding — 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> 151 <pre>adb forward tcp:6100 tcp:7100</pre> 152 <p>You can also use adb to set up forwarding to named abstract UNIX domain sockets, as illustrated here:</p> 153 <pre>adb forward tcp:6100 local:logd </pre> 154 155 <a name="copyfiles"></a> 156 157 <h2>Copying Files to or from an Emulator/Device Instance</h2> 158 159 <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> 160 161 <p>To copy a file or directory (recursively) <em>from</em> the emulator or device, use</p> 162 <pre>adb pull <remote> <local></pre> 163 164 <p>To copy a file or directory (recursively) <em>to</em> the emulator or device, use</p> 165 <pre>adb push <local> <remote></pre> 166 167 <p>In the commands, <code><local></code> and <code><remote></code> refer to the paths to the target files/directory on your development machine (local) and on the emulator/device instance (remote).</p> 168 169 <p>Here's an example: </p> 170 <pre>adb push foo.txt /sdcard/foo.txt</pre> 171 172 <a name="commandsummary"></a> 173 174 <h2>Listing of adb Commands</h2> 175 176 <p>The table below lists all of the supported adb commands and explains their meaning and usage. </p> 177 178 179 <table> 180 <tr> 181 <th>Category</th> 182 <th>Command</th> 183 <th>Description</th> 184 <th>Comments</th> 185 </tr> 186 187 <tr> 188 <td rowspan="3">Options</td> 189 <td><code>-d</code></td> 190 <td>Direct an adb command to the only attached USB device.</td> 191 <td>Returns an error if more than one USB device is attached.</td> 192 </tr> 193 194 <tr> 195 <td><code>-e</code></td> 196 <td>Direct an adb command to the only running emulator instance.</td> 197 <td>Returns an error if more than one emulator instance is running. </td> 198 </tr> 199 200 <tr> 201 <td><code>-s <serialNumber></code></td> 202 <td>Direct an adb command a specific emulator/device instance, referred to by its adb-assigned serial number (such as "emulator-5556").</td> 203 <td>If not specified, adb generates an error.</td> 204 </tr> 205 206 <tr> 207 <td rowspan="3">General</td> 208 <td><code>devices</code></td> 209 <td>Prints a list of all attached emulator/device instances.</td> 210 <td>See <a href="#devicestatus">Querying for Emulator/Device Instances</a> for more information.</td> 211 </tr> 212 213 <tr> 214 <td><code>help</code></td> 215 <td>Prints a list of supported adb commands.</td> 216 <td> </td> 217 </tr> 218 219 <tr> 220 <td><code>version</code></td> 221 <td>Prints the adb version number. </td> 222 <td> </td> 223 </tr> 224 225 <tr> 226 <td rowspan="3">Debug</td> 227 <td ><code>logcat [<option>] [<filter-specs>]</code></td> 228 <td>Prints log data to the screen. </td> 229 <td> </td> 230 </tr> 231 232 <tr> 233 <td><code>bugreport</code></td> 234 <td>Prints <code>dumpsys</code>, <code>dumpstate</code>, and <code>logcat</code> data to the screen, for the purposes of bug reporting. </td> 235 <td> </td> 236 </tr> 237 238 <tr> 239 <td><code>jdwp</code></td> 240 <td>Prints a list of available JDWP processes on a given device. </td> 241 <td>You can use the <code>forward jdwp:<pid></code> port-forwarding specification to connect to a specific JDWP process. For example: <br> 242 <code>adb forward tcp:8000 jdwp:472</code><br> 243 <code>jdb -attach localhost:8000</code></p> 244 </td> 245 </tr> 246 247 <tr> 248 <td rowspan=3">Data</td> 249 <td><code>install <path-to-apk></code></td> 250 <td>Pushes an Android application (specified as a full path to an .apk file) to the data file of an emulator/device. </td> 251 <td> </td> 252 </tr> 253 254 <tr> 255 <td><code>pull <remote> <local></code></td> 256 <td>Copies a specified file from an emulator/device instance to your development computer. </td> 257 <td> </td> 258 </tr> 259 260 <tr> 261 <td><code>push <local> <remote></code></td> 262 <td>Copies a specified file from your development computer to an emulator/device instance. </td> 263 <td> </td> 264 </tr> 265 266 <tr> 267 <td rowspan="2">Ports and Networking</td> 268 <td><code>forward <local> <remote></code></td> 269 <td>Forwards socket connections from a specified local port to a specified remote port on the emulator/device instance. </td> 270 <td>Port specifications can use these schemes: 271 <ul><li><code>tcp:<portnum></code></li> 272 <li><code>local:<UNIX domain socket name></code></li> 273 <li><code>dev:<character device name></code></li> 274 <li><code>jdwp:<pid></code></li></ul> 275 </td> 276 </tr> 277 278 <tr> 279 <td><code>ppp <tty> [parm]...</code></td> 280 <td>Run PPP over USB. 281 <ul> 282 <li><code><tty></code> — the tty for PPP stream. For example <code>dev:/dev/omap_csmi_ttyl</code>. </li> 283 <li><code>[parm]... </code> — zero or more PPP/PPPD options, such as <code>defaultroute</code>, <code>local</code>, <code>notty</code>, etc.</li></ul> 284 285 <p>Note that you should not automatically start a PPP connection. </p></td> 286 <td></td> 287 </tr> 288 289 <tr> 290 <td rowspan="3">Scripting</td> 291 <td><code>get-serialno</code></td> 292 <td>Prints the adb instance serial number string.</td> 293 <td rowspan="2">See <a href="#devicestatus">Querying for Emulator/Device Instances</a> for more information. </td> 294 </tr> 295 296 <tr> 297 <td><code>get-state</code></td> 298 <td>Prints the adb state of an emulator/device instance.</td> 299 </td> 300 </tr> 301 302 <tr> 303 <td><code>wait-for-device</code></td> 304 <td>Blocks execution until the device is online — that is, until the instance state is <code>device</code>.</td> 305 <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: 306 <pre>adb wait-for-device shell getprop</pre> 307 308 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 309 310 <pre>adb wait-for-device install <app>.apk</pre> 311 312 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> 313 </tr> 314 315 316 317 <tr> 318 <td rowspan="2">Server</td> 319 <td><code>start-server</code></td> 320 <td>Checks whether the adb server process is running and starts it, if not.</td> 321 <td> </td> 322 </tr> 323 324 <tr> 325 <td><code>kill-server</code></td> 326 <td>Terminates the adb server process.</td> 327 <td> </td> 328 </tr> 329 330 331 332 <tr> 333 <td rowspan="2">Shell</td> 334 <td><code>shell</code></td> 335 <td>Starts a remote shell in the target emulator/device instance.</td> 336 <td rowspan="2">See <a href="#shellcommands">Issuing Shell Commands</a> for more information. </td> 337 </tr> 338 339 <tr> 340 <td><code>shell [<shellCommand>]</code></td> 341 <td>Issues a shell command in the target emulator/device instance and then exits the remote shell.</td> 342 </tr> 343 344 </table> 345 346 347 <a name="shellcommands"></a> 348 349 <h2>Issuing Shell Commands</h2> 350 351 <p>Adb provides an ash shell that you can use to run a variety of commands on an emulator 352 or device. The command binaries are stored in the file system of the emulator or device, 353 in this location: </p> 354 355 <pre>/system/bin/...</pre> 356 357 <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> 358 359 <p>To issue a single command without entering a remote shell, use the <code>shell</code> command like this: </p> 360 361 <pre>adb [-d|-e|-s {<serialNumber>}] shell <shellCommand></pre> 362 363 <p>To drop into a remote shell on a emulator/device instance, use the <code>shell</code> command like this:</p> 364 365 <pre>adb [-d|-e|-s {<serialNumber>}] shell</pre> 366 367 <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> 368 369 <p>The sections below provide more information about shell commands that you can use.</p> 370 371 <a name="sqlite" id="sqlite"></a> 372 373 <h3>Examining sqlite3 Databases from a Remote Shell</h3> 374 375 <p>From an adb remote shell, you can use the 376 <a href="http://www.sqlite.org/sqlite.html">sqlite3</a> command-line program to 377 manage SQLite databases created by Android applications. The 378 <code>sqlite3</code> tool includes many useful commands, such as 379 <code>.dump</code> to print out the contents of a table and 380 <code>.schema</code> to print the SQL CREATE statement for an existing table. 381 The tool also gives you the ability to execute SQLite commands on the fly.</p> 382 383 <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><package_name></em>/databases</span></span>/</code>. </p> 384 385 <p>Here's an example: </p> 386 387 <pre>$ adb -s emulator-5554 shell 388 # sqlite3 /data/data/com.example.google.rss.rssexample/databases/rssitems.db 389 SQLite version 3.3.12 390 Enter ".help" for instructions 391 <em>.... enter commands, then quit...</em> 392 sqlite> .exit </pre> 393 394 <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>. 395 396 397 <a name="monkey"></a> 398 399 <h3>UI/Application Exerciser Monkey</h3> 400 401 <p>The Monkey is a program that runs on your emulator or device and generates pseudo-random 402 streams of user events such as clicks, touches, or gestures, as well as a number of system-level 403 events. You can use the Monkey to stress-test applications that you are developing, 404 in a random yet repeatable manner.</p> 405 406 <p>The simplest way to use the monkey is with the following command, which will launch your 407 application and send 500 pseudo-random events to it.</p> 408 409 <pre>$ adb shell monkey -v -p your.package.name 500</pre> 410 411 <p>For more information about command options for Monkey, see the complete 412 <a href="{@docRoot}tools/help/monkey.html" title="monkey">UI/Application Exerciser Monkey</a> documentation page.</p> 413 414 415 <a name="othershellcommands"></a> 416 417 <h3>Other Shell Commands</h3> 418 419 <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> 420 421 <pre>adb shell ls /system/bin</pre> 422 423 <p>Help is available for most of the commands. </p> 424 425 <table> 426 <tr> 427 <th>Shell Command</th> 428 <th>Description</th> 429 <th>Comments</th> 430 </tr> 431 432 <tr> 433 <td><code>dumpsys</code></td> 434 <td>Dumps system data to the screen.</td> 435 <td rowspan=4">The <a href="{@docRoot}tools/debugging/ddms.html">Dalvik Debug Monitor Server</a> 436 (DDMS) tool offers integrated debug environment that you may find easier to use.</td> 437 </tr> 438 439 <tr> 440 <td><code>dumpstate</code></td> 441 <td>Dumps state to a file.</td> 442 </tr> 443 444 <tr> 445 <td><code>logcat [<option>]... [<filter-spec>]...</code></td> 446 <td>Enables radio logging and prints output to the screen. </td> 447 </tr> 448 449 <tr> 450 <td><code>dmesg</code></td> 451 <td>Prints kernel debugging messages to the screen. </td> 452 </tr> 453 454 <tr> 455 <td><code>start</code></td> 456 <td>Starts (restarts) an emulator/device instance.</td> 457 <td> </td> 458 </tr> 459 460 <tr> 461 <td><code>stop</code></td> 462 <td>Stops execution of an emulator/device instance.</td> 463 <td> </td> 464 </tr> 465 466 </table> 467 <a name="stdout"></a> 468 <a name="usinglogcat"></a> 469 <a name="outputformat"></a> 470 <a name="filteringoutput"></a> 471 <a name="stdout"></a> 472 <a name="logcatoptions"></a> 473 <a name="logcat"></a> 474 475 <h2>Enabling logcat Logging</h2> 476 477 <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> 478 479 <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> 480 481 <pre>[adb] logcat [<option>] ... [<filter-spec>] ...</pre> 482 483 <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> 484 485 <pre>$ adb logcat</pre> 486 487 <p>and from a remote adb shell you use</p> 488 489 <pre># logcat</pre> 490 491 <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> 492 493 494 <a name="stopping"></a> 495 496 <h2>Stopping the adb Server</h2> 497 498 <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> 499 500 <p>To stop the adb server, use the <code>kill-server</code>. You can then restart the server by issuing any adb command. </p> 501 502 503