Skip to content

Android Debug Bridge (ADB)

Introduction

Now that the Android infrastructure installation is complete, and before we move on to configuring devices for Android automation, let’s first take a moment to learn about the Android Debug Bridge (ADB).

ADB is a versatile command-line tool that enables communication between your development machine and an Android device. It supports a wide range of operations, such as installing and debugging applications, as well as providing access to a Unix shell where you can run various commands directly on the device.

A Quick Note

Don’t worry about executing all ADB commands right now. This section serves as a reference so you can get familiar with the tool. In the upcoming modules, we’ll use these commands in real scenarios while working with actual devices connected to your computer.

Common Commands

Here are some of the most commonly used ADB commands.

adb devices

Lists all connected devices and their status. This is the first command you should use to verify that your device is correctly connected.

shell
adb devices

Output:

text
List of devices attached
RQCX2029BAE     device
192.168.0.6:5555  device

adb connect <ip_address>

Connects to a device over Wi-Fi. You need to know the device's IP address.

shell
adb connect 192.168.0.10

adb disconnect <ip_address>

Disconnects from a device that was connected over Wi-Fi.

shell
adb disconnect 192.168.0.10

adb shell

Starts a remote shell on the target device. You can run various Linux commands here.

shell
adb shell

adb install <path_to_apk>

Installs an APK file on the device.

shell
adb install myapp.apk

adb uninstall <package_name>

Uninstalls an app from the device.

shell
adb uninstall com.example.myapp

adb push <local_path> <remote_path>

Copies a file or directory from your computer to the device.

shell
adb push local_file.txt /sdcard/remote_file.txt

adb pull <remote_path> <local_path>

Copies a file or directory from the device to your computer.

shell
adb pull /sdcard/remote_file.txt local_file.txt

Troubleshooting: adb kill-server

Sometimes, especially on Windows, the ADB server process can get stuck. This might happen if you disconnect a device's USB cable, but the system doesn't release the connection properly. This can prevent you from connecting to other devices or even reconnecting to the same one.

If you suspect the ADB server is unresponsive, you can force it to stop using the kill-server command.

shell
adb kill-server

After running this command, the ADB server will be shut down. The next time you run an adb command (like adb devices), the server will automatically restart. This usually resolves connection issues caused by a stuck process.

Capturing Logs with logcat

adb logcat is a powerful command that displays real-time logs from an Android device. This is incredibly useful for debugging applications, monitoring system events, and understanding what your app is doing behind the scenes.

Saving Logs to a File

To save the log output to a file, you can redirect the command's output.

powershell
adb logcat > logs.txt
shell
adb logcat > logs.txt

This will create a file named logs.txt in your current directory containing the device logs. Press Ctrl + C to stop capturing.

Filtering Log Output

The log output can be very verbose. You can filter it to find specific information.

powershell
adb logcat | Select-String -Pattern "https://myapi.service.com" > logs.txt
shell
adb logcat | grep "https://myapi.service.com" > logs.txt

Example 2: Find the Activity name for an application

This is useful for automation, as you often need the package name and the main activity to launch an app.

powershell
adb logcat | Select-String -Pattern "AgileContent", "AgileTV" > logs.txt
shell
adb logcat | grep -E "AgileContent|AgileTV" > logs.txt

After running the command, open logs.txt and look for lines that mention an Activity being started, often in a format like ActivityManager: START u0 {act=... cmp=com.package.name/.ActivityName ...}. This will help you identify the full component name (package/activity).