Android Debug Bridge - Android Developers
Android Debug Bridge - Android Developers
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.
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:
As shown, the emulator instance connected to adb on port 5555 is the same as the instance whose console listens on
port 5554.
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).
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.
You can issue adb commands from a command line on your development machine or from a script. The usage is:
adb [-d|-e|-s <serialNumber>] <command>
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 -d option to specify the target
instance to which the command should be directed. For more information about using this option, see Directing Commands
to a Specific Emulator/Device Instance.
adb devices
Serial number — A string created by adb to uniquely identify an emulator/device instance by its console port number.
The format of the serial number is <type>-<consolePort>. Here's an example serial number: emulator-5554
State — The connection state of the instance. Three states are supported:
offline — the instance is not connected to adb or is not responding.
device — 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.
[serialNumber] [state]
$ adb devices
List of devices attached
emulator-5554 device
emulator-5556 device
emulator-5558 device
If multiple emulator/device instances are running, you need to specify a target instance when issuing adb commands. To
so so, use the -s option in the commands. The usage for the -s option is:
As shown, you specify the target instance for a command using its adb-assigned serial number. You can use the
devices command to obtain the serial numbers of running emulator/device instances.
Here is an example:
adb -s emulator-5556 install helloWorld.apk
Note that, if you issue a command without specifying a target emulator/device instance using -s, adb generates an error.
Installing an Application
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 install command. With the command, you must specify the path to the .apk file that you want to install:
For more information about how to create an .apk file that you can install on an emulator/device instance, see Building
and Running
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.
Forwarding Ports
You can use the forward 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:
You can also use adb to set up forwarding to named abstract UNIX domain sockets, as illustrated here:
In the commands, <local> and <remote> refer to the paths to the target files/directory on your development machine
(local) and on the emulator/device instance (remote).
Here's an example:
adb push foo.txt /sdcard/foo.txt
The table below lists all of the supported adb commands and explains their meaning and usage.
jdwp Prints a list of available JDWP You can use the forward
processes on a given device. jdwp:<pid> port-forwarding
specification to connect to
a specific JDWP process.
For example:
adb forward tcp:8000
jdwp:472
jdb -attach
localhost:8000
Ports and forward <local> <remote> Forwards socket connections Port specifications can us
Networking from a specified local port to a these schemes:
specified remote port on the tcp:<portnum>
emulator/device instance.
local:<UNIX domain
socket name>
dev:<character
device name>
jdwp:<pid>
Scripting get-serialno Prints the adb instance serial See Querying for
number string. Emulator/Device Instances
for more information.
get-state Prints the adb state of an
emulator/device instance.
wait-for-device Blocks execution until the device You can prepend this
is online — that is, until the command to other adb
instance state is device. commands, in which case
adb will wait until the
emulator/device instance is
connected before issuing
the other commands.
Here's an example:
adb
wait-for-device
shell getprop
adb
wait-for-device
install <app>.apk
Shell shell Starts a remote shell in the target See Issuing Shell
emulator/device instance. Commands for more
information.
shell [<shellCommand>] Issues a shell command in the
target emulator/device instance
and then exits the remote shell.
/system/bin/...
You can use the shell command to issue commands, with or without entering the adb remote shell on the
emulator/device.
To issue a single command without entering a remote shell, use the shell command like this:
To drop into a remote shell on a emulator/device instance, use the shell command like this:
When you are ready to exit the remote shell, use CTRL+D or exit to end the shell session.
The sections below provide more information about shell commands that you can use.
To use sqlite3, enter a remote shell on the emulator instance, as described above, then invoke the tool using the
sqlite3 command. Optionally, when invoking sqlite3 you can specify the full path to the database you want to explore.
Emulator/device instances store SQLite3 databases in the folder /data/data/<package_name>/databases/.
Here's an example:
Once you've invoked sqlite3, you can issue sqlite3 commands in the shell. To exit and return to the adb remote shell,
use exit or CTRL+D.
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.
For more information about command options for Monkey, see the complete UI/Application Exerciser Monkey
documentation page.
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 logcat command.
Using logcat Commands
You can use the logcat command to view and follow the contents of the system's log buffers. The general usage is:
The sections below explain filter specifications and the command options. See Listing of logcat Command Options for a
summary of options.
You can use the logcat 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
$ adb logcat
# logcat
The tag of a log message is a short string indicating the system component from which the message originates (for
example, "View" for the view system).
The priority is one of the following character values, ordered from lowest to highest priority:
V — Verbose (lowest priority)
D — Debug
I — Info
W — Warning
E — Error
F — Fatal
S — Silent (highest priority, on which nothing is ever printed)
You can obtain a list of tags used in the system, together with priorities, by running logcat and observing the first two
columns of each message, given as <priority>/<tag>.
Here's an example of logcat output that shows that the message relates to priority level "I" and tag "ActivityManager":
To reduce the log output to a manageable level, you can restrict log output using filter expressions. Filter expressions let
you indicate to the system the tags-priority combinations that you are interested in — the system suppresses other
messages for the specified tags.
A filter expression follows this format tag:priority ..., where tag indicates the tag of interest and priority indicates
the minimum level of priority to report for that tag. Messages for that tag at or above the specified priority are written to
the log. You can supply any number of tag:priority specifications in a single filter expression. The series of
specifications is whitespace-delimited.
Here's an example of a filter expression that suppresses all log messages except those with the tag "ActivityManager", at
priority "Info" or above, and all log messages with tag "MyApp", with priority "Debug" or above:
The following filter expression displays all log messages with priority level "warning" and higher, on all tags:
If you're running logcat from your development computer (versus running it on a remote adb shell), you can also set a
default filter expression by exporting a value for the environment variable ANDROID_LOG_TAGS:
Note that ANDROID_LOG_TAGS filter is not exported to the emulator/device instance, if you are running logcat from a
remote shell or using adb shell logcat.
brief — Display priority/tag and PID of originating process (the default format).
process — Display PID only.
tag — Display the priority/tag only.
thread — Display process:thread and priority/tag only.
raw — Display the raw log message, with no other metadata fields.
time — Display the date, invocation time, priority/tag, and PID of the originating process.
long — Display all metadata fields and separate messages with a blank lines.
When starting logcat, you can specify the output format you want by using the -v option:
Here's an example that shows how to generate messages in thread output format:
Note that you can only specify one output format with the -v option.
Here's an example of how to view a log buffer containing radio and telephony messages:
To route the output in this way, you stop a running emulator/device instance and then use the shell command setprop to
enable the redirection of output. Here's how you do it:
The system retains this setting until you terminate the emulator/device instance. To use the setting as a default on the
emulator/device instance, you can add an entry to /data/local.prop on the device.
-b <buffer> Loads an alternate log buffer for viewing, such as event or radio. The main buffer is used by
default. See Viewing Alternative Log Buffers.
-n <count> Sets the maximum number of rotated logs to <count>. The default value is 4. Requires the -r
option.
-r <kbytes> Rotates the log file every <kbytes> of output. The default value is 16. Requires the -f option.
-v <format> Sets the output format for log messages. The default is brief format. For a list of supported
formats, see Controlling Log Output Format.
To stop the adb server, use the kill-server. You can then restart the server by issuing any adb command.
Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.
Android 3.0 r1 - 24 Mar 2011 11:38
Site Terms of Service - Privacy Policy - Brand Guidelines