Android Debug Bridge (adb)

ADB is an important tool for developers because it allows you to interact with Android emulators, and also with connected devices. There are many features covered in the ADB documentation but here are some useful commands:

  • adb kill-server and adb start-server – stop and start the ADB service if needed
  • adb devices – list the devices available (including emulators and phones or tablets attached to your computer)
  • adb install – install APK files
  • adb shell – execute commands on the Android system, for example:
    • adb shell pm list packages – list all the packages installed
    • adb shell pm path com.example.someapp – gets the file system path of the package specified
  • adb pull – extract files from the emulator or device

Simulate app spanning

Another use for ADB is simulating input commands, such as automating spanning of an app on Surface Duo. These screenshots illustrate the process of spanning by grabbing the handle at the bottom of the window, and dragging it towards the hinge until the span indicator covers both screens:

Three steps of automatically spanning an app using ADB commands

You can simulate this gesture with ADB, using the following command:

adb shell input touchscreen swipe 675 1780 1350 1500 3000

The first four values are the start and end coordinates of the drag gesture, and the final value is elapsed time (milliseconds). The time is important because if you drag too fast, the app is “flung” to the other screen rather than spanned. Three seconds works well.

You can also simulate app spanning and other gestures as part of UI Testing.

Debug with ADB commands

Follow these steps to debug using ADB commands:

  1. Find your emulator device ID

    Run C:\>adb devices.

    emulator-5554   device
    
  2. Find the package you want to debug.

    Run adb shell pm list packages.

    package:com.android.managedprovisioning
    package:com.android.smspush
    package:com.android.wallpaper.livepicker
    package:com.android.storagemanager
    package:com.android.bookmarkprovider
    package:com.android.settings
    package:com.android.calculator2
    package:com.android.vpndialogs
    package:com.android.email
    package:com.android.music
    
  3. Set the app to debug at startup (note the -w)

    Run adb shell am set-debug-app -w com.microsoft.device.display.samples.masterdetail.

  4. Start the app in the emulator.

    You will get a popup that the app is waiting for a debug to attach. You will need to re-run the set debug each time (or use --persistent option).

  5. Connect Android Studio Debugger

    In Android Studio menu options, select Run > Attach debugger. The emulator and process should be listed.

  6. Point to source code and set breakpoints.