Mastering Android: A Comprehensive Guide to ADB Commands

Srinivasan Sekar

Posted On: April 10, 2023

view count66170 Views

Read time10 Min Read

Mastering Android

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 unavailable via the regular Android user interface.

To use ADB, you must have the Android SDK (Software Development Kit) installed on your computer. The Android SDK includes ADB, as well as other debugging 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 run various commands on your Android device. Here are some of the handiest ADB commands you should be aware of:

adb devices

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

adb install [apk file]

The APK file (android application package) is installed on your device by this command.

adb uninstall [package name]

This command removes an application from your device, where the package name is the application’s package name.

adb shell

With the help of this command, you can perform commands and get direct access to system files on your device.

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 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.

adb shell pm list packages

This command lists all the installed packages (apps) on the device.

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.

adb logcat

This command displays the logcat output of the device, which can help with debugging and troubleshooting.

adb reboot

This command reboots the device.

adb shell dumpsys

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

adb shell dumpsys activity

This command displays information about the currently running activities on your device.

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.

adb shell dumpsys batterystats

This command shows detailed battery usage statistics on your device.

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.

adb shell dumpsys connectivity

This command shows information about the device’s network connectivity, including the current network type and connection status.

adb shell dumpsys surface

This command shows detailed information about the currently visible surfaces on your device.

adb shell dumpsys display

This command shows detailed information about the device’s display, including the current display mode and resolution.

adb shell dumpsys cpuinfo

This command shows detailed CPU usage statistics on your device.

adb shell dumpsys netstats

This command shows detailed network usage statistics on your device.

adb shell dumpsys window

This command shows detailed information about the device’s window manager, including the current focus and layout of all windows.

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.

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.

adb shell dumpsys battery

This command shows detailed information about the device’s battery, including the current battery level, voltage, and temperature.

adb shell dumpsys batterystats --reset

This command resets the battery usage statistics on the device.

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.

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.

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.

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.

adb shell screencap [filename]

This command takes a screenshot of the device and saves it to the specified filename on your computer.

adb shell screenrecord [filename]

This command records the screen of the device and saves it to the specified filename on your computer.

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.

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.

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.

adb shell dumpsys activity [activity name]

This command shows detailed information about a specific activity, including its state, intent, and more.

adb shell dumpsys batterystats --charged

This command shows the battery statistics when the device is fully charged.

adb shell netcfg

This command displays the current network configuration on the device.

adb shell top

This command displays the current CPU usage and process statistics on the device.

adb shell ps

This command displays the currently running processes on the device.

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.

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.

adb shell df

This command displays the current disk usage statistics on the device.

adb shell ifconfig

This command displays the current IP configuration on the device.

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.

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.

adb shell dumpsys activity broadcasts

This command shows detailed information about the broadcast receivers on the device.

adb logcat -d | grep -i "FATAL"

The device’s log output is displayed 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 stored in logcat will be displayed.

adb logcat -d -s :F

This command gets crash logs of a specific app, which can help debug and troubleshoot.

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.

adb shell dumpsys package | 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, while performing mobile app automation testing.

However, if you are performing mobile app automation testing on a real device cloud like LambdaTest, you can leverage a set of ADB 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.

Looking to run app test automation scripts via JavaScript Executors? Head over to our documentation on ADB commands to get started on the LambdaTest 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.

In short, ensuring that the application is free from errors before it is released to users is crucial, and debugging is a key part of this process for developers, engineers, and testers. Debugging is complementary to testing, which helps identify how errors have occurred in a program. To simplify the debugging process for testers, LambdaTest, a unified digital testing cloud, has released a new extension called LT Debug. With this tool, testers can debug more efficiently and with less hassle.

Conclusion

In conclusion, mastering ADB commands can greatly enhance your ability to work with Android devices, whether you’re a developer, a tester, or a power user. With ADB, you can interact with the device in various ways and retrieve valuable information, such as the currently running processes, network configuration, disk usage, and more.

In this article, I have covered a variety of ADB commands essential for working with Android devices. I have also provided examples and explanations for each command to help you better understand how they work and how to use them effectively.

Remember, ADB commands can be powerful tools, so use them with caution and ensure you know what you’re doing before you make any changes to your device. With practice and patience, you can become a master of ADB and unlock the full potential of your Android device.

Author Profile Author Profile Author Profile

Author’s Profile

Srinivasan Sekar

Srinivasan Sekar is a Lead Consultant at ThoughtWorks. He loves contributing to Open Source. He is an Appium Member and Contributor to various open-source repositories like Selenium, Webdriver.io, taiko, etc. He worked extensively on testing various Microservices, EvenDriven Apps, and Mobile and Web Applications. He specializes in building automation frameworks. He has spoken at various conferences including SeleniumConf, AppiumConf, AgileIndia, Quest4Quality, BelgradeTestConf, and FOSDEM.

Blogs: 3



linkedintwitter