In this tutorial we will learn what is an ADB and how we can issue adb commands from command line or from a testing script. Following will be covered in this chapter:
- How to print the adb version number
- How to print a list of all attached devices/emulator instances to adb server.
- How to install an apk file to an emulator/read android device
- How to capture logs of android device
- How to start and terminate adb server
- How to starts a remote shell in the target emulator/device instance.
- How to push a file (.txt/.pdf/.jpg/.apk ) to a device/emulator
- How to pull a file (.txt/.pdf/.jpg/.apk ) from a device/emulator to local machine
- How to redirect adb command to a specific device.
ADB : ADB stands for Android Debug Bridge. It is a command line tool that lets us communicate with an Android device or an Emulator. It is a client-server program that includes three components:
- A client, which runs on our machine. We can invoke a client by issuing an adb command.
- A daemon, which runs as a background process on each emulator or device instance. It by default runs on port number 5037
- A server, which runs as a background process on our machine. It manages communication between the client and the adb daemon running on an emulator or device.
This tool can be found in Android ADB bundle which we can download from here. The location of adb tool is Android >> android-sdk >> platform-tools >> adb:
Note: Steps to Install Android SDK / ADB on Windows.
Most Commonly used ADB Commands
How to print the adb version number?
Command Syntax: adb version
This command prints the adb version number
This command prints the adb version number
How to print a list of all attached devices/emulator instances to adb server?
Command Syntax: adb devices
This command prints status information of all the attached devices.
This command prints status information of all the attached devices.
This displays the information in the format:
Serial Number State
6a2ed0b device
6a2ed0b device
Serial Number: A string created by adb to uniquely identify an emulator/device
State: The connection state of the instance. It can be offline, device, no device
State: The connection state of the instance. It can be offline, device, no device
- Offline : The device is not connected or not responding
- device : The device is connected to adb server
- no device : There is no device connected to adb server
Take a look at the above screenshot, adb device command is fired three times and the result is different at each time.
First Attempt: It displayed two devices attached, out of which one is real device and another one is Emulator running on the machine. To learn more about Emulators or setting up emulators for mobile testing please go through How to Create an Android Virtual Device using Android Emulator.
Second Attempt: Again it displayed two device but this time the Emulator is showing as Offline state, as I switched it off.
Third Attempt: In the last attempt it is not displaying anything, as I detached the attached device and shutdown the running Emulator on the machine.
How to install an apk file to an emulator/read android device?
Command Syntax: adb install <local_path_to_apk>
This command will install the apk file to attached device / emulator
Note: Emulator should be up & running before firing adb install command. In the above screen shot emulator-5554 is displayed with device status, it means it is up & running.
Note: I got one Amazon APK file placed at C:\Apps\Amazon.
Note: I got one Amazon APK file placed at C:\Apps\Amazon.
Once done, notice that the Amazon app will display in the Emulator screen.
To learn more about Emulators or setting up emulators for mobile testing please go through How to Create an Android Virtual Device using Android Emulator.
How to capture logs of android device?
Command Syntax: adb logcat
This command will print the logs data to the screen for the purposes of bug reporting. We can also store these logs into local file using below command:
Command Syntax: adb logcat > <local_path_to_text_file>
This command will print the logs data to the screen for the purposes of bug reporting. We can also store these logs into local file using below command:
Command Syntax: adb logcat > <local_path_to_text_file>
A log file will be created at the given location.
How to start and terminate adb server?
Command Syntax to Terminate Server: adb kill-server
This command terminates the adb server.
This command terminates the adb server.
Command Syntax to Start Server: adb start-server
This command checks whether the adb server is running and starts it, if not running.
This command checks whether the adb server is running and starts it, if not running.
How to starts a remote shell in the target emulator/device instance?
Command Syntax: adb shell
ADB provides a Unix shell that can be used to run a variety of commands on an emulator or connected device. The command binaries are stored in the file system of the emulator or device. The following commands can be executed on shell:
Command Syntax: getprop ro.product.model
This command get’s the attached device model no
This command get’s the attached device model no
Command Syntax: getprop ro.build.version.release
This command get’s the android build release version i.e. 4.4.2/4.4.4
This command get’s the android build release version i.e. 4.4.2/4.4.4
Command Syntax: getprop ro.build.version.sdk
This command get’s the device api version i.e. 19,20,21,22
This command get’s the device api version i.e. 19,20,21,22
Command Syntax: pm list packages –f
Using pm (package manager tool) we can perform actions and queries on app packages installed on device. E.g. we are listing all packages with path of their associated file.
Using pm (package manager tool) we can perform actions and queries on app packages installed on device. E.g. we are listing all packages with path of their associated file.
Note: Here pm is a tool and list package is a command and –f is an option to list path of file associated with package.
The above command can also be utilized to print the package of installed apps. E.g. Android browser’s app package can also be printed with this which is installed on the device.
Command Syntax: pm list packages –f | grep “chrome”
Command Syntax: pm list packages –f | grep “mozilla”
Command Syntax: pm list packages –f | grep “mozilla”
Command Syntax: pm uninstall <app Package Name>
This can be used to uninstall an app using pm tool as per above mentioned command.
This can be used to uninstall an app using pm tool as per above mentioned command.
Step 1: To get the package for Amazon app
Step 2: Uninstall Amazon Package
Step 3: Again to get the package for Amazon app, this time it will not displayed anything, as Amazon app is now uninstalled from the device, please refer the below screenshot.
Step 2: Uninstall Amazon Package
Step 3: Again to get the package for Amazon app, this time it will not displayed anything, as Amazon app is now uninstalled from the device, please refer the below screenshot.
How to push a file (.txt/.pdf/.jpg/.apk ) to a device/emulator?
Command Syntax: adb push <local_Path_of_file> <remote_Path_where_file_to_be_copied>
Let’s see how to do this. First it is required to start a remote shell in the target emulator/device instance and find remote path to copy the file. E.g. To copy the file to sdcard
- ls -l | grep “storage” – List files/directories with name storage
- cd storage – Changed current directory to ‘storage’
- ls -l | grep “sdcard” – List files/directories with name sdcard
- cd sdcard1 – Changed current directory to ‘sdcard1’ = storage/sdcard1
- ls -l – List all the files/directories in sdcard1
- pwd – See current path, this is remote path
Now the command to push the Amazon apk fill be: adb push C:\Apps\Amazon\in.amazon.mshop.android.shopping.apk /storage/sdcard1
How to pull a file (.txt/.pdf/.jpg/.apk ) from a device/emulator to local machine?
Command Syntax: adb pull <remote_Path_of_file_on_device> <local_Path_where_file_to_be_copied >
How to redirect adb command to a specific device?
If multiple emulators are running and/or multiple devices are attached and if perform adb shell command. This command returns an error if more than one devices are attached. Refer the below screenshot for Step 2.
-d, -e, or -s options can be used to specify the target device to which the command should be directed.
Command Syntax: adb -s <deviceName> <command>
Command Syntax: adb -s <deviceName> <command>
Direct an adb command a specific emulator/device instance whose deviceName is passed.
Command Syntax: adb –d <command>
Directs adb command to the only attached USB device.
Directs adb command to the only attached USB device.
Command Syntax: adb –e <command>
Directs adb command to the only attached Emulator.
Directs adb command to the only attached Emulator.
Note: This command returns an error if more than one emulator instance is running.
Comments
Post a Comment