User Tools

Site Tools


How To Debug Android By Adb

About this Article

What is the ADB

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 <sdk>/platform-tools/.

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.

How to use ADB

  • Similar to many commands,when you don't know what can the usb do,you can import:
 $adb help
  • Then you will see some introductions
device commands:
  adb push <local> <remote>    - copy file/dir to device
  adb pull <remote> [<local>]  - copy file/dir from device
  adb sync [ <directory> ]     - copy host->device only if changed
                                 (-l means list but don't copy)
                                 (see 'adb help all')
  adb shell                    - run remote shell interactively
  adb shell <command>          - run remote shell command
  adb emu <command>            - run emulator console command
  adb logcat [ <filter-spec> ] - View device log
  adb forward <local> <remote> - forward socket connections
                                 forward specs are one of:
                                   localabstract:<unix domain socket name>
                                   localreserved:<unix domain socket name>
                                   localfilesystem:<unix domain socket name>
                                   dev:<character device name>
                                   jdwp:<process pid> (remote only)

Directing Commands to a Specific Emulator/Device Instance

  • You can 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. For example:
 $adb -s emulator-5556 install helloWorld.apk

Note:if you issue a command without specifying a target emulator/device instance while multiple devices are available, adb generates an error.

Installing and unstalling 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:

 $adb install <path_to_apk>

And then uninstall it:

 $adb uninstall <path_to_apk>
  • 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.

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 the install 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.

  • You can use this to copy file or directory from the emulator or device:
 $adb pull <remote> <local>
  • You can use this to copy file or directory to the emulator or device:
 $adb push <remote> <local>

Note: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). For example:

 $adb push /work/tools/adb.txt /system

Issuing Shell Commands

  • 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 <serialNumber>] shell <shell_command>
  • Or enter a remote shell on an emulator/device like this:
 $adb [-d|-e|-s <serialNumber>] 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

Unable to connect adb

Some people will get such note when they use $adb device

List of devices attached
???????????? no permissions
  • It is say that you don't have the permission to run ADB by USB.So you can set the USB permission.
  • (1)find your USB ID:
$ lsusb
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 003: ID 18d1:0003 Google Inc.
Bus 002 Device 002: ID 0461:4d81 Primax Electronics, Ltd
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 010: ID 0bb4:0c87 High Tech Computer Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Note: And then find your USB ID.Here I can find my USB ID is Bus 001 Device 003: ID 18d1:0003 Google Inc.

  • (2)Add the rule
 $sudo vim /etc/udev/rules.d/70-android.rules
  • (3)Add the message
 SUBSYSTEM=="usb", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="0003",MODE="0666"

Note:The 18d1 and 0003 is my ID,you must add your ID.

  • (4)Restart the service
 $sudo chmod a+rx /etc/udev/rules.d/70-android.rules
 $sudo service udev restart
  • (5)Swap the USB cable and enter the directory where have the adb.
 $sudo ./adb kill-server
 $./adb devices
 $./adb root
how_to_debug_android_by_adb.txt · Last modified: 2013/12/23 14:50 (external edit)