Using Android Debug Bridge (ADB) for App Development and Device Management
Introduction
Android Debug Bridge (ADB) is a versatile command-line tool that lets you communicate with and control an Android device over a USB connection from your computer. Whether you’re a developer debugging applications, a power user managing your device, or someone who needs to recover data from a device with a broken screen, ADB is an essential tool in your Android toolkit.
What is ADB?
ADB is part of the Android SDK (Software Development Kit) and provides a variety of functions:
- Installing and debugging apps
- Accessing the device shell
- Transferring files between computer and device
- Creating backups
- Taking screenshots and screen recordings
- Managing system settings and permissions
- Troubleshooting device issues
Setting Up ADB
Installation
On Windows:
- Download the Android SDK Platform Tools ZIP file
- Extract the ZIP file to an easily accessible location (e.g.,
C:\adb
) - Add the platform-tools directory to your system PATH:
- Right-click on “This PC” and select “Properties”
- Click on “Advanced system settings”
- Click on “Environment Variables”
- Under “System variables”, find and select “Path”, then click “Edit”
- Click “New” and add the path to your platform-tools directory
- Click “OK” to save
On macOS:
Using Homebrew:
brew install android-platform-tools
Or manual installation:
- Download the Android SDK Platform Tools ZIP file
- Extract to a location like
~/Library/Android/sdk/platform-tools
- Add to your PATH by adding this line to your
~/.bash_profile
or~/.zshrc
:export PATH="$PATH:~/Library/Android/sdk/platform-tools"
On Linux:
Using apt (Debian/Ubuntu):
sudo apt update
sudo apt install adb
Using dnf (Fedora):
sudo dnf install android-tools
Enabling USB Debugging on Your Android Device
- Go to Settings > About phone
- Tap “Build number” seven times to enable Developer options
- Go back to Settings > System > Developer options
- Enable “USB debugging”
- Connect your device to your computer via USB
- Accept the “Allow USB debugging” prompt on your device
Verifying Installation
To check if ADB is properly installed and can detect your device:
adb devices
You should see your device listed with a serial number and “device” status.
Basic ADB Commands
Device Management
# List connected devices
adb devices
# Connect to a specific device when multiple are connected
adb -s DEVICE_SERIAL_NUMBER command
# Connect to a device over Wi-Fi
adb connect DEVICE_IP:5555
# Disconnect from a Wi-Fi device
adb disconnect DEVICE_IP:5555
# Reboot device
adb reboot
# Reboot to recovery mode
adb reboot recovery
# Reboot to bootloader
adb reboot bootloader
File Management
# Push a file to device
adb push /path/to/local/file /path/on/device
# Pull a file from device
adb pull /path/on/device /path/to/local/directory
# List files in a directory on device
adb shell ls /path/on/device
App Management
# Install an app
adb install path/to/app.apk
# Install app, replacing existing app
adb install -r path/to/app.apk
# Uninstall an app
adb uninstall com.example.app
# Uninstall app but keep data and cache
adb uninstall -k com.example.app
# Clear app data
adb shell pm clear com.example.app
# List all installed packages
adb shell pm list packages
# List only third-party packages
adb shell pm list packages -3
Debugging
# View device log
adb logcat
# Filter logcat by tag
adb logcat -s TAG_NAME
# Clear logcat buffer
adb logcat -c
# Dump system information
adb shell dumpsys
# Dump specific service information
adb shell dumpsys battery
Advanced ADB Usage
Shell Commands
ADB shell gives you access to a Unix shell running on the device:
# Enter interactive shell
adb shell
# Run a shell command directly
adb shell command
Common shell commands:
# View running processes
adb shell ps
# Check memory usage
adb shell dumpsys meminfo
# View CPU info
adb shell cat /proc/cpuinfo
# Check battery stats
adb shell dumpsys battery
# List all permissions
adb shell pm list permissions
# Grant a permission
adb shell pm grant com.example.app android.permission.CAMERA
# Revoke a permission
adb shell pm revoke com.example.app android.permission.CAMERA
Screen Capture
# Take a screenshot
adb shell screencap /sdcard/screenshot.png
adb pull /sdcard/screenshot.png
# Record screen (Android 4.4+)
adb shell screenrecord /sdcard/video.mp4
# Press Ctrl+C to stop recording
adb pull /sdcard/video.mp4
Backup and Restore
# Create a full backup
adb backup -apk -shared -all -f backup.ab
# Restore from backup
adb restore backup.ab
# Backup a specific app
adb backup -apk -f app_backup.ab com.example.app
Wireless Debugging
Connect your device via USB first, then:
# Make sure device and computer are on same network
adb tcpip 5555
# Disconnect USB and connect wirelessly
adb connect DEVICE_IP:5555
# To switch back to USB mode
adb usb
Practical Examples
Transferring Files When Screen is Broken
If your device screen is broken but the device still works:
# Enable USB debugging via ADB (if not already enabled)
adb shell settings put global adb_enabled 1
# Pull your photos
adb pull /sdcard/DCIM/Camera /path/on/computer
# Pull your documents
adb pull /sdcard/Documents /path/on/computer
Automating App Testing
# Install app
adb install app.apk
# Start an activity
adb shell am start -n com.example.app/.MainActivity
# Send keyevents
adb shell input keyevent KEYCODE_HOME
# Tap on screen
adb shell input tap X Y
# Swipe on screen
adb shell input swipe X1 Y1 X2 Y2 DURATION_MS
Fixing Common Issues
# Force stop an app
adb shell am force-stop com.example.app
# Clear app cache
adb shell pm clear com.example.app
# Reset app permissions
adb shell pm reset-permissions
Troubleshooting ADB
Device Not Detected
- Ensure USB debugging is enabled
- Try different USB cables and ports
- Install/update device drivers
- Restart ADB server:
adb kill-server adb start-server
Permission Denied Errors
On Linux/macOS:
sudo adb kill-server
sudo adb start-server
Connection Refused
For wireless connections:
- Ensure device and computer are on same network
- Check firewall settings
- Reconnect via USB and restart wireless connection
Conclusion
ADB is a powerful tool that gives you unprecedented control over your Android device. While it may seem intimidating at first, mastering even a few basic commands can significantly enhance your ability to develop, test, and manage Android devices. As you become more comfortable with ADB, you’ll discover even more ways to leverage its capabilities for your specific needs.
Further Resources
- Official ADB Documentation{target="_blank" rel=“noopener noreferrer”}
- Android Developers Guide{target="_blank" rel=“noopener noreferrer”}
- ADB Shell Commands Reference{target="_blank" rel=“noopener noreferrer”}