Android ADB Debugging Skill
Overview
This skill provides a structured reference for using Android Debug Bridge (ADB) to interact with Android devices/emulators. It covers environment setup, application management, log analysis, file transfer, and UI automation.
Prerequisites
- Android SDK Platform-Tools installed (
adbin PATH). - USB Debugging enabled on the target device.
- Device connected via USB or Wi-Fi.
Core Capabilities
1. Device Connection & Management
Use these commands to verify connectivity and manage device state.
| Action | Command | Notes |
| :--- | :--- | :--- |
| List Devices | adb devices | Check if device is authorized. |
| Restart ADB Server | adb kill-server && adb start-server | Fix connection issues. |
| Reboot Device | adb reboot | Normal reboot. |
| Reboot to Bootloader | adb reboot bootloader | For flashing ROMs. |
| Wireless Connect | adb tcpip 5555 <br> adb connect <IP>:5555 | Requires initial USB connection. |
2. Application Installation & Management
Commands to install, uninstall, and clear app data.
| Action | Command | Notes |
| :--- | :--- | :--- |
| Install APK | adb install path/to/app.apk | Fresh install. |
| Reinstall (Keep Data) | adb install -r path/to/app.apk | Update existing app. |
| Uninstall App | adb uninstall <package_name> | e.g., com.example.app |
| Clear App Data | adb shell pm clear <package_name> | Resets app to fresh state. |
| List Packages | adb shell pm list packages | Find package names. |
| Grant Permission | adb shell pm grant <pkg> <perm> | e.g., android.permission.CAMERA |
3. Logcat & Debugging
Essential for diagnosing crashes and runtime behavior.
| Action | Command | Notes |
| :--- | :--- | :--- |
| View Logs | adb logcat | Real-time stream. |
| Clear Logs | adb logcat -c | Clear buffer before testing. |
| Filter by Tag | adb logcat -s MyTag | Case-sensitive tag filter. |
| Filter by PID | adb logcat --pid=<pid> | Specific process. |
| Save to File | adb logcat -d > logs.txt | Dump current buffer. |
| Crash Stacktrace | adb logcat *:E | Show only errors. |
4. Screen Capture & Recording
Visual debugging tools.
| Action | Command | Notes |
| :--- | :--- | :--- |
| Screenshot | adb exec-out screencap -p > screen.png | Saves directly to host. |
| Start Recording | adb shell screenrecord /sdcard/demo.mp4 | Max 180s. Press Ctrl+C to stop. |
| Pull Recording | adb pull /sdcard/demo.mp4 . | Retrieve video file. |
5. File Transfer
Moving files between host and device.
| Action | Command | Notes |
| :--- | :--- | :--- |
| Push to Device | adb push local_file.txt /sdcard/ | Upload file. |
| Pull from Device | adb pull /sdcard/file.txt . | Download file. |
| List Files | adb shell ls /sdcard/ | Remote directory listing. |
6. UI Automation & Interaction
Simulating user input for testing.
| Action | Command | Notes |
| :--- | :--- | :--- |
| Tap Coordinate | adb shell input tap <x> <y> | Simulate touch. |
| Swipe | adb shell input swipe <x1> <y1> <x2> <y2> <duration_ms> | Simulate swipe. |
| Type Text | adb shell input text "Hello" | No spaces allowed directly (use %s). |
| Key Event | adb shell input keyevent <code> | e.g., 4 (Back), 66 (Enter). |
| Start Activity | adb shell am start -n <pkg>/<activity> | Launch specific screen. |
| Force Stop | adb shell am force-stop <pkg> | Kill app process. |
Troubleshooting Common Issues
"Device Unauthorized"
- Check device screen for RSA fingerprint confirmation dialog.
- Run
adb kill-server && adb start-server. - Revoke USB debugging authorizations in Developer Options and reconnect.
"ADB Not Recognized"
- Ensure Android SDK Platform-Tools are installed.
- Add platform-tools directory to system PATH.
- Verify installation:
adb version.
"More than one device connected"
Specify the target device using -s: adb -s <device_serial>
7. Performance Monitoring (性能监控)
Network & Traffic
| Action | Command | Notes |
| :--- | :--- | :--- |
| List Active Connections | adb shell ss -tunp | Shows IP:Port pairs. |
| Filter Connections by UID | adb shell ss -tunp \| grep <uid> | Get UID via dumpsys package. |
| Check Traffic Stats | adb shell dumpsys netstats \| grep <pkg> | Cumulative RX/TX data. |
| Legacy Traffic Check | adb shell cat /proc/uid_stat/<uid>/tcp_rcv | Receive bytes only. |
CPU Usage
| Action | Command | Notes |
| :--- | :--- | :--- |
| Real-time Top | adb shell top -n 1 -o %CPU,PID,NAME | Snapshot of CPU usage. |
| Continuous Monitor | adb shell top \| grep <package_name> | Live stream for specific app. |
| Detailed CPU Info | adb shell dumpsys cpuinfo \| grep <pkg> | User/System split. |
Memory Usage
| Action | Command | Notes |
| :--- | :--- | :--- |
| Detailed Meminfo | adb shell dumpsys meminfo <pkg> | Look for "TOTAL PSS". |
| Summary Meminfo | adb shell dumpsys meminfo --summary <pkg> | Quick overview. |
| Java Heap Dump | adb shell am dumpheap <pid> /sdcard/heap.hprof | Then pull file for analysis. |
Disk I/O
| Action | Command | Notes |
| :--- | :--- | :--- |
| Process IO Stats | adb shell cat /proc/<pid>/io | May require Root access. |
| Systrace for IO | python systrace.py -a <pkg> -t 5 io | Generates HTML trace file. |
Pro Tips for Performance Debugging
- PSS vs RSS: Always prefer PSS (Proportional Set Size) from
dumpsys meminfoover RSS, as PSS accounts for shared memory libraries correctly. - UID Mapping: To map network traffic to an app, first get the UID:
adb shell dumpsys package com.example.app | grep userId. Then use that UID inssor/proc/uid_stat/. - IO Permissions: If
/proc/<pid>/iois inaccessible, rely onsystraceor Android Studio Profiler for detailed I/O breakdowns.
微信扫一扫