常用命令

注:文中 "[ip] ,[包名],[apk路径]"等位置整体替换。

  • 安装apk的时候自动授予app所有权限
adb shell install -g MyApp.apk
  • 查看已连接设备
adb devices
  • adb wifi连接(需要数据线)
//查看Ip地址
adb shell ifconfig | grep Mask
//设置端口号
adb tcpip 5555
无线连接adb
adb connect [ip]:5555
//断开连接
adb disconnect [ip]:5555
  • 安装应用
//安装(升级或者同级覆盖安装)
adb install -r [apk路径]
//允许降级安装
adb install -d [apk路径]
  • 卸载应用
adb uninstall [包名]
  • 清除应用缓存和数据
adb shell pm clear [包名]
  • 关闭App
adb shell am force-stop [包名]
  • 启动App -n为指定完整 component 名,用于明确指定启动哪个 Activity
adb shell am start -n com.android.settings/.Settings
  • 列出包名 可以加 | grep xxxx 过滤也可以不加直接后边接要查询的字符串
adb shell pm list packages xxxx
adb shell dumpsys package | grep xxxx
  • 查找应用详细信息和版本号
adb shell dumpsys package com.android.settings
adb shell dumpsys package com.android.settings | grep version
  • 从设备复制文件到电脑
adb pull [设备里的文件路径] [电脑上的目录]
adb pull data/anr/ /Desktop
  • 从电脑复制文件到设备
adb push  /Desktop/xxx.png /sdcard/
  • 模拟点击
adb shell input tap 50 50
  • 模拟按键
adb shell input keyevent [keycode]

keycode

含义

3

centered HOME 键

4

返回键

5

打开拨号应用

6

挂断电话

24

增加音量

25

降低音量

26

电源键

27

拍照(需要在相机应用里)

64

打开浏览器

82

菜单键

85

播放/暂停

86

停止播放

87

播放下一首

88

播放上一首

122

移动光标到行首或列表顶部

123

移动光标到行末或列表底部

126

恢复播放

127

暂停播放

164

静音

176

打开系统设置

187

切换应用

207

打开联系人

208

打开日历

209

打开音乐

210

打开计算器

220

降低屏幕亮度

221

提高屏幕亮度

223

系统休眠

224

点亮屏幕

231

打开语音助手

276

如果没有 wakelock 则让系统休眠

  • 模拟滑动
adb shell input swipe 300 300 500 500
  • 位于文本框时,文本框要获取焦点并可以输入此时可通过input键入文本
adb shell input text hello
  • 日志(注: 在 macOS 下需要给 :W 这样以 * 作为 tag 的参数加双引号,如 adb logcat “:W”,不然会报错 no matches found: *:W。)
adb logcat [过滤] 
例如: adb logcat ":W"
  • 清空日志
adb logcat -c
  • 获取设备属性信息(更多信息查看:常用命令参考)
adb shell getprop ro.product.model
  • 获取设备Dpi屏幕密度
adb shell wm density
  • 获取设备屏幕大小(像素)
adb shell wm size
  • 屏幕截图结合adb pull 可以导入电脑中
//-p 指定保存文件为 png 格式
adb shell screencap -p /sdcard/sc.png
  • 屏幕录屏adb pull 可以导入电脑中
adb shell screenrecord /sdcard/filename.mp4
  • 查看进程
adb shell ps
  • 查看实时资源占用情况
adb shell top
  • 查看某个app的进程id
adb shell dumpsys package [包名] | grep userId=
  • 查看monkey进程Id
adb shell ps | grep monkey
  • 杀死进程
adb shell kill [进程Id]

常用命令参考常用命令大全Monkey测试相关命令