Skip to main content

ADB Commands

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:
  1. How to print the adb version number
  2. How to print a list of all attached devices/emulator instances to adb server.
  3. How to install an apk file to an emulator/read android device
  4. How to capture logs of android device
  5. How to start and terminate adb server
  6. How to starts a remote shell in the target emulator/device instance.
  7. How to push a file (.txt/.pdf/.jpg/.apk ) to a device/emulator
  8. How to pull a file (.txt/.pdf/.jpg/.apk ) from a device/emulator to local machine
  9. 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 hereThe location of adb tool is Android >> android-sdk >> platform-tools >> adb:
adb_Command_0

Most Commonly used ADB Commands


How to print the adb version number?

Command Syntax: adb version
This command prints the adb version number
adb_Command_1


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.
adb_Command_2
This displays the information in the format:
Serial Number   State
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
  • 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
adb_Command_3
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.
Once done, notice that the Amazon app will display in the Emulator screen.
adb_Command_4
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>
adb_Command_5
A log file will be created at the given location.
adb_Command_6


How to start and terminate adb server?

Command Syntax to Terminate Server: adb kill-server
This command terminates the adb server.
adb_Command_7
Command Syntax to Start Server: adb start-server
This command checks whether the adb server is running and starts it, if not running.
adb_Command_8


How to starts a remote shell in the target emulator/device instance?

Command Syntax: adb shell
adb_Command_9
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 Syntaxgetprop ro.product.model
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
Command Syntax: getprop ro.build.version.sdk
This command get’s the device api version i.e. 19,20,21,22
adb_Command_10

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.
adb_Command_11
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”
adb_Command_12

Command Syntax: pm uninstall <app Package Name>
This can be used to uninstall an app using pm tool as per above mentioned command.
adb_Command_13
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.
adb_Command_14


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
  1. ls -l | grep “storage” – List files/directories with name storage
  2. cd storage – Changed current directory to ‘storage’
  3. ls -l | grep “sdcard” – List files/directories with name sdcard
  4. cd sdcard1  Changed current directory to ‘sdcard1’ = storage/sdcard1
  5. ls -l  – List all the files/directories in sdcard1
  6. pwd – See current path, this is remote path
adb_Command_15
adb_Command_16
Now the command to push the Amazon apk fill be: adb push C:\Apps\Amazon\in.amazon.mshop.android.shopping.apk /storage/sdcard1
adb_Command_17

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>
Direct an adb command a specific emulator/device instance whose deviceName is passed.
adb_Command_18

Command Syntax: adb –d <command>
Directs adb command to the only attached USB device.
Command Syntax: adb –e <command>
Directs adb command to the only attached Emulator.
adb_Command_19
Note: This command returns an error if more than one emulator instance is running.

Comments

Popular posts from this blog

JMeter Exceeded Maximum Number of Redirects Error Solution

While running performance test, JMeter allows maximum 5 redirects by default. However, if your system demands more than 5 redirects, it may result in JMeter exceeded maximum number of redirects error. In this post, we have listed down steps to overcome this error. Actual error in JMeter: Response code: “Non HTTP response code: java.io.IOException” Response message: “Non HTTP response message: Exceeded maximum number of redirects: 5” This error is noticed because  JMeter  allows maximum 5 redirects by default and your system may be using more than 5 redirects. You need to increase this count to more than 5 in jmeter.properties file. Follow below steps to achieve this. Navigate to /bin directory of your JMeter installation. Locate jmeter.properties file and open it in any editor. Search for “httpsampler.max_redirects” property in opened file. Uncomment the above property by removing # before it. Change to value to more than 5 Eg. 20. Save the file and restart JMeter. If

SSO with SAML login scenario in JMeter

SAML(Security Assertion Markup Language) is increasingly being used to perform single sign-on(SSO) operations. As WikiPedia puts it, SAML is an XML-based open standard data format for exchanging authentication and authorization data between parties, in particular, between an identity provider and a service provider. With the rise in use of SAML in web applications, we may need to handle this in JMeter. This step-by-step tutorial shows SAML JMeter scenario to perform login operation. First request from JMeter is a GET request to fetch Login page. We need to fetch two values ‘SAMLRequest’ and ‘RelayState’ from the Login page response data. We can do this by using  Regular Expression Extractor . These two values need to be sent in POST request to service provider. Refer below image to see how to do this. We will get an HTML login page as a response to the request sent in 1st step. We need to fetch values of some hidden elements to pass it in the next request. We can do this b

A Tutorial to Send Email using JMeter

Sending email is a mundane activity in any professional’s life. It’s a common medium for communication nowadays. Therefore performance testing of email server is not only important but necessary for an organization. JMeter can be helpful to perform load testing in such scenarios. In this tutorial, we will see how JMeter can be used to send email. We will use SMTP Sampler of JMeter to send an email. JavaMail API is needed to enable email functionality in JMeter. Download it from  here  and paste the jar in JMeter’s lib folder. Now, perform below steps to configure SMTP Sampler. Add a new Thread Group under Test Plan. Right click on Thread Group and select Add–>Sampler–>SMTP Sampler. We need to populate SMTP server’s details in this sampler. We will use GMail for sending an email. For this, enter these values in SMTP Sampler fields. Server: smtp.googlemail.com, Port: 587. Provide values in Email Address From and To fields of Mail Settings section to specify sender and reci