Android comes with a specialized command-line debug utility called adb. It lets you control a device or emulator from your host, offering the kind of remote terminal or remote shell service that embedded programmers have come to expect when working with their target systems. Invoke the adb client from a command prompt on the host (Start ^ Run ^ cmd.exe on Windows, or open a terminal window on Linux or OS X). The client talks to an adb server that runs in background on the host and processes requests. If the server isn't running when you start the client, it starts the server for you. The server in turn communicates with adb daemons that run on either a device or an emulator. All of this communication is through TCP/IP ports. A single client/server can deal with multiple devices and emulators, but to simplify things for our discussion, we'll assume there's only one.
If you just type adb at the command prompt, you get the help information for adb: Android Debug Bridge version 1.0.20
-s <serial number> -p <product name or path>
devices directs command to the only connected USB device returns an error if more than one USB device is present.
directs command to the only running emulator. returns an error if more than one emulator is running.
directs command to the USB device or emulator with the given serial number simple product name like 'sooner', or a relative/absolute path to a product out directory like 'out/target/product/sooner'.
If -p is not specified, the ANDROID_PRODUCT_OUT
environment variable is used, which must be an absolute path.
list all connected devices device commands: adb push <local> <remote> adb pull <remote> <local> adb sync [ <directory> ]
copy file/dir to device copy file/dir from device copy host -> device only if changed (see 'adb help all')
adb shell adb shell <command> adb emu <command> adb logcat [ <filter-spec> ] adb forward <local> <remote>
adb uninstall [-k] <package> adb bugreport run remote shell interactively run remote shell command run emulator console command View device log forward socket connections forward specs are one of: tcp:<port>
localabstract:<unix domain socket name> localreserved:<unix domain socket name> localfilesystem:<unix domain socket name> dev:<character device name> jdwp:<process pid> (remote only) list PIDs of processes hosting a JDWP transport push this package file to the device and install it ('-l' means forward-lock the app) ('-r' means reinstall the app, keeping its data) remove this app package from the device ('-k' means keep the data and cache directories) return all information from the device that should be included in a bug report.
adb help adb version show this help message show version num
DATAOPTS: (no option) -w -d don't touch the data partition wipe the data partition flash the data partition scripting: adb wait-for-device adb start-server adb kill-server adb get-state adb get-product adb get-serialno adb status-window adb remount block until device is online ensure that there is a server running kill the server if it is running prints: offline | bootloader | device prints: <product-id> prints: <serial-number>
continuously print device status for a specified device remounts the /system partition on the device read-write networking:
adb ppp <tty> [parameters] - Run PPP over USB. Note: you should not automatically start a PDP connection. <tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1 [parameters] - Eg. defaultroute debug dump local notty usepeerdns adb sync notes: adb sync [ <directory> ] <localdir> can be interpreted in several ways:
- If <directory> is not specified, both /system and /data partitions will be updated.
- If it is "system" or "data", only the corresponding partition is updated.
Here are a few of the more useful adb commands. There is much more information about these and other adb commands in the Android documentation and online.
Displays a list of devices and emulators that the adb server knows about. This is a good way to find the TCP/IP port for an emulator or device if you don't already know it. The port number is also displayed in the title of each emulator at the top of its window. If there's only one device or emulator running (the normal case, unless you're debugging a multidevice application), any adb commands you issue automatically go to that target. The -s and -e options are provided for multidevice applications to let you specify a device or emulator. adb shell
This connects you with a shell running on the target and gives you a # prompt. The shell is a simplified Unix-like shell, so you can use the usual shell commands (ls, cat, rm, ps, etc.) to explore the target and make changes as appropriate. Ctrl-D or exit will get you out of the shell and back to your environment on the host.
A particularly useful shell command (you have to get into the shell with adb shell first) for manipulating SQLite database files. The sqlite3 program is further described in Chapter 8, and on the SQLite website (http://www.sqlite.org). You can optionally include the path to the database file you want to manipulate (the MJAndroid database, for example, would be in data/data/com.micro jobsinc.mjandroid/databases/MJAndroid). adb logcat [filter_spec]
This is another way of looking at the logcat log on the target. When you run it, it dumps the existing log to your virtual terminal and continues to send additional log entries as they are generated in the running system. The command is normally entered with a trailing &, the Unix parameter for "run this in a separate process," so that you can go on and use the terminal for other commands (including, eventually, to kill the logcat process). The filter specs are of the form tag:priority, where tag and priority were described in "Logcat" on page 67. So the command to see all AndroidRuntime log entries of priority E would be:
adb logcat AndroidRuntime:E &
This is also useful for reading the "other" logs, of which there are two: radio and events. The radio log is accessed through a command like:
Similarly, to read the events log, enter:
This can be used to install or reinstall an application. The -l option forward-locks the installation (preventing the application from being copied later to another device), and the -r option reinstalls the application without overwriting the existing application data. The file_spec must be a valid, signed .apk file for the application to be installed. adb uninstall [-k] package
This uninstalls the application with the given package name. The package parameter needs to be the full name of the package, without the ".apk" extension. So to uninstall MicroJobs, for example, you'd type:
adb uninstall com.microjobsinc.mjandroid If you want to keep the application's associated data, you include the -k option.
adb push local remote
This command copies a file from the local name on the host to the remote name on the target. adb pull remote local
This is the counterpart to the previous command, and copies a file from the target to the host.
Was this article helpful?