Android Debug Bridge (ADB)
Android Debug Bridge is a command-line utility tool, that provides interaction between an Android device or an emulator instance and a PC. It has three components:
- A Client: Client component runs on a PC which can be invoked by issuing adb commands.
- A Server: A background process that runs on a PC, it facilitates the interaction between the client and the adb daemon running on the device/emulator.
- ADB Daemon: A background process that runs on the device/emulator.
ADB is mostly located in the “Platform-tools” sub-folder under the Android SDK installation directory.
Let’s see some of the commonly used ADB commands.
1. Locating connected devices/emulators:
Command: adb devices
Output: 02157df2d2b10c29 device
The first part in the output is the serial number of the device/emulator instance. The second part “device” depicts that the device is connected successfully. Another value for the second part i.e. connection state can be “offline” which means the connection is not set up properly and needs further diagnosis.
If multiple devices are connected then the output will have a separate row for each instance, something like this:
02157df2d2b10c29 device
03257dd4d4a10d21 device
Note: “no device” would be returned in case no device/emulator is connected to the PC.
2. Issuing commands to specific device/emulator:
In case when a single device/emulator is connected then you can simply type adb <command>. The command would be directed to the connected device/emulator.
e.g. adb install test.apk
But what happens when multiple devices/emulators are connected? Use -s switch to direct the command to a particular device/instance.
e.g. First issue adb devices to get the serial ID of all the connected device/emulator instance.
Command : adb devices
Output:
02157df2d2b10c29 device
03257dd4d4a10d21 device
Now to direct the command to a particular device let’s say the first device in the above list, type the following command:
adb -s 02157df2d2b10c29 install test.apk
Result: The above command would install the test app on the device with 02157df2d2b10c29 serial ID.
3. Copying files from Android device to PC:
Copy files from Android device/emulator to PC.
Command: adb pull <Path of the file/folder on Device/emulator> <Path on PC>
e.g. adb pull “storage/emulated/0/DCIM/Screenshots/Screenshot_2023.png” “d:\TestProject”
Where “storage/emulated/0/DCIM/Screenshots/Screenshot_2023.png” is the location of a file on the device and “d:\TestProject” is the target location on the PC.
In order to know the location of files/folder on the device, go to the details page in more menu, it will show you the path to that file/folder.
4. Copying files from PC to Android device:
This case is the opposite of the above case. In this, we will copy from the PC to an Android device/emulator.
Command: adb push <Path of the file/folder on PC> <Path on Device/emulator>
e.g. adb push “d:\TestProject\Report.pdf” “storage/emulated/0/DCIM/Screenshots/
Where “d:\TestProject\Report.pdf” is the file from the PC to be copied to the Screenshots folder in the device/emulator.
5. Installing apps using adb:
ADB lets you install apk files directly onto your device/emulator without requiring any need for the internet or any third-party tool.
Command: adb install <path to the .apk file>
The sample command would look like this:
adb install “d:\TestProject\TestApplication.apk”
Where “TestApplication.apk” is the apk file residing in “d:\TestProject” folder
6. Uninstalling apps using adb:
ADB lets you uninstall installed applications without requiring any need for the internet or any third-party tools.
Command: adb uninstall <name of the package file>
Note: Most of the end users know only the name of the application not the package name for that application. To overcome this, download the “Package Name Viewer” application from the Play Store. This application will tell the package name of the app you want to uninstall.
e.g. adb uninstall “com.xyz.client”
Where “com.xyz.client” is the package name of the app that needs to be uninstalled.
After the uninstallation, the command prompt would display “Success”.
7. Forwarding ports using adb:
Using the forward command of ADB, the request on a particular port can be arbitrarily forwarded to
another port on the device/emulator.
Command: adb forward tcp:4100 tcp:3200
Where 3200 is the port on the device/emulator
8. Directing command to a device when both emulator and device are connected:
Imagine you have both an emulator and a device running on a PC and you want to direct the command only to the device. To do so you can use -s switch and provide the serial number of the device. Easy isn’t it?
An even more easier way to do this without bothering about the serial number is to use -d switch. This switch would direct the command to the connected device.
e.g. adb -d <command to be issued>
A sample command would look like:
adb -d logcat
This command would direct the logcat command to the connected device. It will give an error if more than one device is connected.
9. Directing command to an emulator when both emulator and device are connected:
To direct command only to the emulator when both device and emulator are connected use -e switch
This switch would direct the command to the connected emulator.
e.g. adb -e <command to be issued>
A sample command would be like
adb -e logcat
This command would direct the logcat command to the connected emulator. It will give an error if more than one emulator is connected.
10. Fetching the state of the device:
The connected device/emulator instance can have two states “device” and “offline”. To know the state of the connected device/emulator use the following command:
adb get-state
The output of the command would be either device or offline.
Note: There is however one more state when the device is not connected which is “no device”
11. Wait for the device command:
This command blocks the execution until the device is online (i.e. the connection state should be “device”). This command can be prepended to the other commands.
e.g. of this command is adb wait-for-device logcat
Note: This command only waits for the device to be available (i.e. when the connection state is a device), it does not wait for the device to boot fully so any command that depends on the system to be fully booted is bound to fail.
12. Starting an adb Server:
Use the following commands to start the ADB server:
adb start-server
If the server is already running, this command will return nothing. If the server is stopped then this command would start it again.
13. Stopping an adb Server:
Use the following commands to stop the ADB server:
adb kill-server
This command will stop the server if it is running.