ADB

Android Debug Bridge

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:
  • 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.
  • 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.
  • A daemon, which runs as a background process on each emulator or device instance.
You can find the adb tool in /platform-tools/.
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.

Syntax


You can issue adb commands from a command line on your development machine or from a script. The usage is:
adb [-d|-e|-s ] 
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 -d-e, or -s option to specify the target device to which the command should be directed.

Commands


The table below lists all of the supported adb commands and explains their meaning and usage.
Table 1. Available adb commands
CategoryCommandDescriptionComments
Target Device-dDirect an adb command to the only attached USB device.Returns an error if more than one USB device is attached.
-eDirect an adb command to the only running emulator instance.Returns an error if more than one emulator instance is running.
-s Direct an adb command a specific emulator/device instance, referred to by its adb-assigned serial number (such as "emulator-5556").See Directing Commands to a Specific Emulator/Device Instance.
GeneraldevicesPrints a list of all attached emulator/device instances.See Querying for Emulator/Device Instances for more information.
helpPrints a list of supported adb commands.
versionPrints the adb version number.
Debuglogcat [option] [filter-specs]Prints log data to the screen.
bugreportPrints dumpsysdumpstate, andlogcat data to the screen, for the purposes of bug reporting.
jdwpPrints a list of available JDWP processes on a given device.You can use the forward jdwp: port-forwarding specification to connect to a specific JDWP process. For example:
adb forward tcp:8000 jdwp:472
jdb -attach localhost:8000
Datainstall Pushes an Android application (specified as a full path to an .apk file) to an emulator/device.
pull  Copies a specified file from an emulator/device instance to your development computer.
push  Copies a specified file from your development computer to an emulator/device instance.
Ports and Networkingforward  Forwards socket connections from a specified local port to a specified remote port on the emulator/device instance.Port specifications can use these schemes:
  • tcp:
  • local:
  • dev:
  • jdwp:
ppp  [parm]...Run PPP over USB.
  •  — the tty for PPP stream. For exampledev:/dev/omap_csmi_ttyl.
  • [parm]... — zero or more PPP/PPPD options, such asdefaultroutelocal,notty, etc.
Note that you should not automatically start a PPP connection.
Scriptingget-serialnoPrints the adb instance serial number string.See Querying for Emulator/Device Instances for more information.
get-statePrints the adb state of an emulator/device instance.
wait-for-deviceBlocks execution until the device is online — that is, until the instance state is device.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:
adb wait-for-device shell getprop
Note that this command does not 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 install requires the Android package manager, which is available only after the system is fully booted. A command such as
adb wait-for-device install .apk
would issue the install 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.
Serverstart-serverChecks whether the adb server process is running and starts it, if not.
kill-serverTerminates the adb server process.
ShellshellStarts a remote shell in the target emulator/device instance.See Issuing Shell Commands for more information.
shell [shellCommand]Issues a shell command in the target emulator/device instance and then exits the remote shell.

Copying Files to or from an Emulator/Device Instance


You can use the adb commands pull and push to copy files to and from an emulator/device instance. Unlike theinstall command, which only copies an APK file to a specific location, the pull and push commands let you copy arbitrary directories and files to any location in an emulator/device instance.
To copy a file or directory (and its sub-directories) from the emulator or device, use
adb pull  
To copy a file or directory (and its sub-directories) to the emulator or device, use
adb push  
In the commands,  and  refer to the paths to the target files/directory on your development machine (local) and on the emulator/device instance (remote). For example:
adb push foo.txt /sdcard/foo.txt

Issuing Shell Commands


Adb provides a Unix shell that you can use to run a variety of commands on an emulator or connected device. The command binaries are stored in the file system of the emulator or device, at /system/bin/...
Two of the most common command tools are activity manager (am) and package manager (pm).
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:
adb [-d|-e|-s ] shell 
Or enter a remote shell on an emulator/device like this:
adb [-d|-e|-s ] shell
When you are ready to exit the remote shell, press CTRL+D or type exit.


Enabling logcat logging


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.
You can use the logcat command to view and follow the contents of the system's log buffers. The general usage is:
[adb] logcat [option] ... [filter-spec] ...
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
and from a remote adb shell you use
logcat

Stopping the adb server


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.
To stop the adb server, use the kill-server command. You can then restart the server by issuing any other adb command.

IP usage


adb is usually used over USB. However, it is also possible to use over IP, as described here.
  1. Connect Android device and adb host computer to a common 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.
  2. Connect the device with USB cable to host.
  3. Make sure adb is running in USB mode on host.
    $ adb usb
    restarting in USB mode
  4. Connect to the device over USB.
    $ adb devicesList of devices attached######## device
  5. Restart host adb in tcpip mode.
    $ adb tcpip 5555
    restarting in TCP mode port: 5555
  6. Find out the IP address of the Android device: Settings -> About tablet -> Status -> IP address. Remember the IP address, of the form #.#.#.#.
  7. Connect adb host to device:
    $ adb connect #.#.#.#
    connected to #.#.#.#:5555
  8. Remove USB cable from device, and confirm you can still access device:
    $ adb devicesList of devices attached#.#.#.#:5555 device
You're now good to go!
If the adb connection is ever lost:
  1. Make sure that your host is still connected to the same Wi-Fi network your Android device is.
  2. Reconnect by executing the "adb connect" step again.
  3. Or if that doesn't work, reset your adb host:
    adb kill-server
    and then start over from the beginning.


Common Usage

* show the list of devices attached to the PC and their serial numbers
   > adb devices

* display the real time log of your device
  > adb logcat

* install apps
  > adb install G:\test.apk
or
  > adb push G:\test.apk /system/app

* pull
  > adb pull /system/app G:\temp

* to execute linux shell commands from your PC
  > adb shell




留言

熱門文章