Introduction

ADB is a command-line tool that lets you communicate with a device. The adb command facilitates a variety of device actions, such as installing and debugging apps, and it provides access to a Unix shell that you can use to run a variety of commands on a device. It is a client-server program that includes three components:

  • A client, which sends commands. The client runs on your development machine.
  • A daemon (adbd), which runs commands on a device. The daemon runs as a background process on each device.
  • A server, which manages communication between the client and the daemon. The server runs as a background process on your development machine.

Useful Command List

Managing Device

  1. List devices/emulators are attached
    adb devices
    adb devices -l
  2. Reboot your device
    adb reboot
  3. In case of multiple device connected to the development machine, use below command. adb devices command shows the id of connected devices.
    adb -s (redirect command to specific device) <id>
    adb –d (directs command to only attached USB device) <id>
    adb –e (directs command to only attached emulator) <id>
  4. Wait for the device until it is connected back to development PC
    adb wait-for-device
  5. Connect to the device by its IP address.
    adb connect device_ip_address
  6. Start the bootloader
    adb reboot-bootloader

Basic Commands

  1. Restarts adbd with root permissions
    adb root
  2. Manage ADB server
    adb start-server
    adb kill-server
  3. Manage terminal on the device
    adb shell (starts the backround terminal)
    exit (exits the background terminal)
  4. List all the commands
    adb help

Device Security

  1. DM-verity is a security measure to check the integrity of your device. The disable-verity command will disable dm-verity protection which lives in the kernel. Disabling dm-verity will retain kernel modifications by bypassing this protection. So if a rooting software compromises the system before the kernel comes up, it will retain those modifications. The enable-verity will enable the protection if it is disabled in a user debug build.

    adb disable-verity // Disable Verity
    adb enable-verity // Enable Verity

Manage Package

  1. Install a apk
    adb shell install (install app)
    adb shell install (install app from phone path)
  2. Uninstall an application
    adb uninstall com.myapp.packagename

    Example
    adb install -d -g -r -t MediaPlayer.apk

    Above command will install the application in test mode and grant all the permisison.
    -g = Grant all permission
    -d = Debug
    -t = Test mode

File Operations

  1. Send a file to the device:
    adb push /home/myhome/image.png /sdcard/Download/
    adb push –sync Clips/ /sdcard/

    First command will copy ‘image.png’ from the development machine to ‘Download’ folder on the device
  2. Retrieve a file from the device
    adb pull /sdcard/Download/pic.jpg /home/myhome/Desktop/

    This command will copy ‘pic.jpg’ from the device to ‘Desktop’ folder on the development machine.

Phone Info

  1. Show device state
    adb get-statе (print device state)
  2. Get the device serial number
    adb get-serialno
  3. Get the device IMEI
    adb shell dumpsys iphonesybinfo
  4. List TCP connectivity
    adb shell netstat
  5. Show battery status
    adb shell dumpsys battery
  6. List phone features
    adb shell pm list features
  7. List all services
    adb shell service list
  8. Show activity info
    adb shell dumpsys activity
  9. Print process status
    adb shell ps
  10. Displays the current screen resolution
    adb shell wm size
  11. Retrieve just a single property
    adb shell getprop ro.build.version.sdk
    Above command retrieve Android api level the devices has.

Permissions

  1. Grant a permission
    adb shell pm grant your.app.package android.permission.INTERNET
  2. Revoke a permission
    adb shell pm revoke your.app.package android.permission.INTERNET
  3. List permission groups definitions
    adb shell permissions groups
  4. List permissions details
    adb shell list permissions -g -r
  5. Reset permission
    adb shell pm reset-permissions -p your.app.package

Package Info

  1. List package names along with path to apk
    adb shell list packages -r
  2. List third party package names
    adb shell list packages -3
  3. List only system packages
    adb shell list packages -s
  4. List info on one package
    adb shell dump <package name>
  5. List all the installed packages in the device
    adb shell pm list packages

Configure Device

  1. Change the battery status
    adb shell dumpsys battery set level
    adb shell dumpsys battery reset
    adb shell dumpsys battery set usb
  2. Sets the resolution to W(width) x H(height)
    adb shell wm size WxH
  3. Get the list of available properties
    adb shell getprop
  4. Disable SELinux policy
    adb shell setenforce 0
  5. Change screen density
    adb shell wm density 100

Logging

  1. Collect log from the device
    adb logcat
  2. Clear the current logs on the device.
    adb logcat -c
  3. Save the logcat output to a file on the development machine
    adb logcat -d > [path_to_file]
  4. Will dump the whole device information like dumpstate, dumpsys and logcat output.
    adb bugreport > [path_to_file]
  5. Set log buffer size
    adb logcat -G 128M
    In the above command log buffer size is set to 128 mega bytes
  6. Filter logs based on log type
  7. adb logcat *:<Log Type>

    Supported log types are
    • V: Verbose (lowest priority)
    • D: Debug
    • I: Info
    • W: Warning
    • E: Error
    • F: Fatal
    • S: Silent (highest priority, on which nothing is ever printed)

The following filter expression displays all log messages with priority level “warning” and higher, on all tags:
adb logcat *:W

Process Details

  • Get the name of current activity
    adb shell dumpsys window windows | grep -E ‘mCurrentFocus|mFocusedApp’
  • Stop an Application
    adb shell am force-stop com.my.app.id
  • List all running processes on the device and their process ids
    adb shell ps
  • Kill the Process
    adb shell kill
  • Kill all processes associated with (the app’s package name).
    adb shell am kill [options]

Emulate device

  1. Set specific screen resolution/density
    adb shell wm size 2048×1536
    adb shell wm density 288
  2. Reset resolution and/or density to default
    adb shell wm size reset
    adb shell wm density reset

Screen capture

  1. Take screenshoot
    adb shell screencap -p /sdcard/screenshot.png
  2. Record screen
    adb shell screenrecord /sdcard/Login.mp4