How to use the command adb (with examples)
The command adb
stands for Android Debug Bridge and allows users to communicate with an Android emulator instance or connected Android devices. It has various subcommands, such as adb shell
, which have their own usage documentation. The command is widely used by developers and testers for various tasks related to Android app testing and debugging.
Use case 1: Check whether the adb server process is running and start it
Code:
adb start-server
Motivation: When working with Android devices or emulators, it is important to ensure that the adb server is running. This command helps to start the adb server process if it is not already running.
Explanation: The start-server
subcommand is used to start the adb server process. It checks whether the server is already running and if not, starts it.
Example output:
* daemon not running; starting now at tcp:5037
* daemon started successfully
Use case 2: Terminate the adb server process
Code:
adb kill-server
Motivation: Sometimes, it may be necessary to stop the adb server process, for example, to release system resources or to restart it for any reason. This command helps to terminate the adb server process.
Explanation: The kill-server
subcommand is used to terminate the adb server process. It stops the server if it is running.
Example output: None
Use case 3: Start a remote shell in the target emulator/device instance
Code:
adb shell
Motivation: To interact with the shell of an Android device or emulator, the adb shell
command is used. It provides a remote shell session for executing commands and accessing device-specific files and services.
Explanation: The shell
subcommand is used to start a remote shell in the target emulator/device instance. It opens a terminal-like interface where users can execute commands on the device.
Example output:
$ adb shell
generic_x86:/ $
Use case 4: Push an Android application to an emulator/device
Code:
adb install -r path/to/file.apk
Motivation: When developing or testing Android applications, it is often required to install the application package (.apk) onto the emulator/device. This command helps to push the Android application to the emulator/device.
Explanation: The install
subcommand is used to install an Android application package onto the target emulator/device. The -r
option replaces the existing application if it is already installed.
Example output:
Performing Streamed Install
Success
Use case 5: Copy a file/directory from the target device
Code:
adb pull path/to/device_file_or_directory path/to/local_destination_directory
Motivation: Sometimes, it is necessary to retrieve files or directories from the target device for analysis or debugging purposes. This command helps to copy files or directories from the target device to the local system.
Explanation: The pull
subcommand is used to copy a file or directory from the target device to the local system. Users need to provide the path of the file or directory on the target device and the path of the destination directory on the local system.
Example output:
/path/to/device_file_or_directory: 1 file pulled. 0 files skipped.
Use case 6: Copy a file/directory to the target device
Code:
adb push path/to/local_file_or_directory path/to/device_destination_directory
Motivation: To transfer files or directories from the local system to the target device, the adb push
command is used. This can be useful for testing specific files on the device or transferring data for use in the application.
Explanation: The push
subcommand is used to copy a file or directory from the local system to the target device. Users need to provide the path of the file or directory on the local system and the path of the destination directory on the target device.
Example output:
/path/to/local_file_or_directory: 1 file pushed. 0 files skipped.
Use case 7: Get a list of connected devices
Code:
adb devices
Motivation: To validate the connectivity with Android devices or emulators, it is useful to obtain a list of connected devices. This command helps to check the status and availability of devices for debugging and testing.
Explanation: The devices
subcommand is used to get a list of connected devices. It displays the device ID and status of each connected device.
Example output:
List of devices attached
emulator-5554 device
Conclusion:
In this article, we have explored various use cases of the adb
command. We have seen how to start or terminate the adb server process, start a remote shell on the target emulator/device, install an Android application, copy files/directories to/from the target device, and get a list of connected devices. These use cases showcase the versatility and importance of the adb
command in Android app development and testing.