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.
adb devicesOutput:
List of devices attached
RQCX2029BAE device
192.168.0.6:5555 deviceadb connect <ip_address>
Connects to a device over Wi-Fi. You need to know the device's IP address.
adb connect 192.168.0.10adb disconnect <ip_address>
Disconnects from a device that was connected over Wi-Fi.
adb disconnect 192.168.0.10adb shell
Starts a remote shell on the target device. You can run various Linux commands here.
adb shelladb install <path_to_apk>
Installs an APK file on the device.
adb install myapp.apkadb uninstall <package_name>
Uninstalls an app from the device.
adb uninstall com.example.myappadb push <local_path> <remote_path>
Copies a file or directory from your computer to the device.
adb push local_file.txt /sdcard/remote_file.txtadb pull <remote_path> <local_path>
Copies a file or directory from the device to your computer.
adb pull /sdcard/remote_file.txt local_file.txtTroubleshooting: 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.
adb kill-serverAfter 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.
adb logcat > logs.txtadb logcat > logs.txtThis 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.
Example 1: Find logs related to a specific API endpoint
adb logcat | Select-String -Pattern "https://myapi.service.com" > logs.txtadb logcat | grep "https://myapi.service.com" > logs.txtExample 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.
adb logcat | Select-String -Pattern "AgileContent", "AgileTV" > logs.txtadb logcat | grep -E "AgileContent|AgileTV" > logs.txtAfter 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).