Mastering Android: A Comprehensive Guide to ADB Commands

Mastering Android: A Comprehensive Guide to ADB Commands

Unlocking the full potential of your Android device with ADB

The article was originally published in Lambdatest's blog here.

The Android Debug Bridge (ADB) is a command-line tool that enables advanced users and developers to interface with Android devices. ADB enables developers to run commands on Android devices, access system files, install and remove apps, and conduct a variety of other operations not available via the regular Android user interface.

To use ADB, you will need to have the Android SDK (Software Development Kit) installed on your computer. The Android SDK includes ADB, as well as other tools and libraries needed for Android development. Once you have the SDK installed, you can access ADB by opening a command prompt or terminal window and navigating to the platform-tools directory in the SDK.

Once you have ADB set up, you can use it to run a variety of commands on your Android device. Here are some of the most useful and handy ADB commands that you should be aware of:

  1. adb devices: This command displays a list of every connected device that ADB has currently detected on your computer.

     > adb devices
     List of devices attached
     emulator-5554   device
    
  2. adb install [apk file]: The APK file (android application package) is installed on your device by this command.

     > adb install ./workspace/VodQaAdvancedAppium/apps/VodQA.apk
     Performing Streamed Install
     Success
    
  3. adb uninstall [package name]: This command removes an application from your device, where the package name is the application's package name.

     > adb uninstall com.vodqareactnative
     Success
    
  4. adb shell: With the help of this command, you can perform commands and get direct access to system files on your device.

     > adb shell
     generic_x86_64:/ $ ls
     acct      bin         config  data_mirror    dev   init.environ.rc  metadata  oem      res      sys         vendor
     adb_keys  bugreports  d       debug_ramdisk  etc   linkerconfig     mnt       proc     sdcard   system
     apex      cache       data    default.prop   init  lost+found       odm       product  storage  system_ext
     generic_x86_64:
    
  5. adb pull [source] [destination]: This command transfers a file or directory from the device to your computer, with the destination being the location on your computer where the file or directory will be copied to. The source is the file or directory on the device.

     > adb pull config .
     config/: 0 files pulled, 0 skipped.
    
  6. adb push [source] [destination]: This command transfers a file or directory from your computer to the target device, with the destination being the location on the target device and the source being the file or directory on your computer.

  7. adb shell pm list packages: This command lists all the installed packages (apps) on the device.

     > adb shell pm list packages
     package:com.android.cts.priv.ctsshim
     package:com.android.internal.display.cutout.emulation.corner
     package:com.android.internal.display.cutout.emulation.double
     package:com.android.providers.telephony
     package:com.android.dynsystem
     package:com.android.theme.icon.pebble
     package:com.android.providers.calendar
     .......
    
  8. adb shell pm clear [package name]: This command clears the data and cache of an app, where the package name is the package name of the app you want to clear.

  9. adb logcat: This command displays the logcat output of the device, which can be useful for debugging and troubleshooting.

  10. adb reboot: This command reboots the device.

  11. adb shell dumpsys: This command shows a wide range of system information on your device, including battery status, memory usage, and process information.

  12. adb shell dumpsys activity: This command shows information about the currently running activities on your device.

  13. adb shell dumpsys package [package name]: This command shows detailed information about a specific package (app), where the package name is the package name of the app you want to get information about.

  14. adb shell dumpsys batterystats: This command shows detailed battery usage statistics on your device.

  15. adb shell dumpsys meminfo [package name]: This command shows detailed memory usage statistics for a specific package (app), where the package name is the package name of the app you want to get information about.

  16. adb shell dumpsys connectivity: This command shows information about the device's network connectivity, including the current network type and connection status.

  17. adb shell dumpsys surface: This command shows detailed information about the currently visible surfaces on your device.

  18. adb shell dumpsys display: This command shows detailed information about the device's display, including the current display mode and resolution.

  19. adb shell dumpsys cpuinfo: This command shows detailed CPU usage statistics on your device.

  20. adb shell dumpsys netstats: This command shows detailed network usage statistics on your device.

  21. adb shell dumpsys window: This command shows detailed information about the device's window manager, including the current focus and layout of all windows.

  22. adb shell dumpsys input: This command shows detailed information about the device's input manager, including the current state of all input devices and input queues.

  23. adb shell dumpsys power: This command shows detailed information about the device's power manager, including the current battery level, power sources, and wake lock statistics.

  24. adb shell dumpsys battery: This command shows detailed information about the device's battery, including the current battery level, voltage, and temperature.

  25. adb shell dumpsys batterystats --reset: This command resets the battery usage statistics on the device.

  26. adb shell dumpsys meminfo -a [package name]: This command shows detailed memory usage statistics for all processes of a specific package (app), where the package name is the package name of the app you want to get information about.

  27. adb shell dumpsys gfxinfo [package name]: This command shows detailed graphics performance statistics for a specific package (app), where the package name is the package name of the app you want to get information about.

  28. adb shell dumpsys input [input device id]: This command shows detailed information about a specific input device, where the input device id is the ID of the input device you want to get information about.

  29. adb shell dumpsys surface [window name]: This command shows detailed information about a specific window, where the window name is the name of the window you want to get information about.

  30. adb shell screencap [filename]: This command takes a screenshot of the device and saves it to the specified filename on your computer.

  31. adb shell screenrecord [filename]: This command records the screen of the device and saves it to the specified filename on your computer.

  32. adb shell input [event]: This command sends an input event to the device, where the event can be a variety of actions such as key presses, touch events, and more.

  33. adb shell monkey [options] [package name]: This command runs a random stress test on the specified package (app), where options can be used to specify the number of events to generate, and the package name is the package name of the app you want to run the test on.

  34. adb shell am [start|instrument] [options] [package/activity]: This command starts an activity or runs an instrumentation on the specified package (app), where options can be used to specify additional parameters such as the target package, component, and data and package/activity is the package name or activity name you want to start or run the instrumentation on.

  35. adb shell dumpsys activity [activity name]: This command shows detailed information about a specific activity, including its state, intent, and more.

  36. adb shell dumpsys batterystats --charged : This command shows the battery statistics when the device is fully

  37. adb shell netcfg: This command displays the current network configuration on the device.

  38. adb shell top: This command displays the current CPU usage and process statistics on the device.

    > adb shell top
    Tasks: 245 total,   1 running, 244 sleeping,   0 stopped,   0 zombie
      Mem:  2028168K total,  1668304K used,   359864K free,  33271808 buffers
     Swap:  1521120K total,         0 used,  1521120K free,   921464K cached
    400%cpu   0%user   0%nice   7%sys 393%idle   0%iow   0%irq   0%sirq   0%host
       PID USER         PR  NI VIRT  RES  SHR S[%CPU] %MEM     TIME+ ARGS
      3305 shell        20   0  10G 4.4M 3.6M R 11.1   0.2   0:00.02 top
       313 bluetooth    20   0  10G 6.2M 5.1M S  7.4   0.3  88:39.49 android.hardwa+
    .......
    
  39. adb shell ps: This command displays the currently running processes on the device.

  40. adb shell cat [file path]: This command displays the contents of a file on the device, where the file path is the path to the file you want to view.

  41. adb shell ls [directory path]: This command displays the contents of a directory on the device, where the directory path is the path to the directory you want to view.

  42. adb shell df: This command displays the current disk usage statistics on the device.

  43. adb shell ifconfig: This command displays the current IP configuration on the device.

  44. adb shell wm: This command allows you to interact with the device's window manager, you can use it to display/hide the status bar, change the DPI, etc.

  45. adb shell dumpsys activity services [package name]: This command shows detailed information about the services running within a specific package (app), where the package name is the package name of the app you want to get information about.

  46. adb shell dumpsys activity broadcasts: This command shows detailed information about the broadcast receivers on the device.

  47. adb logcat -d | grep -i "FATAL": The device's log output is shown using the logcat command in this command, which then streams the output to the grep program, which searches the output for lines that contain the word FATAL. By doing so, all crash logs that have been stored in logcat will be displayed.

  48. adb logcat -d -s <PACKAGE_NAME>:F: This command gets crash logs of a specific app, which can be useful for debugging and troubleshooting.

  49. adb shell dumpsys activity | grep -i 'http|https': The activity manager's state is displayed by this command using the dumpsys activity command. The output is then piped to the grep command, which searches the output for lines containing the terms http or https.

  50. adb shell dumpsys package <PACKAGE_NAME> | grep -i 'permission': This command uses the dumpsys package command to display information about the specified package (app), where PACKAGE_NAME is the package name of the app you want to get information about, and then pipes the output to the grep command, which filters the output for lines that contain the word "permission". This will display all the permissions that the app is currently using.

These commands allow you to interact with the device in various ways and retrieve valuable information, such as the currently running processes, network configuration, disk usage, and more.

LambdaTest offers a set of commands that can be executed within your app automation test scripts via JavaScript executors. To execute adb commands on LambdaTest's real device cloud, you need to use the following command with the specified parameters. Please note that the number of available adb commands may be limited.

Map<String, Object> params = new HashMap<>();
params.put("command", "input-swipe");
params.put("startX", 200);
params.put("startY", 900);
params.put("endX", 200);
params.put("endY", 300);

// command to generate a swipe gesture
Object result = driver.executeScript("lambda-adb", params);

Here is the detailed set of ADB commands supported by LambdaTest on their platform.

In addition to supporting ADB commands, LambdaTest also provides a range of other mobile testing capabilities, including automated testing on real devices, device logs and video recordings, and more.

Overall, LambdaTest provides a robust platform for mobile automation testing that includes support for executing ADB commands.

Did you find this article valuable?

Support Srinivasan Sekar by becoming a sponsor. Any amount is appreciated!