DEV Community

Attilio Carotenuto
Attilio Carotenuto

Posted on • Updated on

Android Debugging using ADB

ADB, or Android Debug Bridge, is a set of tools that lets you manage your Android devices and emulators, facilitating debugging, installing and handling APKs. It is part of the Android SDK Platform Tools package.

The easiest way to set it up is by using the SDK Manager within Android Studio. You can also install it via the sdkmanager command line tool (https://developer.android.com/tools/sdkmanager).

Alternatively, you can download it from here: https://developer.android.com/tools/releases/platform-tools
In this case, ensure you have this added to your PATH environment variable, so you can easily invoke it from the terminal.

Now that you have the tools installed, you need to enable USB debugging on your device. To do so, you first need to unlock the hidden Developers options menu (https://developer.android.com/studio/debug/dev-options#enable). You can then connect your device to your development machine via USB.

While the tool contains a long list of commands, I'll cover the most useful when debugging Android games or applications.

adb devices

This command lets you retrieve the list of devices currently connected to adb. It can be either via cable or wireless, and it will include both real devices and emulators.

If you include the -l modifier, it will also print a description of the devices, including the model and the transport_id.

By default, if you have just one device connected, adb will direct commands to that device. If you have more than one, you'll need to use one of these modifiers for all other commands:
-d Target the USB connected device (if just one is available)
-e Target the TCP/IP connected device (if just one is available)
-s serial Target the device with the serial number supplied.

adb connect / adb disconnect

These commands let you respectively connect to, and disconnect, a device via TCP/IP.

The process changes depending on the Android version your device is using. If you are using Android 10 or lower, you'll first need to connect your device with a USB cable and run the tcpip command, to set your device to listen for a connection on port 5555:

adb tcpip 5555
Enter fullscreen mode Exit fullscreen mode

This is not required on newer versions. Instead, you can enable Wireless Debug from the Developers menu, as shown below:

Image description

When enabled, it will also show the IP and port you need to connect to. You can also find it in the Network settings, if you're using an older device.

Finally, run this command to connect (port will be 5555 on older devices):

adb connect device_ip_address:port
Enter fullscreen mode Exit fullscreen mode

When you're done debugging, you can then disconnect using adb disconnect. If you don't pass any parameters, it will disconnect all devices connected via TCP/IP, otherwise you can specify the IP address (and port) of the device you want to disconnect.

adb logcat

Possibly the most common tool within ADB, logcat will print log messages from your device into your terminal. This by default includes application logs, system logs and crashes.

Logs will be streamed continuously, including whatever was on the buffer before you run the command.
If you'd like to clear the buffer to reduce the amount of logs, you can use the -c modifier. This command for example will flush the buffer and store it in a text file:

adb logcat -c > logs.txt
Enter fullscreen mode Exit fullscreen mode

If you want to stop logcat, you can just type CTLR+C.

To reduce noise and show only logs from your application, you can add filter expressions, pairing tags and their desired log priority, for example:

adb logcat MyGameActivity:W *:S
Enter fullscreen mode Exit fullscreen mode

This will print logs from your game, Warning or higher priority. The second filter is used to suppress logs from any other source, effectively setting them to silent.

Using filters might give you an error, such as "matches not found", depending on the OS and terminal you're using. If that's the case, you likely need to enclose it in quotes:

adb logcat "MyGameActivity:W *:S"
Enter fullscreen mode Exit fullscreen mode

For convenience, here is the list of priorities:

V: Verbose
D: Debug
I: Info
W: Warning
E: Error
F: Fatal
S: Silent (no logs printed)

adb bugreport

This command lets you generate a bug report containing device logs, stack traces, dumpstate, dumpsys and other diagnostic information, all packed in a zip file.

By default it will be saved in the current directory, but you can also define the output directory:

adb bugreport path/to/save/report 
Enter fullscreen mode Exit fullscreen mode

Generating a report will take around a minute.

adb install

This lets you install a package on your device, for example:

adb install path_to_app.apk
Enter fullscreen mode Exit fullscreen mode

If you want to enable all required runtime permission right after install, you can pass the -g modifier. This is particularly useful when doing automated testing.

adb uninstall

Similar to the install command, this lets you remove a package from your device.

adb uninstall com.example.test
Enter fullscreen mode Exit fullscreen mode

You can choose to keep data and cache directories by using the -k modifier.

adb pull / adb push

These two commands will let you transfer files and directories from and to your device.

For example, if you want to copy a file from your android device's SD card to your computer, use:

adb pull /sdcard/device_file_path local_destination_path 
Enter fullscreen mode Exit fullscreen mode

In the same way, if you want to transfer a file from your computer to your device's SD card:

adb push local_file_path /sdcard/device_destination path
Enter fullscreen mode Exit fullscreen mode

adb reboot

This lets you reboot your connected device.

You can also reboot in recovery mode by passing an extra parameter

adb reboot recovery
Enter fullscreen mode Exit fullscreen mode

adb shell

This open a shell in your android device or emulator, allowing you to run custom commands on it and invoke other system utilities.

For example, you can invoke getpropt to get the current Android version by doing:

adb shell getprop ro.build.version.release
Enter fullscreen mode Exit fullscreen mode

Or the API level:

adb shell getprop ro.build.version.sdk
Enter fullscreen mode Exit fullscreen mode

Top comments (0)