How To Use Android Debug Bridge (ADB)

ADB is a powerful tool, it is an abbreviation of Android Debug Bridge. It is located in %ANDROID_HOME%\platform-tools like C:\Users\Jerry\AppData\Local\Android\sdk\platform-tools. It provides a lot of useful subcommands for android developers to operate between android physical or virtual devices and computers. It can be used to copy files, install apps, or execute Linux commands on an android device directly. This article will show you how to use it in detail.

1. Android ADB Commands Examples

1.1 List Currently Running Android Devices Connected To Your Computer.

  1. First, you need to enable USB debugging mode in your physical Android device. You can click here to learn how to do this if you do not know.
  2. Connect the android device ( an android phone ) with a PC using a USB line.
  3. Open a dos window, and go to %ANDROID_HOME%\platform-tools directory.
  4. Type command adb devices, click enter key.
  5. You can see there is one device listed in the console, it is just the connected physical android phone.
    C:\> adb devices
    List of devices attached
    ZX1B32FFXF      device
  6. Launch Android Studio, click AVD(  Android Virtual Device ) manager icon in the top toolbar.
    avd-manager-in-the-tool-bar
  7. It will list all existing android virtual devices in a window as below.
    launch-avd-in-avd-list
  8. Click the green button at the end of each virtual device line to launch an AVD.
  9. After the virtual device startup. Run adb devices command in dos window again, you can see that both the physical android device ( android phone ) and the virtual device ( an android emulator ) are listed in the window.
    C:\> adb devices
    List of devices attached
    emulator-5554   device
    APU0216415003885   unauthorized

1.2 Copy Files Between Computer And Physical Or Virtual Device.

With adb command, copy files between devices and computer is very easy. By default, adb command always copy files to the currently connected device, but if there are more than one device is connected when you run the copy files command, it will prompt an error message like adb: error: failed to get feature set: more than one device/emulator. So you should only have one device connected to your PC.

C:\> adb push c:\windows-version.txt /sdcard/
adb: error: failed to get feature set: more than one device/emulator
  1. Run command adb push c:\windows-version.txt /sdcard/ to copy file c:\windows-version.txt to currently connected device /sdcard/ directory.
    C:\> adb push c:\windows-version.txt /sdcard/
    c:\windows-version.txt: 1 file pushed. 0.0 MB/s (42 bytes in 0.029s)
  2. Now you can check the file on your device to see whether it is there or not.
    adb-push-file-from-pc-to-android-device-check-file-in-android-device
  3. You may encounter an error message during this process such as “This adb server’s $ADB_VENDOR_KEYS is not set“, just do like the detail message suggests “Try ‘adb kill-server’ if that seems wrong. Otherwise check for a confirmation dialog on your device.” to resolve all the problems.
    C:\> adb push c:\windows-version.txt /sdcard/
    adb: error: failed to get feature set: device unauthorized.
    This adb server's $ADB_VENDOR_KEYS is not set
    Try 'adb kill-server' if that seems wrong.
    Otherwise check for a confirmation dialog on your device.
  4. Run command adb pull /sdcard/roam.txt C:\WorkSpace will pull files from android device to current PC’s C:\WorkSpace directory.

1.3 Open Shell Window In Android Device.

The kernel of android platform is Linux-based, and sometimes developers want to open shell window of android platform directly. This allows you to perform some common Linux commands in shell window, such as ls, mkdir, rm, etc. Below commands can achieve this.

  1. adb shell : This command will let you log in to the device as a common user. When the shell opened, we can run cd /sdcard/, ls -l to see the file we just copied.
    C:\Users\Jerry\AppData\Local\Android\sdk\platform-tools> adb shell
    HWNXT:/ $ pwd
    /
    HWNXT:/ $ cd /sdcard/
    HWNXT:/sdcard $ ls -l windows-version.txt

1.4 Install or Uninstall APK File.

  1. adb install [-r -s] apk_file : This command will install apk_file. -r means reinstall this apk, -s means install apk in sd card.
  2. adb uninstall [-k] package : This command will uninstall apk package. -k means to delete the app only but reserve app-related settings data and cache.

1.5 How To Resolve Unauthorized Device Issue When Run adb devices Command.

When you run command adb devices in section 1.1 step 9, it will list an Andriod device that is tagged as unauthorized as below.

C:\> adb devices
List of devices attached
emulator-5554   device
APU0216415003885   unauthorized

You can follow below steps to fix it.

  1. Unplug the android device from your computer.
  2. Remove “adbkey” related files which is saved in PC side user directory, such as “C:\Users\etc\.android\adbkey, C:\Users\etc\.android\adb_usb“.
  3. Type Settings —> Developer Options —> Revoke USB debugging authorizations menu item on android device and press OK in the popup dialog to enable it.
  4. Plug the Android device with the PC through a USB line.
  5. Open a dos window in PC and run command adb kill-server to stop the adb server.
  6. Run command adb start-server to start the adb server.
  7. Run command adb devices again, now you can see the android device will pass the authorization process and tagged as a device.
    C:\> adb devices
    List of devices attached
    emulator-5554   device
    APU0216415003885   device

For more adb command prarmeters, you can run adb -help to get help list of adb command. Or you can click here to go to adb online help documents.

5 2 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x