Basic ADB commands


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

switch -r
adb install -r test.apk
This will replace the existing application installed in your device

switch -s
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:
adb shell

ADB_Shell01
Now you can use various commands to view the files
ls -a
do not hide entries starting with

ls -s
print size of each file, in blocks

ls -n
list numeric UIDs and GIDs

cd /system
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

touch /sdcard/tmp/test.txt
This will create empty file or change file timestamps

adb shell screencap /sdcard/screen.png
This will take a screenshot of a device display.

top
This will display top CPU processes

adb kill-server
This will terminate the adb server process
adb start-server
This will start the adb server

Mounting the system partition as Read Write to transfer files to your /system partition:

method1:
adb remount[/system]

method2:
adb shell
mount[/system]

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:
adb logcat[/code]
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.

Advertisements

One thought on “Basic ADB commands

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s