adb 常用命令笔记
环境准备
确保设备已安装ADB工具并启用USB调试模式。通过以下命令验证设备连接:
adb devices
输出应显示设备序列号和“device”状态。
基础ADB操作
获取当前活动窗口信息:
adb shell dumpsys window windows | grep -E 'mCurrentFocus'
模拟点击事件需获取屏幕坐标:
adb shell getevent -p
输入自动化
执行文本输入命令:
adb shell input text "test123"
模拟物理按键操作:
adb shell input keyevent 4 # 返回键
屏幕截图与分析
截取屏幕并导出到本地:
adb shell screencap -p /sdcard/screen.png
adb pull /sdcard/screen.png ~/Desktop/
使用OpenCV进行图像识别(需Python环境):
import cv2
template = cv2.imread('button_template.png', 0)
screen = cv2.imread('screen.png', 0)
res = cv2.matchTemplate(screen, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
事件序列封装
创建可复用的Shell函数:
function tap() {adb shell input tap $1 $2
}
function swipe() {adb shell input swipe $1 $2 $3 $4 $5
}
循环测试逻辑
实现重复压力测试:
for i in {1..100}; dotap 500 800sleep 1adb shell input keyevent 3
done
日志收集
定向抓取错误日志:
adb logcat -v time | grep "E/"
按时间戳保存日志文件:
adb logcat -d -v time > $(date +"%Y%m%d_%H%M%S").log
性能监控
获取CPU使用率:
adb shell top -n 1 | grep com.target.app
监测内存占用:
adb shell dumpsys meminfo com.target.app
异常处理
检测ANR并重启应用:
if adb logcat -d | grep -q "ANR in"; thenadb shell am force-stop com.target.appadb shell am start -n com.target.app/.MainActivity
fi
设备控制
重启设备并等待恢复:
adb reboot
until adb devices | grep -w device; do sleep 5; done
设置系统属性值:
adb shell setprop debug.layout false