Skip to content

lana-20/adb-commands

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 

Repository files navigation

ADB commands can be used to debug Android devices, install or uninstall apps, and get information about a connected device. The adb util can practically replicate any action a user performs on the UI.

ADB Shell commands provide access to a Unix Shell that runs a command directly on an Android device. As soon as I execute an adb shell command on the command terminal, it sends a signal to my Android device and triggers the remote shell command console. Thus ADB shell commands let me control my Android device.

Using ADB commands, I can reboot my device, push and pull files, create a backup and restore it, and sideload an updated zip package or an APK. ADB Shell commands, however, work on a much deeper level. They can be used to change the resolution of my device display, uninstall bloatware or system apps, enable and disable features, modify the system files, and change their configuration directly using commands from my computer.

TOC

Common Commands

Here is a list of some common ADB (Android Debug Bridge) commands that I use frequently as a mobile QA engineer:

adb devices -l - Lists all the devices that are connected to my computer and are recognized by ADB. To make sure the device I'm planning to manipulate is actually connected, I always start with the command adb devices and then issue my next adb command.

  • For example, confirm that the target devices are connected to the host computer and are recognized by ADB:

      $ adb devices
      List of devices attached
      <udid> device
    
      $ adb devices
      List of devices attached
      A6PVD6LNFQ578HUS  device
      emulator-5554 device
      emulator-5556 device
    
    • 📝 The emulator -list-avds command also lists devices, but only the virtual ones, and not necessarily the ones which are awake and running.

          % emulator -list-avds
          Nexus_6_API_30
          Pixel_6_API_33_-_Galaxy_S23_Skin
          Pixel_6_Pro_API_33
      

adb push <local_file> <remote_destination> - Copies a file, or directory and its sub-directories, from my computer to the connected device.

  • For example:

      adb push myfile.txt /sdcard/myfile.txt
    

adb pull <remote_file> <local_location> - Copies a file, or a directory and its sub-directories, from the connected device to my computer.

adb logcat - Displays the logcat output for the connected device, which can be helpful for debugging.

adb shell - Opens a shell on the connected device, allowing me to run commands on the device directly.

  • For example, on a physical Android device ADB runs in a Secure mode. In order to access the device shell I first have to switch to the ADB root user with command adb root or adb shell su. An emulator runs in a Unsecure mode and hence doesn't require root access but the Switch User su binary is available in an emulator's /system/xbin/su directory. On a production build, for example, on my OnePlus phone this folder is empty.

    Screenshot 2023-03-04 at 6 54 25 PM

adb shell dumpsys <system_service> - Displays detailed information about a specific system service on the device.

  • I often use the dumpsys util to to obtain the minSDK version info about the package when selecting/configuring devices for proper test coverage. For example, minSDK 25 stands for Android version 7.1:

       % adb shell dumpsys package com.myapp.app | grep version
       versionCode=72 minSDK=25 targetSDK=31 versionName=2.0.72
    

    On Windows, replace grep with either find or findstr, or download Cygwin and use it to run Unix/Linux commands.

adb -d shell ip addr show wlan0 - Finds the device's IP address. Alternatively, I can search in the phone settings.

adb tcpip 5555 - Sets the target device to listen for a TCP/IP connection on port 5555, when connecting to a device over Wi-Fi.

adb connect device_ip_address:5555 - Connects to the device by its IP address.

adb start-server - Starts the adb server.

adb kill-server - Resets the adb host / Stops the adb server. In some cases, I need to terminate the adb server process and then restart it to resolve a problem (e.g., if adb does not respond to a command). After stopping, I can then restart the server by issuing any adb command.

adb reboot - Reboots the connected device.

  • For example, I can choose to reboot my device fully or to reboot it to bootloader or recovery. Or I can choose to reboot it to sideload, which is a new system image provided by Google for the line Nexus devices.:

      adb reboot bootloader
      adb reboot recovery
      adb reboot sideload (Nexus)
    

adb forward tcp:9222 localabstract:chrome_devtools_remote - Calls out to the Android Debug Bridge and forwards the Chrome dev tools port on the device to the local port 9222

adb shell setprop debug.layout true- Turns on the debug mode.

adb --help - Yields a detailed list of all supported adb commands.

These are just a few examples of the many ADB commands that are available. Android documentation has a complete list of ADB commands.

In the following section, I cover some of the commands I use as a mobile tester most frequently.

Install, Update, Uninstall an App

adb install ~/apks/my_app.apk - Installs an app (specified by the APK file path) on a single connected device.

adb devices | grep device | grep -v devices | cut -f 1 | xargs -I {} adb -s {} install ~/apks/my_app.apk - Installs an app (specified by the APK file) on multiple connected devices.

adb install -r Downloads/<file_name>.apk - Reinstalls an (updated) app on the connected device. Add -r before the path to .apk.

adb uninstall <package_name> - Uninstalls an app by its package name from the connected device.

adb -d - Sends a command to the only connected physical device (CONNECTED via USB).

adb -e - Sends a command to the only connected emulator (CONNECTED via TCP/IP).

Once a physical device is connected via TCP/IP, use the -e or -s <serial_number> redirection option, since -d sends a command to devices connected via USB.

adb -s <serial_number> - Use if more than one device or more than one emulator are connected.

  • For example:

      adb -s emulator-5554 install my_app.apk
    

ADB Shell Commands

ADB Shell commands provide access to a Unix Shell that runs a command directly on my Android device. As soon as I execute an adb shell command on the command terminal, it sends a signal to my Android device and triggers the remote shell command console. Thus ADB shell commands let me control my Android device.

adb [-d | -e | -s serial_number] shell - Uses the shell command to start an interactive shell through adb.

adb [-d |-e | -s serial_number] shell <shell_command> - Uses the shell command to issue device commands through adb.

adb shell ls /system/bin - Lists available [Unix/Linux-like] CLI tools.

adb shell getprop - Returns the list of the device's properties; can be useful when I need to get, e.g., a device model or an Android version.

adb shell getprop ro.build.version.release - Returns the Android version installed on the device, e.g., Android 13.0.

adb shell getprop ro.build.version.sdk - Returns the API level, e.g., 33.

adb shell getprop sys.boot_completed - There is an interesting property that I can access through shell, which is boot_completed. When setting up a CI pipeline involving Android emulators, I come across this command. This is a flag that I can periodically query for, and wait until its state changes to True. Therefore I can be sure that my system is fully booted and proceed with installing apps on it, and perform automated tests.

adb shell am - Issues commands with (calls) the Activity Manager am tool to perform various system actions, such as start an activity, force-stop a process, broadcast an intent, modify the device screen properties, etc.

  • For example:

      adb shell am start -a android.intent.action.VIEW
    
  • Or, force-close the app:

      adb shell am force-stop com.my_app.standalone.test2
    
  • Or, start the app - need to know the app's 1st activity created on launch:

      adb shell am start <package_name>/<activity_name>
      adb shell am start com.my_app.standalone.test2/com.my_app.standalone.features.auth.AuthActivity
    

adb shell pm - Issues commands with (calls) the Package Manager pm tool to perform actions and queries on app packages installed on the device.

  • For example:

      adb shell pm uninstall com.example.MyApp
    
  • Or, clear all package data from the device - after running this command, the app behaves as if it was just installed:

      adb shell pm clear com.my_app.standalone.test2
    

adb shell dpm - Issues commands with (calls) the Device Policy Manager dpm tool to control the active admin app or change a policy's status data on the device.

  • For example:

      adb shell dpm force-network-logs
    

adb shell cmd testharness enable - Resets test devices between tests, e.g., to remove user data and reset the test environment.

adb shell sqlite3 db_location - sqlite CLI program for examining SQLite databases.

  • For example:

      $ adb -s emulator-5554 shell
      $ sqlite3 /data/data/com.example.app/databases/rssitems.db
      SQLite version 3.3.12
      Enter ".help" for instructions
    

adb shell monkey -p <package_name> -v 500 - Performs 500 random interactions on the given app. Monkey is a program that runs on your emulator or device and generates pseudo-random streams of user events such as clicks, touches, or gestures, as well as a number of system-level events. The tool is useful for stress/interruption testing.

  • For example, send 1000 random clicks and touches to the app:

      adb shell monkey -p com.appiumpro.the_app -v 1000
    
    • When I conduct interruption testing, I use the adb shell util to turn the mobile and WiFi service networks on and off. For example:

          adb shell svc data enable
          adb shell svc data disable
          adb shell svc wifi enable
          adb shell svc wifi disable
      

      In the context of interrupt testing, I can also mimic a call on an emulator:

          adb shell am start -a android.intent.action.CALL
      

adb shell settings put secure enabled_accessibility_services - Uses TalkBack with ADB for accessibility testing.

exit - Exits an interactive shell.

adb shell screencap file_name.png

  • For example:

    • adb shell screencap /sdcard/screenshot.png
      • saves the screenshot to the device's SD card - must always state the .png format explicitly
    • adb shell screencap -p > ~/Desktop/screenshot.png
      • -p forces screencap to use PNG format

    Alternatively, I can take a screenshot using (1) emulator settings and (2) in Android Studio (under Logcat) with buttons.

adb shell screenrecord [options] file_name.mp4

  • For example:

    • adb shell screenrecord /sdcard/ErrorMsgRegistrationScreen.mp4

    Or, split the command into several steps:

     	$ adb shell
     	shell@ $ screenrecord --verbose /sdcard/demo.mp4
     	(press Control + C to stop)
     	shell@ $ exit
     	$ adb pull /sdcard/demo.mp4
    

    ✘ Give the file a meaningful name, e.g., use a bug number as a file name.

    💥 Limitations:

    1. 🔇 The recording has no audio sound.

    2. ⏳ The recording time limit is 180 seconds (3 minutes). I may, however, change that by adding the --time-limit argument. I can use the --time shortcut in lieu of the --time-limit.

       adb shell screenrecord --time-limit <TIME> /sdcard/ErrorMsgRegistrationScreen.mp4
      
      • For example, produce a 2-minute video:

          adb shell screenrecord --time 120 /sdcard/ErrorMsgRegistrationScreen.mp4
        

    Since the video is saved to the SD card, I need to pull it from the device to my current working directory:

     adb pull /sdcard/ErrorMsgRegistrationScreen.mp4
    

    Or, pull to a specified destination:

     adb pull /sdcard/ErrorMsgRegistrationScreen.mp4 ~/Desktop
    

    If no destination directory is specified, the file is stored at my current working directory. To check the current directory location, use command pwd on Mac or cd on Windows.

    ❌ To remove a file from the device, run the rm command:

     adb shell rm /sdcard/ErrorMsgRegistrationScreen.mp4
    

    Alternatively, I can record a video in Android Studio.

  • adb push <local_file> <remote_destination>: copy a file from your computer to the connected device

    • When testing an upload feature on an app like YouTube or Instagram, I might need to push some files to my device’s /sdcard directory. I can copy a file (image, video, etc.) from the host machine to the mobile device with the following command:

          adb push Desktop/image.png /sdcard/Pictures
      

      I don’t use shell in the push command, because I push from my computer. adb push is not a util that lives on my mobile device. It’s just a command to exchange files between the the host machine and the connected device. push and pull work in tandem. pull helps me get files from the device, while push helps me push files onto the device.

      📝 If the destination directory name does not exist on the device, the command will create a new directory with that name.

  • adb pull <remote_file> <local_destination>: copy a file from the connected device to your computer

    • I often use the command to get a copy of my device screen recording on my computer. I also utilize the command to get a copy of an app on my computer by its package name. For example:

         adb pull /sdcard/<video_name>.mp4 ~/Documents
         adb pull /data/app/~~TwHdRTDoNtvL1DRfi8jrCS==/com.myapp.app-3MsubaTFWY_02Tgq-TNEAN==/base.apk
      

dumpsys is a tool which runs on Android devices and provides information about system services. Sometimes it's helpful for retrieving info about the device memory or battery usage.

List options available with dumpsys

  • adb shell dumpsys | grep "DUMP OF SERVICE"

Check the current status of the phone

  • adb shell dumpsys battery

      $ adb shell dumpsys battery
        USB powered: true
        
      $ adb shell dumpsys battery unplug
              
      $ adb shell dumpsys battery
        USB powered: false
    

Change battery level

  • adb shell dumpsys battery set level <battery_percent_value>
    • eg, adb shell dumpsys battery set level 20

Test how the device behaves under low power conditions

  • adb shell settings put global low_power 1

Mimic a disabled battery charge

  • adb shell dumpsys battery unplug
    • only MOCKS the battery status. When checking the current indicator on the USB cable, this does not change the amount of flowing current at all. So the battery is still charging. It only changes what apps think about the state. Eg, Google Play would not start updating, if you have configured that it can only update when charging.

      $ adb shell dumpsys battery unplug
      
      $ adb shell service list | grep battery
      88      batterymanager: [android.app.IBatteryService]
      107     batterystats: [com.android.internal.app.IBatteryStats]
      114     batteryproperties: [android.os.IBatteryPropertiesRegistrar]
      
      $ adb shell dumpsys batterymanager
      
      Current Battery Service state:
        (UPDATES STOPPED -- use 'reset' to restart)
        AC powered: false
        USB powered: true
      
      $ adb shell dumpsys batterymanager unplug
      
      Current Battery Service state:
        (UPDATES STOPPED -- use 'reset' to restart)
        AC powered: false
        USB powered: false
      

For a rooted device:

  • Enable battery Charging (mimic that device is charging, even if it's not -> simulate the uncharged state of the phone)

    • adb shell dumpsys battery set ac 1
    • adb shell dumpsys battery set usb 1
    • adb shell dumpsys battery set wireless 1
  • Disable battery Charging (mimic that device isn't charging, even if it is)

    • adb shell dumpsys battery set ac 0
    • adb shell dumpsys battery set usb 0
    • adb shell dumpsys battery set wireless 0

Restore the phone to its state

  • adb shell dumpsys battery reset

Inspect an app's memory usage in one of two ways:

  1. Over a period of time using procstats.
  • Get application memory usage stats over the last three hours, in human-readable format
    • adb shell dumpsys procstats --hours 3
  1. At a particular point in time using meminfo.
  • Record a snapshot of how the app's memory is divided between different types of RAM allocation

    • adb shell dumpsys meminfo package_name|pid [-d]

    The -d flag prints more info related to Dalvik and ART memory usage. The output lists all of your app's current allocations, measured in KBs.

The output lists all of the app's current allocations, measured in kilobytes.

Change Runtime Permissions

Grant and revoke any runtime permission the application may require. It saves time, especially when working with an emulator.

adb shell dumpsys package <package_name> | grep permission - Returns the runtime permissions the app is using.

  • For example, the following (partial) output means the user is currently allowing the app to access the camera:

    ...
    android.permission.CAMERA: granted=true
    ...
    

adb shell pm grant <package_name> android.permission.CAMERA - Grants a permission.

adb shell pm revoke <package_name> android.permission.CAMERA - Revokes a permission.

Find App Package Name

Review the ADB and AAPT approaches here.


dumpsys

Test power-related issues

Test apps on Android

What to test in Android

How to disable battery charging during ADB connection?

Android Command-line tools

Enter Doze Mode With Adb

Optimize for Doze and App Standby

Android Doze mode is enabled and restored using commands

ADB Shell Commands List and Detailed Cheat Sheet

About

Android Debug Bridge

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published