一、Monkey 基础命令
adb shell monkey [参数] <事件次数>
二、常用参数分类与示例
1. 基础控制
-
-p <包名>
指定测试的应用(可多次使用测试多个应用):
adb shell monkey -p com.example.app1 -p com.example.app2 1000
-
-v
设置日志详细级别(1~3,默认1):
adb shell monkey -v -v 500
-
-s <种子值>
指定随机数生成器的种子值(用于复现相同操作序列):
adb shell monkey -s 12345 1000
2. 事件类型控制
-
--pct-touch <百分比>
调整触摸事件比例(如点击、长按):
adb shell monkey --pct-touch 40 1000
-
--pct-motion <百分比>
调整手势事件比例(如滑动):
adb shell monkey --pct-motion 30 1000
-
--pct-trackball <百分比>
调整轨迹球事件比例(模拟方向键操作):
adb shell monkey --pct-trackball 10 1000
-
--pct-nav <百分比>
调整导航事件比例(如上下左右键):
adb shell monkey --pct-nav 10 1000
-
--pct-syskeys <百分比>
调整系统按键比例(如 Home、Back、音量键):
adb shell monkey --pct-syskeys 5 1000
3. 异常处理
-
--ignore-crashes
忽略应用崩溃,继续执行后续事件:
adb shell monkey --ignore-crashes 1000
-
--ignore-timeouts
忽略 ANR(应用无响应),继续测试:
adb shell monkey --ignore-timeouts 1000
-
--kill-process-after-error
发生错误后终止进程(默认行为是继续运行):
adb shell monkey --kill-process-after-error 1000
4. 高级控制
-
--throttle <毫秒>
设置事件之间的延迟(模拟真实用户操作速度):
adb shell monkey --throttle 300 1000
-
--monitor-native-crashes
监控 Native 层崩溃(如 C/C++ 代码崩溃):
adb shell monkey --monitor-native-crashes 1000
-
--bugreport
自动生成 Bugreport 日志(需结合 -v -v -v
):
adb shell monkey -v -v -v --bugreport 1000
三、实战场景示例
1. 基础压力测试
adb shell monkey -p com.example.app -v 5000
2. 稳定性测试(忽略崩溃/ANR)
adb shell monkey -p com.example.app --throttle 500 --ignore-crashes --ignore-timeouts 100000
3. 模拟用户高频点击
adb shell monkey -p com.example.app --pct-touch 80 --pct-motion 20 --throttle 100 5000
4. 定向复现问题
adb shell monkey -p com.example.app -s 2024 -v 1000
四、日志分析与调试
1. 实时日志输出
adb logcat | grep "Monkey"
2. 保存 Monkey 日志到文件
adb shell monkey -p com.example.app -v 1000 > monkey_log.txt
3. 关键日志标记
// CRASH
:应用崩溃
// ANR
:应用无响应
// Monkey finished
:测试完成
五、注意事项
- 权限问题:部分系统操作需 Root 权限(如发送特定按键事件)。
- 事件比例:合理分配事件比例,避免过度偏向单一操作(如默认触摸事件较少)。
- 设备状态:测试前关闭无关应用,避免干扰结果。
- 结果分析:结合
logcat
和崩溃堆栈定位问题。