From the previous article we know how to use ADB to interact with devices. Now let’s see few basic commands.
We can install an app using Terminal as well as through your code. Here we will try to install an app with adb commands. Android apps basically would end with ‘.apk‘ extenstion (Application Packages).
To install an app in your device we can use the following command:
adb install [option] <path>
ex: adb install test.apk
This command will let you install an app in your emulator or real device connected to your system. There are few other switches which can be used with adb install
adb install -r test.apk
This will replace the existing application installed in your device
adb install -s test.apk
This will install application on the sdcard present in the device.
To uninstall an app from the device using adb command:
adb uninstall [options] <PACKAGE>
ex: adb uninstall com.test.app
Just like switches for install, there is a switch for uninstall command too
adb uninstall -k com.test.app
This keeps the data and cache directories around after package removal.
Copying files from/to an AVD
1. To copy files from your device to your system. Then use the below command:
adb pull [path to copy from] [local path to copy to]
ex: adb pull /sdcard/pictures/charan.png /home/Desktop/
2. To copy files onto an AVD, you can use the following command:
adb push [local path to copy from] [path to copy to on avd]
ex: adb push test.apk /sdcard
This will copy test.apk to sdcard present in the device. This is one of the ways to install
3. To know what all packages present in your device then try below command:
adb shell pm list packages
This will print all the packages (both enabled and disabled)
There are few switches which can be used to obtain desired output
adb shell pm list packages -e
This will show only enabled packages.
adb shell pm list packages -s
This will print all the system packages.
adb shell pm list packages -3
This will show third party packages.
adb shell pm path com.android.phone
This will show the path of the mentioned package
adb shell pm clear com.test.abc
This will delete all data associated with a package.
4. To impersonate into your device then try the below command:
Now you can use various commands to view the files
do not hide entries starting with
print size of each file, in blocks
list numeric UIDs and GIDs
changing the directory to system directory
mkdir -m 777 /sdcard/tmp
Creating a directory and -m is to set permissions
rm -f /sdcard/test.txt
This will force remove without prompt
rm -d /sdcard/tmp
This will remove directory, even if it is a non-empty directory
This will create empty file or change file timestamps
adb shell screencap /sdcard/screen.png
This will take a screenshot of a device display.
This will display top CPU processes
This will terminate the adb server process
This will start the adb server
Mounting the system partition as Read Write to transfer files to your /system partition:
To change permissions from Read-only to Read-Write type command:
mount -o rw,remount /system
What is a logcat?
A logcat is a report from the Android logging system, which takes place in the background, the whole time your phone is on. It starts the moment you switch on the phone, and continues till you shut it down completely. This log is extremely useful for finding out what went wrong with your system. It is useful to find out why your phone is having force closes.
To get this logs, type command:
If you would like to store the logs in a text file then type
adb logcat > logs.txt /
adb shell screenrecord /sdcard/NotAbleToLogin.mp4
start a recording session of the connected device
That’s it, now you’re familiar with adb commands to play around while automation.