Android – ADB essential commands

     Android Debug Bridge (ADB) is an application that available along with Android SDK (in platform-tools folder). The ADB is typically accessed through the command-line interface. In Android application environment, ADB plays an important role especially in debug situations. Your Android device should be in debug mode enabled state to get the interaction with ADB services. ADB provides lot of useful services and we can utilize those by running it as ADB commands. Run the ADB commands in the following way in your command prompt (in Windows) or terminal (in Mac OS):

adb <commands>

Following are the prerequisites to run the ADB commands:

1. Android SDK should be available in your system.

2. ANDROID_HOME (of Android SDK) and PATH (of platform-tools and tools) in system environment variables.

3. Make sure that your devices debug mode is enables while running the ADB commands.

Essential ADB Commands

     Here I would like to share some essential adb commands that would be useful in Automation as well as manual testing.

  • To get device list

Purpose: This command helps to get list of all connected devices (real devices as well as virtual devices).

How to use: Run below command in command-line tool

           adb devices

list of devices

  • To install Android Application

Purpose: This command helps to install Android application into the connected devices (real devices as well as virtual devices).

How to use: Run below command in command-line tool. In the command section we need to specify location of the .apk file which stored in your system.

adb install <path of the .apk file>

install

  • To uninstall Android Application

Purpose: This command helps to uninstall Android application from connected devices (real devices as well as virtual devices).

How to use: Run below command in command-line tool. In the command section we need to specify package name of the application which need to uninstall from device.

adb uninstall <package>

unistall

  • To know package name and activity name of Android Application

Purpose: This command helps to get the package name and activity name of current running Android application. This command is very much useful for me for Appium – Android automation. The situation was I already installed the application in device but I need to specify the package name and activity name of the application in Desired Capabilities of Appium configuration. In that situation this command was very much useful for me.

How to use: Run below commands in command-line tool. In this case first we need to switch to the shell after that doing the dumpsys to get the details.

adb shell

dumpsys window windows | grep -E ‘mCurrentFocus|mFocusedApp’

getpackage

  • To capture the screenshot

Purpose: This command helps to capture the current active screen of your application in connected device. Same thing we can do from the Phone itself using the shortcut keys. But in some devices we can’t identity the shortcut keys. So in such a situation this command was helps me to capture the screenshots.

How to use: Run below commands in command-line tool. In this case first we need to create a folder in sdcard. Secondly, run the screencap command to capture the screenshot.

adb shell mkdir /sdcard/screenfolder

adb shell screencap /sdcard/screenfolder/”%time%”.png

You can save this command in a batch (.bat) executable file and run it by double click on it to capture the screenshot instead of writing the whole above commands.

screencapt

  • To record the screen flow in video format

Purpose: This command helps to record the current running flow of any application from connected devices. Sometimes the managers or client may ask the screen-recorded flow of an issue; in such a situation we can do it with help of ADB command.

How to use: Run below commands in command-line tool. In this case first we need to create a folder in sdcard. Secondly, run the screenrecord command to record the screen flow as video.

adb shell mkdir /sdcard/screenrecorded

adb shell screenrecord /sdcard/screenrecorded/test.mp4

You can save this command in a batch (.bat) executable file and run it by double click on it to record the video instead of writing the whole above commands.

video

  • Trace your device logs

Purpose: This command helps to trace the device logs. This command helps me to take the crash logs of the applications.

How to use: Run below commands in command-line tool. Here [<filter-spec>] includes different log priority levels like,

V  – Verbose

D  – Debug

I  –  Info

W – Warn

E  – Error

F  – Fatal

S  – Silent

adb logcat [ <filter-spec> ]

You can save this command in a batch (.bat) executable file and run it by double click on it to trace device logs instead of writing the whole above commands.

  • Run Android Apps over WiFi

Purpose: This command helps to run your Android applications over WiFi connection. I have faced lots of risk in executing my automation test scripts without USB cable. Finally I got a solution to run/debug my scripts in Android devices over WiFi. The concept has already there in Android SDK with help of adb. Now, I can run my test scripts in multiple Android devices without cable.

How to use: We can run the commands in following methods:

Method 1:

1.  Activate debug mode in android

2. Connect to pc via usb

3. Open command prompt type: adb tcpip 5555

4. Disconnect your tablet or smartphone from pc

5. Open command prompt type: adb connect <IPADDRESS> (IPADDRESS is the DHCP/IP address of your tablet or smartphone, which you can find by Wi-Fi -> current connected network)

6. Check for list of devices by using adb devices

Method 2:

1.  Activate debug mode in android

2. Connect to pc via usb

3. Open command prompt type: adb connect <IPADDRESS>:<port>

4. Disconnect your tablet or smartphone from pc.

5. Check for list of devices by using adb devices

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: