Android Debugger (ADB) cheat sheet

adb is the command-line tool used for interacting with Android devices and debugging Android apps via the terminal. It’s an incredibly versatile tool, full of many hidden gems. It can let you debug your apps over WiFi, copy files to/from your device, view logs, run terminal commands, and much more.

Here’s a quick list of some ADB commands that I found useful.

Connections

1
2
3
4
# Get a list of connected devices
$ adb devices
List of devices attached
1ABCD9GH4S9836 device
1
2
3
# Tell your device to start listening for incoming connections via WiFi, on port 5555
$ adb tcpip 5555
restarting in TCP mode port: 5555
1
2
3
# Connect to the device via WiFi. After this, you can now unplug it and continue debugging wirelessly.
$ adb connect 10.0.1.120
connected to 10.0.1.120:5555
1
2
3
# Disconnect all wifi connections. USB connections stay active, even though it says "everything".
$ adb disconnect
disconnected everything

Using the above commands, it’s possible to create script to automatically start wireless debugging. Simply plug in your device, run the script, then unplug it again. You can find an example script for Mac (and maybe Linux) here. There’s also one for Windows here.

Networking

1
2
3
4
5
# Get your device's WiFi IP address.
$ adb shell ip -f inet addr show wlan0
7: wlan0: mtu 1500 qdisc mq state UP group default qlen 3000
inet 10.0.1.115/24 brd 10.0.1.255 scope global wlan0
valid_lft forever preferred_lft forever
1
2
3
# Extract just the IP address
$ adb shell "ip -f inet addr show wlan0 | grep -E -o '([0-9]{1,3}[\.]){3}[0-9]{1,3}' | head -1"
10.0.1.115
1
2
3
# Forward the port 8080 from your local machine to the localhost of the device. Very useful for debugging web apps locally.
$ adb forward tcp:8080 tcp:8080
8080

Managing apps

1
2
3
4
# Install an APK from your computer onto the device.
$ adb install myapp.apk
Performing Streamed Install
Success
1
2
3
# Uninstall an app by specifying it's package name.
$ adb uninstall com.mypackage.app
Success
1
2
3
4
5
# List installed apps. The `-3` hides system apps.
$ adb shell pm list packages -3
package:com.sec.android.inputmethod.beta
package:com.skype.raider
...
1
2
3
# Clear all app data for the specified app.
$ adb shell pm clear com.mypackage.app
Success
1
2
# Force quit an application.
$ adb shell am force-stop com.mypackage.app
1
2
# Start an app's default activity. The `1` at the end is necessary.
$ adb shell monkey -p com.mypackage.app 1

Files

1
2
3
4
# List files in the specified folder.
$ adb shell ls /mnt/sdcard
file1.jpg
file2.txt
1
2
3
# Pull a file from the device. You can also specify a destination directory or file name on your computer as an extra argument.
$ adb pull /mnt/sdcard/file1.jpg
1 file pulled, 0 skipped. 1.2 MB/s (5779 bytes in 0.005s)
1
2
3
# Push a file to the device. First argument is the path on your computer, the second is the path on the device.
$ adb push file1.jpg /mnt/sdcard
1 file pushed, 0 skipped. 0.4 MB/s (10414 bytes in 0.027s)

Logs

1
2
# Display all logs from every app. See https://developer.android.com/studio/command-line/logcat for logcat arguments.
$ adb logcat
1
2
# Filter log output to only lines that match the specified search query. Regex supported.
$ adb logcat -e MyTag

Other useful commands

1
2
3
# Take a screenshot and save it to your computer.
$ adb shell screencap /mnt/sdcard/screenshot.png
$ adb pull /mnt/sdcard/screenshot.png
1
2
3
# Record your device's screen. Press Ctrl+C to stop the recording.
$ adb shell screenrecord /mnt/sdcard/video.mp4
$ adb pull /mnt/sdcard/video.mp4
  • Copyrights © 2015-2021 jjv360

请我喝杯咖啡吧~

支付宝
微信