adb常用命令总结

打印 上一主题 下一主题

主题 1048|帖子 1048|积分 3144

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1 前言

ADB(Android Debug Bridge)即 Android 调试桥,采用监听 Socket TCP 端口的方式通讯。毗连手机有2种方式:有线毗连、无线毗连。
(1)有线毗连
使用数据线毗连手机后,在【开辟职员选项】中开启【USB 调试】,并在【选择 USB 设置】中选择【MTP (多媒体传输)】。
(2)无线毗连
  1. adb connect ip
复制代码


注意事项:


  • 手机和 PC 需要毗连同一个局域网
  • 需要开启【USB 调试】,并选择【MTP (多媒体传输)】
若报如下错误:
  1. cannot connect to 192.168.0.109:5555: 由于目标计算机积极拒绝,无法连接。 (10061)
复制代码
按照如下步骤可以毗连上手机:


  • 使用数据线毗连手机和 PC,实行命令:adb tcpip 5555
  • 拔掉数据线,开启【USB 调试】,并选择【MTP (多媒体传输)】
  • 实行命令:adb connect 192.168.0.109
2 常用命令

2.1 基础命令

  1. ::查看设备
  2. adb devices
  3. ::查看详细设备信息(含型号)
  4. adb devices -l
  5. ::查看手机信息(品牌、型号、版本等)
  6. adb shell cat /hw_product/etc/prop/local.prop | findstr product
  7. adb shell cat /system/build.prop | findstr product
  8. ::重启
  9. adb reboot
  10. ::查看日志
  11. adb logcat > log.txt
  12. adb logcat | findstr "matched_string"
  13. ::安装/卸载
  14. adb install xxx.apk
  15. adb uninstall package_name
  16. ::拉取/推送
  17. adb remount
  18. adb pull  /sdcard/a.mp4  C:\Users\81518\Desktop\
  19. adb push  C:\Users\81518\Desktop\a.mp4  /sdcard/
  20. ::查看当前界面运行的 activity 及其包名
  21. adb shell dumpsys window | findstr "mCurrentFocus"
  22. ::查看指定命令的参数
  23. adb command --help
  24. adb shell command --help
复制代码
打印方法调用堆栈:
  1. android.util.Log.d("TAG", android.util.Log.getStackTraceString(new Throwable()));
复制代码
2.2 截屏/录制

  1. ::截屏
  2. adb shell screencap -p /sdcard/1.png
  3. adb pull /sdcard/1.png C:\Users\81518\Desktop\
  4. ::录屏
  5. adb shell screenrecord  /sdcard/1.mp4
复制代码
说明:截屏时,可以通过【-d】指定 display id,默认是 0
2.3 查看/修改体系参数

  1. ::查看系统参数值
  2. adb shell getprop system_prams
  3. ::设置系统参数值
  4. adb shell setprop system_prams params_value
  5. ::查看手机型号
  6. adb shell getprop ro.product.model
  7. ::查看手机固件构建时间
  8. adb shell getprop ro.build.date
复制代码
2.4 模仿输入

  1. ::查看模拟输入命令
  2. adb shell input --help
  3. ::点击指定坐标位置
  4. adb shell input tap <x> <y>
  5. ::滑动
  6. adb shell input swipe <x1> <y1> <x2> <y2>
  7. ::按键事件
  8. adb shell input keyevent <keycode>
复制代码
说明:通过 -d 属性可以指定 displayId,应用如下:
  1. ::点击指定display的指定坐标位置
  2. adb shell input -d 1 tap 100 200
复制代码
常用<keycode>如下:
  1. 3    HOME 键
  2. 4    返回键
  3. 5    打开拨号应用
  4. 6    挂断电话
  5. 24   增加音量
  6. 25   降低音量
  7. 26   电源键
  8. 27   拍照(需要在相机应用里)
  9. 64   打开浏览器
  10. 82   菜单键
  11. 85   播放/暂停
  12. 86   停止播放
  13. 87   播放下一首
  14. 88   播放上一首
  15. 122  移动光标到行首或列表顶部
  16. 123  移动光标到行末或列表底部
  17. 126  恢复播放
  18. 127  暂停播放
  19. 164  静音
  20. 176  打开系统设置
  21. 187  切换应用
  22. 207  打开联系人
  23. 208  打开日历
  24. 209  打开音乐
  25. 210  打开计算器
  26. 220  降低屏幕亮度
  27. 221  提高屏幕亮度
  28. 223  系统休眠
  29. 224  点亮屏幕
  30. 231  打开语音助手
  31. 276  如果没有 wakelock 则让系统休眠
复制代码
2.5 adb shell settings

(1)查看属性
  1. ::查看系统/全局/安全属性
  2. adb shell settings list system
  3. adb shell settings list global
  4. adb shell settings list secure
  5. ::获取自动锁屏时间(单位:毫秒 ms)
  6. adb shell settings list system | findstr "screen_off_timeout"
  7. adb shell settings get system screen_off_timeout
  8. ::获取当前屏幕亮度
  9. adb shell settings get system screen_brightness
  10. ::获取默认输入法(com.baidu.input_huawei/.ImeService)
  11. adb shell settings get secure default_input_method
复制代码
(2)设置属性
  1. ::设置自动锁屏时间(单位:毫秒 ms,此处为:1天)
  2. adb shell settings put system screen_off_timeout 86400000
  3. ::设置当前屏幕亮度
  4. adb shell settings put system screen_brightness 100
  5. ::跳过开机导航
  6. adb shell settings put global device_provisioned 1 && adb reboot
复制代码
2.6 adb shell am

am 即 ActivityManager,重要用于启动 activity、service,发送 broadcast 等。
  1. ::打开记事本
  2. adb shell am start com.huawei.notepad
  3. ::关闭 QQ
  4. adb shell am force-stop com.tencent.mobileqq
  5. ::拉起服务
  6. adb shell am startservice  -a com.example.test.DemoService
  7. ::发送广播
  8. adb shell am broadcast -a com.example.text.ACTION
  9. ::恢复出厂设置
  10. adb shell am broadcast -a android.intent.action.MASTER_CLEAR
复制代码
2.7 adb shell wm

wm 即 WindowManager,重要用于窗口管理。
  1. ::查看手机分辨率:Physical size: 1080x2340
  2. adb shell wm size
  3. ::查看物理密度:Physical density: 480
  4. adb shell wm density
复制代码
2.8 adb shell pm

pm 即 PackageManager,重要作用有:

  • 安装、卸载应用
  • 查询已安装应用
  • 查询、增加、删除 permission
  • 查询 Application 相关信息(application、activity、service、receiver、provider及相应属性等)
  • 扫除用户数据、缓存等
  1. ::安装应用(安装包在手机上)
  2. adb shell pm install package_path
  3. ::卸载应用
  4. adb shell pm uninstall package_name
  5. ::查询已安装应用包名
  6. adb shell pm list packages [-s:系统包] [-3:三方包]
  7. ::获取指应用的apk路径
  8. adb shell pm path com.example.test
  9. ::获取指定应用的安装路径
  10. adb shell pm get-install-location
  11. ::授予权限
  12. adb shell pm grant com.example.test android.permission.READ_EXTERNAL_STORAGE
  13. ::撤销权限
  14. adb shell pm revoke com.example.test android.permission.WRITE_EXTERNAL_STORAGE
  15. ::列出所有用户
  16. adb shell pm list users
  17. ::创建用户
  18. adb shell pm create-user "user_name"
  19. ::删除有用户
  20. adb shell pm remove-user user_id
  21. ::清除指定应用的所有数据
  22. adb shell pm clear com.example.test
复制代码
2.9 历程操作

  1. ::查看进程
  2. adb shell ps | findstr "com.tencent.mobileqq"
  3. ::kill进程
  4. adb shell kill pid
  5. adb shell am force-stop package_name
  6. ::关闭/启动 adb 服务
  7. adb kill-server
  8. adb start-server
复制代码
2.10 adb shell uiautomator

dump 布局
  1. adb shell uiautomator dump --compressed  /data/local/tmp/uidump.xml
  2. adb pull /data/local/tmp/uidump.xml
复制代码
XML 在线格式化
2.11 dumpsys

(1)adb shell dumpsys activity
  1. ::查看Activity组件信息
  2. adb shell dumpsys activity activities
  3. adb shell dumpsys activity activities | findstr /C:"* Hist"
  4. ::查看Service组件信息
  5. adb shell dumpsys activity services
  6. ::查看ContentProvider组件信息
  7. adb shell dumpsys activity providers
  8. ::查看BraodcastReceiver信息
  9. db shell dumpsys activity broadcasts
  10. ::查看Intent信息
  11. adb shell dumpsys activity intents
  12. ::查看进程信息
  13. adb shell dumpsys activity processes
复制代码
(2)adb shell dumpsys window
  1. ::查看窗口信息
  2. adb shell dumpsys window windows
  3. adb shell dumpsys window windows | findstr /C:"Window #"
  4. ::查看屏幕信息
  5. adb shell dumpsys window displays
  6. ::查看策略信息
  7. adb shell dumpsys window policy
  8. ::查看动画信息
  9. adb shell dumpsys window animator
  10. ::查看会话信息
  11. adb shell dumpsys window sessions
  12. ::查看 tokens 信息
  13. adb shell dumpsys window tokens
  14. ::查看 trace 信息
  15. adb shell dumpsys window trace
复制代码
(3)其他
  1. ::查看输入信息
  2. adb shell dumpsys input
  3. ::查看内存信息
  4. adb shell dumpsys meminfo
  5. ::查看电源管理信息
  6. adb shell dumpsys power
  7. ::查看电池信息,【即当前电量、电池状态、电池温度等】
  8. adb shell dumpsys battery
  9. ::查看渲染信息
  10. adb shell dumpsys SurfaceFlinger
复制代码
3 窗口和性能分析

3.1 winscope

winscope 可以查看每一帧的窗口层级信息,便于分析窗口 “闪”、“黑” 等问题。
抓取 winscope 的方法如下。抓取到 winscope 和视频文件后,将它们拖拽到 winscope.html 网页,可以查看每一帧的截图、窗口层级布局、窗口属性等信息。winscope.html 文件的位置: aosp\platform\prebuilts\misc\common\winscope\winscope.html。
1)window 跟踪
  1. adb root
  2. :: 开始抓trace
  3. adb shell cmd window tracing start
  4. :: 开始录屏
  5. adb shell screenrecord /sdcard/screenrecord.mp4
  6. :: 结束录屏(按 Ctrl + C)
  7. :: 结束抓trace
  8. adb shell cmd window tracing stop
  9. :: 获取录屏文件
  10. adb pull /sdcard/screenrecord.mp4
  11. :: 获取trace文件
  12. adb pull /data/misc/wmtrace/wm_trace.winscope wm_trace.winscope
复制代码
2)SurfaceFlinger 跟踪
  1. adb root
  2. :: 开始抓trace
  3. adb shell service call SurfaceFlinger 1025 i32 1
  4. :: 开始录屏
  5. adb shell screenrecord /sdcard/screenrecord.mp4
  6. :: 结束录屏(按 Ctrl + C)
  7. :: 结束抓trace
  8. adb shell service call SurfaceFlinger 1025 i32 0
  9. :: 获取录屏文件
  10. adb pull /sdcard/screenrecord.mp4
  11. :: 获取trace文件
  12. adb pull /data/misc/wmtrace/layers_trace.winscope
复制代码
3.2 Perfetto

Perfetto 是一个高性能的端到端可视化工具,用于体系性能跟踪和分析。它重要用于 Android 和 Chrome 操作体系,但也可以在 Linux 和 Windows 平台上运行。Perfetto 能够网络和分析来自多个来源的数据,包罗内核变乱、体系调用、应用程序变乱和用户界说的跟踪数据。
1)自界说跟踪数据
假如用户想自界说跟踪数据,可以添加以下代码块。
  1. import android.os.Trace;
  2. Trace.beginSection("trace tag");
  3. // 需要跟踪的代码逻辑
  4. Trace.endSection();
复制代码
2)抓 Trace
在【开辟者选项-体系跟踪】中打开【恒久跟踪】、【录制跟踪记载1】,将开启跟踪,接着开始操作手机复现 Bug,Bug 复现后点击【录制跟踪记载1】结束跟踪,trace 文件保存在 /data/local/traces/ 下面,以 .perfetto 为扩展名。
将 trace 文件拖拽到 https://ui.perfetto.dev 网页,就可以查看 trace 信息。
4 拓展

(1)批量 push
有时需要批量 push 某文件夹下的带特定后缀的文件,这时可以使用如下脚本:
  1. set source_path=E:\android\jar\
  2. set target_path=/system/framework/
  3. cd %source_path%
  4. for %%s in (*.jar) do (
  5.     adb push %%s %target_path%
  6. )
  7. pause
复制代码
注意:以上代码需放在 bat 脚本文件中,若在命令行中输入以上命令,则将 %%s 改为 %s。
(2)追加属性
  1. adb pull hw_product/etc/prop/local.prop
  2. echo xxx_prop = true >> local.prop
  3. adb push local.prop hw_product/etc/prop/local.prop
复制代码
(3) 模仿点击
  1. @echo off
  2. setlocal EnableDelayedExpansion
  3. set /a loop_times=10
  4. adb remount
  5. for /l %%i in (1,1,!loop_times!) do (
  6.     echo **********%%i**********
  7.     call :loop
  8. )
  9. pause
  10. ::循环体
  11. :loop
  12.     adb shell input tap 500 500
  13.     :wait 3
  14.     adb shell input swipe 500 1000 500 0
  15.     :wait 3
  16.     call :screenshot
  17.     :wait 2
  18. goto:eof
  19. ::截屏
  20. :screenshot
  21.     adb shell screencap -p /sdcard/1.png
  22.     adb pull /sdcard/1.png 1.png
  23.     set date_temp=%date:~0,10%
  24.     set time_temp=%time:~0,12%
  25.     set time_temp=%time_temp::=-%
  26.     set time_temp=%time_temp:.=-%
  27.     set t=%date_temp:/=-%_%time_temp: =0%
  28.     echo %t%
  29.     set filename=%t%.png
  30.     ren 1.png %filename%
  31. goto:eof
  32. ::等待
  33. :wait
  34.     choice /T %1 /C ync /CS /D y /n
  35.     ::ping 127.0.0.1 -n 1 >nul
  36. goto:eof
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

北冰洋以北

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表