Unleash the power of adb commands

Bipin Kumar Chaurasia
BYJU’S Exam Prep Engineering
3 min readAug 31, 2018

While testing of android apps, we usually come across following similar mundane task involving manual efforts, which can be avoided with usage of adb commands with a bit of python scripting.

So, here I’m sharing the common commands that will simplify your day to day tasks.

To copy a file or directory and its sub-directories to the device, do the following,

adb push local remote
adb push ~/Desktop/Screenshots/image.png /sdcard/

To copy a file or directory and its sub-directories from the device, do the following:

adb pull remote local
adb pull /sdcard/image.png ~/Desktop/Screenshots/

ADB commands to grant permissions for android app,

adb shell pm grant package_name permission
adb shell pm grant co.gradeup.android android.permission.CAMERA

ADB command to revoke permissions for android app,

adb shell pm revoke package_name permission
adb shell pm revoke co.gradeup.android android.permission.CAMERA

Following permissions are used on different app as follows,
CAMERA, READ_CONTACTS, GET_ACCOUNTS, ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION, READ_PHONE_NUMBERS, RECEIVE_SMS, READ_SMS, READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE, etc.

The screencap command is a shell utility for taking a screenshot of a device display.

adb shell screencap -p /sdcard/image.png

The screenrecord command is a shell utility for recording the display of devices. The utility records screen activity to an MPEG-4 file. Stop the screen recording by pressing Control + C, otherwise the recording stops automatically at three minutes or the time limit set by — time-limit.

adb shell screenrecord /sdcard/video.mp4

The following command with parameter -r is used to re-install the app and keep its data on the device. Executing this command is simulating the app update process from an older to the latest version of your app.

adb -r install co.gradeup.android

Activity Manager (am) command to start an activity,

adb shell am start -n co.gradeup.android/com.udofy.ui.activity.AppLauncherActivity

Activity Manager (am) command to force stop everything associated with package name,

adb shell am force-stop co.gradeup.android

Activity Manager (am) command to test deeplinks for android app,

adb shell am start -W -a android.intent.action.VIEW -d "http://grdp.co/pimk9ddo5" co.gradeup.android

Following are the hacks which you can use for simplifying your efforts,

Connect Your Devices via Wi-Fi (Wireless) to your PC

Please make sure to configure “adb” on your machine prior to following below steps,

Type "adb devices" and hit enter to see connected device UDID.
Type "adb tcpip 5657" to assign a port to your device. (If you want, you can give another unassigned port number to your device.)
Go to your device's settings and find its IP address. (LG G4's assigned IP is 192.168.18.61).
Type "adb connect 192.168.18.61:5657" to connect your device via wireless (Wi-Fi).
Type again "adb devices" and see both your device UDID via USB and ip:port pair via Wireless.
Then, unplug your device from your PC's USB port.

Python script to take screenshot app UI issue and then pull into your Screenshots folder at Desktop,

import os
from time import sleep
import glob
import time
millis = int(round(time.time() * 1000))
os.chdir(“Desktop/Screenshots”)
print os.system(“rm -rf *”)
print os.system(“adb shell screencap -p /sdcard/”+str(millis)+”.png”)
sleep(2)
print os.system(“adb pull /sdcard/”+str(millis)+”.png”)

Python script to uninstall older version of app and then install latest apk file as available in your downloads folder,

import os
import glob
os.chdir("Downloads")
print os.system("adb devices")
print os.system("adb uninstall co.gradeup.android")
print os.system("adb install -t Gradeup.apk")
print os.system("adb shell am start -n co.gradeup.android/.view.activity.LauncherActivity")

That’s it folks!

--

--