马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1 前言
ADB(Android Debug Bridge)即 Android 调试桥,采用监听 Socket TCP 端口的方式通讯。毗连手机有2种方式:有线毗连、无线毗连。
(1)有线毗连
使用数据线毗连手机后,在【开辟职员选项】中开启【USB 调试】,并在【选择 USB 设置】中选择【MTP (多媒体传输)】。
(2)无线毗连
注意事项:
- 手机和 PC 需要毗连同一个局域网
- 需要开启【USB 调试】,并选择【MTP (多媒体传输)】
若报如下错误:
- cannot connect to 192.168.0.109:5555: 由于目标计算机积极拒绝,无法连接。 (10061)
复制代码 按照如下步骤可以毗连上手机:
- 使用数据线毗连手机和 PC,实行命令:adb tcpip 5555
- 拔掉数据线,开启【USB 调试】,并选择【MTP (多媒体传输)】
- 实行命令:adb connect 192.168.0.109
2 常用命令
2.1 基础命令
- ::查看设备
- adb devices
- ::查看详细设备信息(含型号)
- adb devices -l
-
- ::查看手机信息(品牌、型号、版本等)
- adb shell cat /hw_product/etc/prop/local.prop | findstr product
- adb shell cat /system/build.prop | findstr product
-
- ::重启
- adb reboot
-
- ::查看日志
- adb logcat > log.txt
- adb logcat | findstr "matched_string"
-
- ::安装/卸载
- adb install xxx.apk
- adb uninstall package_name
-
- ::拉取/推送
- adb remount
- adb pull /sdcard/a.mp4 C:\Users\81518\Desktop\
- adb push C:\Users\81518\Desktop\a.mp4 /sdcard/
-
- ::查看当前界面运行的 activity 及其包名
- adb shell dumpsys window | findstr "mCurrentFocus"
-
- ::查看指定命令的参数
- adb command --help
- adb shell command --help
复制代码 打印方法调用堆栈:
- android.util.Log.d("TAG", android.util.Log.getStackTraceString(new Throwable()));
复制代码 2.2 截屏/录制
- ::截屏
- adb shell screencap -p /sdcard/1.png
- adb pull /sdcard/1.png C:\Users\81518\Desktop\
-
- ::录屏
- adb shell screenrecord /sdcard/1.mp4
复制代码 说明:截屏时,可以通过【-d】指定 display id,默认是 0
2.3 查看/修改体系参数
- ::查看系统参数值
- adb shell getprop system_prams
-
- ::设置系统参数值
- adb shell setprop system_prams params_value
-
- ::查看手机型号
- adb shell getprop ro.product.model
- ::查看手机固件构建时间
- adb shell getprop ro.build.date
复制代码 2.4 模仿输入
- ::查看模拟输入命令
- adb shell input --help
-
- ::点击指定坐标位置
- adb shell input tap <x> <y>
-
- ::滑动
- adb shell input swipe <x1> <y1> <x2> <y2>
-
- ::按键事件
- adb shell input keyevent <keycode>
复制代码 说明:通过 -d 属性可以指定 displayId,应用如下:
- ::点击指定display的指定坐标位置
- adb shell input -d 1 tap 100 200
复制代码 常用<keycode>如下:
- 3 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 则让系统休眠
复制代码 2.5 adb shell settings
(1)查看属性
- ::查看系统/全局/安全属性
- adb shell settings list system
- adb shell settings list global
- adb shell settings list secure
-
- ::获取自动锁屏时间(单位:毫秒 ms)
- adb shell settings list system | findstr "screen_off_timeout"
- adb shell settings get system screen_off_timeout
-
- ::获取当前屏幕亮度
- adb shell settings get system screen_brightness
-
- ::获取默认输入法(com.baidu.input_huawei/.ImeService)
- adb shell settings get secure default_input_method
复制代码 (2)设置属性
- ::设置自动锁屏时间(单位:毫秒 ms,此处为:1天)
- adb shell settings put system screen_off_timeout 86400000
-
- ::设置当前屏幕亮度
- adb shell settings put system screen_brightness 100
- ::跳过开机导航
- adb shell settings put global device_provisioned 1 && adb reboot
复制代码 2.6 adb shell am
am 即 ActivityManager,重要用于启动 activity、service,发送 broadcast 等。
- ::打开记事本
- adb shell am start com.huawei.notepad
-
- ::关闭 QQ
- adb shell am force-stop com.tencent.mobileqq
-
- ::拉起服务
- adb shell am startservice -a com.example.test.DemoService
-
- ::发送广播
- adb shell am broadcast -a com.example.text.ACTION
-
- ::恢复出厂设置
- adb shell am broadcast -a android.intent.action.MASTER_CLEAR
复制代码 2.7 adb shell wm
wm 即 WindowManager,重要用于窗口管理。
- ::查看手机分辨率:Physical size: 1080x2340
- adb shell wm size
-
- ::查看物理密度:Physical density: 480
- adb shell wm density
复制代码 2.8 adb shell pm
pm 即 PackageManager,重要作用有:
- 安装、卸载应用
- 查询已安装应用
- 查询、增加、删除 permission
- 查询 Application 相关信息(application、activity、service、receiver、provider及相应属性等)
- 扫除用户数据、缓存等
- ::安装应用(安装包在手机上)
- adb shell pm install package_path
-
- ::卸载应用
- adb shell pm uninstall package_name
-
- ::查询已安装应用包名
- adb shell pm list packages [-s:系统包] [-3:三方包]
-
- ::获取指应用的apk路径
- adb shell pm path com.example.test
-
- ::获取指定应用的安装路径
- adb shell pm get-install-location
-
- ::授予权限
- adb shell pm grant com.example.test android.permission.READ_EXTERNAL_STORAGE
-
- ::撤销权限
- adb shell pm revoke com.example.test android.permission.WRITE_EXTERNAL_STORAGE
-
- ::列出所有用户
- adb shell pm list users
-
- ::创建用户
- adb shell pm create-user "user_name"
-
- ::删除有用户
- adb shell pm remove-user user_id
-
- ::清除指定应用的所有数据
- adb shell pm clear com.example.test
复制代码 2.9 历程操作
- ::查看进程
- adb shell ps | findstr "com.tencent.mobileqq"
-
- ::kill进程
- adb shell kill pid
- adb shell am force-stop package_name
-
- ::关闭/启动 adb 服务
- adb kill-server
- adb start-server
复制代码 2.10 adb shell uiautomator
dump 布局
- adb shell uiautomator dump --compressed /data/local/tmp/uidump.xml
- adb pull /data/local/tmp/uidump.xml
复制代码 XML 在线格式化
2.11 dumpsys
(1)adb shell dumpsys activity
- ::查看Activity组件信息
- adb shell dumpsys activity activities
- adb shell dumpsys activity activities | findstr /C:"* Hist"
-
- ::查看Service组件信息
- adb shell dumpsys activity services
-
- ::查看ContentProvider组件信息
- adb shell dumpsys activity providers
-
- ::查看BraodcastReceiver信息
- db shell dumpsys activity broadcasts
-
- ::查看Intent信息
- adb shell dumpsys activity intents
-
- ::查看进程信息
- adb shell dumpsys activity processes
复制代码 (2)adb shell dumpsys window
- ::查看窗口信息
- adb shell dumpsys window windows
- adb shell dumpsys window windows | findstr /C:"Window #"
-
- ::查看屏幕信息
- adb shell dumpsys window displays
-
- ::查看策略信息
- adb shell dumpsys window policy
-
- ::查看动画信息
- adb shell dumpsys window animator
-
- ::查看会话信息
- adb shell dumpsys window sessions
-
- ::查看 tokens 信息
- adb shell dumpsys window tokens
-
- ::查看 trace 信息
- adb shell dumpsys window trace
复制代码 (3)其他
- ::查看输入信息
- adb shell dumpsys input
-
- ::查看内存信息
- adb shell dumpsys meminfo
-
- ::查看电源管理信息
- adb shell dumpsys power
-
- ::查看电池信息,【即当前电量、电池状态、电池温度等】
- adb shell dumpsys battery
-
- ::查看渲染信息
- 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 跟踪
- adb root
- :: 开始抓trace
- adb shell cmd window tracing start
- :: 开始录屏
- adb shell screenrecord /sdcard/screenrecord.mp4
- :: 结束录屏(按 Ctrl + C)
- :: 结束抓trace
- adb shell cmd window tracing stop
- :: 获取录屏文件
- adb pull /sdcard/screenrecord.mp4
- :: 获取trace文件
- adb pull /data/misc/wmtrace/wm_trace.winscope wm_trace.winscope
复制代码 2)SurfaceFlinger 跟踪
- adb root
- :: 开始抓trace
- adb shell service call SurfaceFlinger 1025 i32 1
- :: 开始录屏
- adb shell screenrecord /sdcard/screenrecord.mp4
- :: 结束录屏(按 Ctrl + C)
- :: 结束抓trace
- adb shell service call SurfaceFlinger 1025 i32 0
- :: 获取录屏文件
- adb pull /sdcard/screenrecord.mp4
- :: 获取trace文件
- adb pull /data/misc/wmtrace/layers_trace.winscope
复制代码 3.2 Perfetto
Perfetto 是一个高性能的端到端可视化工具,用于体系性能跟踪和分析。它重要用于 Android 和 Chrome 操作体系,但也可以在 Linux 和 Windows 平台上运行。Perfetto 能够网络和分析来自多个来源的数据,包罗内核变乱、体系调用、应用程序变乱和用户界说的跟踪数据。
1)自界说跟踪数据
假如用户想自界说跟踪数据,可以添加以下代码块。
- import android.os.Trace;
-
- Trace.beginSection("trace tag");
- // 需要跟踪的代码逻辑
- Trace.endSection();
复制代码 2)抓 Trace
在【开辟者选项-体系跟踪】中打开【恒久跟踪】、【录制跟踪记载1】,将开启跟踪,接着开始操作手机复现 Bug,Bug 复现后点击【录制跟踪记载1】结束跟踪,trace 文件保存在 /data/local/traces/ 下面,以 .perfetto 为扩展名。
将 trace 文件拖拽到 https://ui.perfetto.dev 网页,就可以查看 trace 信息。
4 拓展
(1)批量 push
有时需要批量 push 某文件夹下的带特定后缀的文件,这时可以使用如下脚本:
- set source_path=E:\android\jar\
- set target_path=/system/framework/
-
- cd %source_path%
-
- for %%s in (*.jar) do (
- adb push %%s %target_path%
- )
-
- pause
复制代码 注意:以上代码需放在 bat 脚本文件中,若在命令行中输入以上命令,则将 %%s 改为 %s。
(2)追加属性
- adb pull hw_product/etc/prop/local.prop
- echo xxx_prop = true >> local.prop
- adb push local.prop hw_product/etc/prop/local.prop
复制代码 (3) 模仿点击
- @echo off
- setlocal EnableDelayedExpansion
-
- set /a loop_times=10
- adb remount
- for /l %%i in (1,1,!loop_times!) do (
- echo **********%%i**********
- call :loop
- )
- pause
-
- ::循环体
- :loop
- adb shell input tap 500 500
- :wait 3
- adb shell input swipe 500 1000 500 0
- :wait 3
- call :screenshot
- :wait 2
- goto:eof
-
- ::截屏
- :screenshot
- adb shell screencap -p /sdcard/1.png
- adb pull /sdcard/1.png 1.png
- set date_temp=%date:~0,10%
- set time_temp=%time:~0,12%
- set time_temp=%time_temp::=-%
- set time_temp=%time_temp:.=-%
- set t=%date_temp:/=-%_%time_temp: =0%
- echo %t%
- set filename=%t%.png
- ren 1.png %filename%
- goto:eof
-
- ::等待
- :wait
- choice /T %1 /C ync /CS /D y /n
- ::ping 127.0.0.1 -n 1 >nul
- goto:eof
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |