blob: 2faff4f3fe5c24c0e545a2d4d33b69806d41948a [file] [log] [blame]
page.title=Android Debug Bridge
parent.title=Tools
parent.link=index.html
page.tags=adb
@jd:body
<div id="qv-wrapper">
<div id="qv">
<h2>In this document</h2>
<ol>
<li><a href="#Enabling">Enabling adb Debugging</a></li>
<li><a href="#issuingcommands">Syntax</a></li>
<li><a href="#commandsummary">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="#stopping">Stopping the adb Server</a></li>
<li><a href="#wireless">Wireless usage</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<br>
and so on...
</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
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>
<h2 id="Enabling">Enabling adb Debugging</h2>
<p>In order to use adb with a device connected over USB, you must enable
<strong>USB debugging</strong> in the device system settings, under <strong>
Developer options</strong>.</p>
<p>On Android 4.2 and higher, the Developer options screen is
hidden by default. To make it visible, go to
<b>Settings &gt; About phone</b> and tap <b>Build number</b> seven times. Return to the previous
screen to find <strong>Developer options</strong> at the bottom.</p>
<p>On some devices, the Developer options screen may be located or named differently.</p>
<p class="note"><strong>Note:</strong> When you connect a device running Android 4.2.2 or higher
to your computer, the system shows a dialog asking whether to accept an RSA key that allows
debugging through this computer. This security mechanism protects user devices because it ensures
that USB debugging and other adb commands cannot be executed unless you're able to unlock the
device and acknowledge the dialog. This requires that you have adb version 1.0.31 (available with
SDK Platform-tools r16.0.1 and higher) in order to debug on a device running Android 4.2.2 or
higher.</p>
<p>For more information about connecting to a device over USB, read
<a href="{@docRoot}tools/device.html">Using Hardware Devices</a>.</p>
<h2 id="issuingcommands">Syntax</h2>
<p>You can issue adb commands from a command line on your development machine or from a script.
The usage is: </p>
<pre class="no-pretty-print">
adb [-d|-e|-s &lt;serialNumber&gt;] &lt;command&gt;
</pre>
<p>If there's only one emulator running or only one device connected, the adb command is
sent to that device by default. If multiple emulators are running and/or multiple devices are
attached, you need to use the <code>-d</code>, <code>-e</code>, or <code>-s</code>
option to specify the target device to which the command should be directed. </p>
<h2 id="commandsummary">Commands</h2>
<p>The table below lists all of the supported adb commands and explains their meaning and usage. </p>
<p class="table-caption"><strong>Table 1.</strong> Available adb commands</p>
<table>
<tr>
<th>Category</th>
<th>Command</th>
<th>Description</th>
<th>Comments</th>
</tr>
<tr>
<td rowspan="3">Target Device</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>See <a href="#directingcommands">Directing
Commands to a Specific Emulator/Device Instance</a>.</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;[option] [filter-specs]</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 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 class="no-pretty-print">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 class="no-pretty-print">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="{@docRoot}tools/help/shell.html#shellcommands">ADB Shell Commands</a> for more information. </td>
</tr>
<tr>
<td><code>shell&nbsp;[shellCommand]</code></td>
<td>Issues a shell command in the target emulator/device instance and then exits the remote shell.</td>
</tr>
</table>
<h2 id="devicestatus">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 class="no-pretty-print">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 class="no-pretty-print">[serialNumber] [state]</pre>
<p>Here's an example showing the <code>devices</code> command and its output:</p>
<pre class="no-pretty-print">adb devices
List of devices attached
emulator-5554&nbsp;&nbsp;device
emulator-5556&nbsp;&nbsp;device
emulator-5558&nbsp;&nbsp;device</pre>
<h2 id="directingcommands">Directing Commands to a Specific Emulator/Device Instance</h2>
<p>If multiple emulator/device instances are running, you must specify a target instance
when issuing adb commands. To do so, use the <code>-s</code> option in the commands. The usage
for the <code>-s</code> option is:</p>
<pre class="no-pretty-print">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. For example: </p>
<pre class="no-pretty-print">adb -s emulator-5556 install helloWorld.apk</pre>
<p>Note that, if you issue a command without specifying a target emulator/device instance
while multiple devices are available, adb generates an error.
<p>If you have multiple devices available (hardware or emulated), but only one is an emulator,
simply use the {@code -e} option to send commands to the emulator. Likewise if there's multiple
devices but only one hardware device attached, use the {@code -d} option to send commands to
the hardware device.
<h2 id="move">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 class="no-pretty-print">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>
<h2 id="forwardports">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 class="no-pretty-print">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 class="no-pretty-print">adb forward tcp:6100 local:logd </pre>
<h2 id="copyfiles">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. 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 (and its sub-directories) <em>from</em> the emulator or device, use</p>
<pre class="no-pretty-print">adb pull &lt;remote&gt; &lt;local&gt;</pre>
<p>To copy a file or directory (and its sub-directories) <em>to</em> the emulator or device, use</p>
<pre class="no-pretty-print">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). For example: </p>
<pre class="no-pretty-print">adb push foo.txt /sdcard/foo.txt</pre>
<h2 id="stopping">Stopping the adb Server</h2>
<p>In some cases, you might need to terminate the adb server process and then restart it
to resolve the problem (e.g., if adb does not respond to a command).</p>
<p>To stop the adb server, use the <code>kill-server</code> command.
You can then restart the server by issuing any other adb command. </p>
<h2 id="wireless">Wireless usage</h2>
<p>
adb is usually used over USB. However, it is also possible to use over
Wi-Fi, as described here.
</p>
<ol>
<li>
<p>Connect your Android device and adb host computer
to a common Wi-Fi network accessible to both.
We have found that not all access points
are suitable; you may need to use an access point
whose firewall is configured properly to support adb.</p>
<p class="note"><strong>Note: </strong>If you are attempting to connect
to a Wear device, force it to connect to Wi-Fi by shutting off Bluetooth
on the phone connected to it.</p>
</li>
<li>
Connect the device to the host computer with a USB cable.
</li>
<li>
Set the target device to listen for a TCP/IP connection on port 5555.
<pre class="no-pretty-print">
$ adb tcpip 5555
</pre>
</li>
<li>
Disconnect the USB cable from the target device.
</li>
<li>
Find the IP address of the Android device. For example, on a Nexus device, you can find
the IP address at <strong>Settings</strong> &gt; <strong>About tablet</strong>
(or <strong>About phone</strong>) &gt; <strong>Status</strong> &gt; <strong>IP address</strong>. Or,
on an Android Wear device, you can find the IP address at <strong>Settings</strong> &gt;
<strong>Wi-Fi Settings</strong> &gt; <strong>Advanced</strong> &gt; <strong>IP address</strong>.
</li>
<li>
Connect to the device, identifying it by IP address.
<pre class="no-pretty-print">
$ adb connect &lt;device-ip-address&gt;
</pre>
</li>
<li>
Confirm that your host computer is connected to the target device:
<pre class="no-pretty-print">
$ adb devices
List of devices attached
&lt;device-ip-address&gt;:5555 device
</pre>
</ol>
<p>
You're now good to go!
</p>
<p>
If the adb connection is ever lost:
</p>
<ol>
<li>
Make sure that your host is still connected to the same Wi-Fi network your Android device is.
</li>
<li>
Reconnect by executing the "adb connect" step again.
</li>
<li>
Or if that doesn't work, reset your adb host:
<pre class="no-pretty-print">
adb kill-server
</pre>
and then start over from the beginning.
</li>
</ol>