Android Debug Bridge (ADB) : Definitive Guide for Developers

Rashik
7 min readAug 20, 2024

--

Imagine you’re deep in the trenches of Android development. You’ve been chasing down an elusive bug for hours, or perhaps you’re a QA tester trying to simulate a complex scenario like an incoming call without access to a real network. The challenges pile up, and frustration mounts. But then, you remember your secret weapon: Android Debug Bridge, better known as ADB.

In this article, we’ll embark on a journey through ADB, revealing the most crucial commands you need to master Android debugging and testing. This article will be your guide, empowering you to solve problems faster and more efficiently.

What is ADB?

ADB, short for Android Debug Bridge, is more than just a command-line tool. It’s a lifeline that connects you to your Android device, enabling you to execute a wide range of actions — everything from installing apps to digging into system logs. If Android were a vast, complex machine, ADB would be the key that lets you peek inside, understand how it ticks, and even tweak its settings.

The Three Pillars of ADB

ADB operates through three core components:

  • Client: The part you interact with on your development machine.
  • Daemon: The worker running on the device, executing your commands.
  • Server: The manager that oversees communication between the client and daemon.

Together, these components create a bridge that spans from your keyboard to the very core of Android’s architecture.

Unlocking the Power of ADB Logcat

When something goes wrong in your app, logs are often the first place you look. ADB Logcat is the tool that lets you tap into those logs, filtering through the noise to find the signals that matter.

  • adb logcat: View live logs streaming from your device.
  • adb logcat -b main: Focus on application logs.
  • adb logcat -b system: Delve into messages from the Android OS itself.
  • adb logcat -b crash: Isolate crash logs to diagnose those dreaded app crashes.
  • adb logcat -b default: Get a comprehensive view by combining main, system, and crash logs.
  • adb logcat -b event: Monitor system events.
  • adb logcat -b kernel: Dive deep into kernel logs.
  • adb logcat -b all: See it all—every log, every message.
  • adb logcat -d: Dump logs for later review.
  • adb logcat -c: Clear the current log buffer, wiping the slate clean.
  • adb logcat > log.txt: Capture logs to a file for detailed analysis later.

Pro Tip: Saving Logs for Future Reference

  • adb logcat -b all > logcat.txt: Save all logs as a text file, perfect for later review or sharing with a colleague.

ADB for Debugging: The Basic commands

Whether you’re trying to connect to a device or reboot it, ADB provides the commands you need to stay in control.

  • adb devices: List all connected devices—know exactly what you’re working with.
  • adb root: Elevate to superuser mode for more powerful commands.
  • adb reboot: Restart your device—sometimes a fresh start is all you need.
  • adb reboot -p: Power off the device gracefully.
  • adb reboot bootloader: Enter bootloader mode for tasks like flashing a new system image.
  • adb reboot recovery: Switch to recovery mode, essential for system repairs.
  • adb install <apk_file>: Quickly install an APK directly onto your device.
  • adb install -r <apk_file>: Reinstall an app without losing its data.
  • adb uninstall <app_package_name>: Cleanly remove an app.
  • adb uninstall -k <package_name>: Uninstall an app but keep its data intact.
  • adb start-server: Start the ADB server to begin your debugging session.
  • adb kill-server: Stop the ADB server when you're done.

ADB Settings:

Ever wished you could tweak your Android device’s settings directly from your command line? With ADB, you can. Adjusting system settings on-the-fly becomes second nature.

  • adb shell settings list system: View all system settings.
  • adb shell settings list secure: List secure settings that affect device security.
  • adb shell settings list global: See global settings that apply across the device.
  • adb shell settings get <namespace> <setting_name>: Retrieve the current value of a specific setting.
  • adb shell settings put <namespace> <setting_name> <value>: Change a setting to suit your needs.

Example: Changing Screen Brightness

adb shell settings put system screen_brightness 100

This command sets the screen brightness to 80 percent.

Transferring Files: Seamless Interaction Between Device and Host

Transferring files between your development machine and your device is a breeze with ADB.

  • adb push <local_file> <remote_path>: Copy a file from your machine to the device.
  • adb pull <remote_file> <local_path>: Retrieve a file from the device to your local machine.

Android Device Properties: Understanding Your Device Inside and Out

ADB lets you probe your device’s properties, giving you insights into its configuration and capabilities.

  • adb shell getprop: List all device properties—a treasure trove of information.
  • adb shell getprop <property_name>: Fetch the value of a specific property.
  • adb shell setprop <property_name> <value>: Modify a property to tweak your device’s behavior.

SELinux Commands:

SELinux is a security module in Android that enforces access control policies. ADB commands allow you to query and adjust its status.

  • adb shell getenforce: Check whether SELinux is disabled, enforcing, or permissive.
  • adb shell setenforce <0|1>: Switch SELinux between permissive (0) and enforcing (1) modes.

Exploring Directories: Navigating Your Device’s Filesystem

Like a digital explorer, you can use ADB to traverse your device’s directories and examine its contents.

  • adb shell ls: List all files and directories.
  • adb shell ls -s: Display the size of each file—handy for managing storage.

Network Commands:

Networking issues can bring development to a halt. ADB provides tools to help you diagnose and manage network connections.

  • adb shell netstat: Show active network connections.
  • adb shell netstat -l -t: Focus on active internet connections.
  • adb forward <local> <remote>: Forward a local port to a port on the device.
  • adb reverse <remote> <local>: Reverse port forwarding, sending data from the device back to your machine.
  • adb shell ifconfig: Display network interfaces and IP addresses.
  • adb shell ip addr show: Get detailed network information.

Dumpsys Commands:

Dumpsys is your go-to tool for detailed diagnostics, offering insights into system services and their statuses.

  • adb shell dumpsys -l: List all system services.
  • adb shell dumpsys: Get detailed data on all system services.
  • adb shell dumpsys battery: Examine battery information—vital for power management.
  • adb shell dumpsys battery set level 20: Set the battery level to 20%.
  • adb shell dumpsys battery set ac 1: Simulate charging mode.

Service Management:

ADB lets you take control of Android’s core services, offering commands to list, start, or stop them as needed.

  • adb shell service list: List all available services on your device.

Core Bug Report Commands: Uncovering the Root of Problems

When something goes wrong, bug reports are essential for uncovering the cause. ADB provides powerful commands to generate these reports.

  • adb shell bugreport: Generate a full bug report—your blueprint for debugging.
  • adb shell procrank: Get a detailed breakdown of memory usage.
  • adb shell cat /proc/meminfo: Access raw memory information.
  • adb shell dumpsys meminfo: Get memory info specific to a process.

Activity Manager Commands: Controlling App Behavior

The Activity Manager (am) commands allow you to start, stop, and interact with applications programmatically.

  • adb shell am force-stop <package_name>: Force-stop an app that’s misbehaving.
  • adb shell am start -n <package_name>/<activity_name>: Launch a specific activity within an app.
  • adb shell am start -a <action> -d <uri>: Start an activity with a particular action and data URI.
  • adb shell am broadcast -a <action>: Send a broadcast intent.
  • adb shell am startservice <service_name>: Start a service.
  • adb shell am stopservice <service_name>: Stop a service.

Example: Sending an SMS via ADB

adb shell am start -a android.intent.action.SENDTO -d sms:22222222 --es sms_body "HELLO"
adb shell input keyevent 22
adb shell input keyevent 23

Package Manager Commands:

Whether you need to list, disable, or clear apps, the Package Manager (pm) commands give you precise control over app management.

  • adb shell pm list packages: List all installed packages on the device.
  • adb shell pm list packages -s: List only system packages.
  • adb shell pm list packages -3: List only third-party packages.
  • adb shell pm disable-user <package_name>: Disable a specific package, temporarily removing it from view.
  • adb shell pm enable <package_name>: Re-enable a package.
  • adb shell pm clear <package_name>: Clear all data associated with an app.

The Monkey Tool: Stress-Testing with Random Events

The Monkey tool is a powerful way to stress-test your application by generating random events, helping you identify weaknesses under pressure.

adb shell monkey -p com.myapp.package -v 5000 -s 100

Granting and Revoking App Permissions: Fine-Tuning App Security

With ADB, you can grant or revoke specific permissions for an app, ensuring it has just the right level of access.

  • adb shell pm grant <package_name> <permission>: Grant a permission to an app.
  • adb shell pm revoke <package_name> <permission>: Revoke a permission from an app.

Example:

adb shell pm grant com.myapp.package android.permission.CAMERA // to grant permission
adb shell pm revoke com.myapp.package android.permission.CAMERA // to revoke permission

Screen Capture and Recording: Documenting Your Work

Sometimes, you need to capture exactly what’s happening on your device’s screen. ADB makes it easy to take screenshots or record screen activity.

  • adb shell screencap /sdcard/screen.png: Capture a screenshot and save it to your device.
  • adb pull /sdcard/screen.png: Pull the screenshot from your device to your computer.
  • adb shell screenrecord /sdcard/video.mp4: Record the screen as a video file.
  • adb pull /sdcard/video.mp4: Pull the recorded video to your computer for analysis or sharing.

Thanks for reading! If this article was helpful, please follow me in medium and share this article on social media!!!

Follow me on Linkedin

--

--

Rashik

I am Rashik, an Android developer I enjoy sharing insights and writing articles about the Android framework/AOSP and embedded Linux..