目录
简介 ADB (Android Debug Bridge) 是一个多功能的命令行工具,用于与Android设备进行通信。它是Android SDK的一部分,允许开发者在计算机和Android设备之间传输数据、安装应用、调试程序等。
基本语法 1 adb [global options] command [command options]
设备连接与管理 查看连接的设备 1 2 3 4 5 adb devices adb devices -l
连接设备 1 2 3 4 5 6 7 8 adb usb adb connect <设备IP>:5555 adb disconnect <设备IP>:5555
多设备操作 1 2 3 4 5 adb -s <设备序列号> <命令> adb -s emulator-5554 install app.apk
设备状态管理 1 2 3 4 5 6 7 8 9 10 11 adb reboot adb reboot recovery adb reboot bootloader adb wait-for-device
应用管理 安装应用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 adb install <apk文件路径> adb install -r <apk文件路径> adb install -s <apk文件路径> adb install -d <apk文件路径> adb install -r -d <apk文件路径>
卸载应用 1 2 3 4 5 6 7 8 adb uninstall <包名> adb uninstall -k <包名> adb uninstall com.tencent.mm
应用信息查询 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 adb shell pm list packages adb shell pm list packages -s adb shell pm list packages -3 adb shell pm list packages | grep <关键字> adb shell dumpsys package <包名> adb shell pm path <包名>
应用操作 1 2 3 4 5 6 7 8 9 10 11 adb shell am start -n <包名>/<活动名> adb shell monkey -p <包名> -c android.intent.category.LAUNCHER 1 adb shell am force-stop <包名> adb shell pm clear <包名>
文件传输 推送文件到设备 1 2 3 4 5 6 7 8 adb push <本地文件路径> <设备路径> adb push /Users/username/photo.jpg /sdcard/Pictures/ adb push <本地文件夹> <设备路径>
从设备拉取文件 1 2 3 4 5 6 7 8 adb pull <设备文件路径> <本地路径> adb pull /sdcard/screenshot.png ./ adb pull <设备文件夹路径> <本地路径>
文件系统操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 adb shell ls -lacat <文件路径>mkdir <目录名>rm <文件路径>rm -rf <目录路径>mv <源路径> <目标路径>cp <源路径> <目标路径>
系统操作 系统信息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 adb shell getprop adb shell getprop ro.build.version.release adb shell getprop ro.product.model adb shell getprop ro.product.manufacturer adb shell getprop ro.product.cpu.abi adb shell cat /proc/meminfo adb shell df -h
权限管理 1 2 3 4 5 6 7 8 9 10 11 adb shell pm grant <包名> <权限名> adb shell pm revoke <包名> <权限名> adb shell dumpsys package <包名> | grep permission adb shell pm grant com.example.app android.permission.CAMERA
设置操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 adb shell am start -a android.settings.SETTINGS adb shell am start -a android.settings.WIFI_SETTINGS adb shell am start -a android.settings.APPLICATION_DETAILS_SETTINGS -d package:<包名> adb shell settings put system <设置名> <值> adb shell settings get system <设置名>
调试与日志 日志查看 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 adb logcat adb logcat -c adb logcat -s <标签名> adb logcat *:E adb logcat > log.txt adb logcat | grep <包名> adb logcat -v time adb logcat -v threadtime
性能分析 1 2 3 4 5 6 7 8 9 10 11 12 13 14 adb shell top adb shell dumpsys meminfo adb shell dumpsys meminfo <包名> adb shell dumpsys battery adb shell cat /proc/net/dev
网络与端口转发 端口转发 1 2 3 4 5 6 7 8 9 10 11 12 13 14 adb forward tcp:<本地端口> tcp:<设备端口> adb forward tcp:8080 tcp:8080 adb forward --list adb forward --remove tcp:<本地端口> adb forward --remove-all
网络操作 1 2 3 4 5 6 7 8 9 10 11 adb shell ifconfig adb shell netstat adb shell ping <目标地址> adb shell dumpsys wifi
屏幕操作 截图和录屏 1 2 3 4 5 6 7 8 9 10 11 12 adb shell screencap /sdcard/screenshot.png adb pull /sdcard/screenshot.png adb shell screenrecord /sdcard/demo.mp4 adb shell screenrecord --time-limit 30 /sdcard/demo.mp4 adb shell screenrecord --size 1280x720 /sdcard/demo.mp4
输入操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 adb shell input text "Hello World" adb shell input keyevent <键码> adb shell input tap <x坐标> <y坐标> adb shell input swipe <x1> <y1> <x2> <y2> <持续时间ms> adb shell input swipe 500 1000 500 500 1000
性能监控 系统监控 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 adb shell top -n 1 adb shell cat /proc/meminfo adb shell iostat adb shell cat /proc/net/dev adb shell ps adb shell ps | grep <进程名>
应用性能 1 2 3 4 5 6 7 8 9 10 11 adb shell top | grep <包名> adb shell dumpsys meminfo <包名> adb shell cat /proc/net/xt_qtaguid/stats | grep <UID> adb shell am start -W <包名>/<活动名>
高级功能 备份与恢复 1 2 3 4 5 6 7 8 adb backup -apk -shared -nosystem -all adb backup -apk <包名> adb restore <备份文件>
开发者选项 1 2 3 4 5 6 7 8 9 10 adb shell settings get global development_settings_enabled adb shell settings put global window_animation_scale 0 adb shell settings put global transition_animation_scale 0 adb shell settings put global animator_duration_scale 0
自动化脚本示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/bin/bash APK_PATH="app.apk" PACKAGE_NAME="com.example.app" ACTIVITY_NAME="com.example.app.MainActivity" echo "安装应用..." adb install -r $APK_PATH echo "启动应用..." adb shell am start -n $PACKAGE_NAME /$ACTIVITY_NAME echo "完成!"
常见问题解决 设备连接问题 1 2 3 4 5 6 7 8 9 adb kill-server adb start-server adb version
权限问题 1 2 3 4 5 6 adb devices adb root
性能优化 1 2 3 4 adb shell settings put global window_animation_scale 0.5 adb shell settings put global transition_animation_scale 0.5 adb shell settings put global animator_duration_scale 0.5
总结 这份ADB命令手册涵盖了日常开发和调试中最常用的命令。建议将常用命令制作成脚本或别名,以提高工作效率。记住在使用某些命令时可能需要设备root权限或用户确认。
快速参考
adb devices
- 查看连接设备
adb install <apk>
- 安装应用
adb shell
- 进入设备shell
adb logcat
- 查看日志
adb push/pull
- 文件传输
adb shell screencap
- 截图
adb shell input
- 模拟输入
更多详细信息请参考 Android官方文档 。