adb功能速查

最近需要弄android调试,把adb功能列于此方便查看。本文内容主要来自以下地址

https://developer.android.com/tools/adb?hl=zh-cn

 

查询设备

adb devices -l

 

将命令发送至特定设备

adb -s [device-name] install helloworld.apk

注意设备名称可以是查询设备时得到的名称。如果有多个可用设备,但只有一个是模拟器,可使用-e选项将命令发送至该模拟器;如果有多个设备,但只连接了一个硬件设备,可使用-d选项将命令发送至该硬件设备。此处install为安装命令,也可以执行其他指令。

 

设置端口转发

adb forward tcp:6100 tcp:7100    # 主机端口6100到设备端口7100的转发
adb forward tcp:6100 local:logd   # 主机端口6100到设备日志

 

文件复制

adb pull remote_path local_path    # 从设备复制某个文件或目录
adb push local_path remote_path    # 将某个文件或目录复制到设备

 

停止adb服务器

adb kill-server

停止后再执行任意adb命令将重新启动adb服务器。

 

发出adb命令

adb [-d | -e | -s serial_number] command

若只有一个设备或模拟器连接,将默认发送命令至该设备;否则需使用-d, -e, -s选项指定发送命令的目标设备。可使用以下命令查看所有支持的adb命令

adb --help

 

发出shell命令

adb [-d | -e | -s serial_number] shell shell_command

也可在设备上启动交互式shell

adb [-d | -e | -s serial_number] shell

android 提供了大多数常见的unix命令行工具。如需查看可用工具列表,可使用以下命令

adb shell ls /system/bin

 

模拟点击

adb shell input tap <x> <y>     # 点击座标(x, y)
adb shell input swipe <x1> <y1> <x2> <y2)     # 从(x1, y1)滑动到(x2, y2)
adb shell input keyevent <keycode>      # 发送按键事件

常用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则让系统休眠

 

调用activity管理器

adb shell am start -a android.intent.action.VIEW

在shell中,activity管理器(am)的语法为

am command

可用的am命令为

start [options] intent     # 启动由intent指定的activity
startservice [options] intent         # 启动由intent指定的service
force-stop package         # 强行停止与package关联的所有进程
kill [options] package     # 中止与package关联的所有进程,此命令仅中止可安全中止且不影响用户体验的进程
kill-all                   # 中止所有后台进程
broadcast [options] intent            # 发出广播intent
instrument [options] component        # 使用Instrumentation实例启动监控,目标component采用package/class格式
profile start process file            # 启动process的性能分析器,将结果写入file
profile stop process       # 停止process的性能分析器
dumpheap [options] process file       # 转储process的堆,写入file
set-debug-app [options] package       # 设置要调试的应用package
clear-debug-app            # 清除之前使用set-debug-app设置的用于调试的软件包
monitor [options]          # 开始监控崩溃或ANR
screen-compat {on | off} package      # 控制package的屏幕兼容性模式
display-size [reset | WIDTHxHEIGHT]   # 替换设备显示尺寸
display-density dpi        # 替换设备显示密度
to-uri intent              # 以URI的形式输出给定的intent规范
to-intent-uri intent       # 以intent:uri的形式输出给定的intent规范

 

调用软件包管理器(pm)

adb shell pm uninstall com.example.myapp

在shell中pm的语法为

pm command

可用的pm命令如下

list packages [options] filter     # 输出所有的软件包,或输出包含filter中文字的软件包
list permission-groups             # 输出所有已知的权限组
list permissions [options] group   # 输出所有已知的权限,或仅输出group的权限
list instrumentation [options]     # 列出所有测试软件包
list features                      # 输出系统的所有功能
list libraries                     # 输出当前设备支持的所有库
list users                         # 输出系统中的所有用户
path package                       # 输出给定package的apk路径
install [options] apk_path         # 将软件包安装到系统
uninstall [options] package        # 移除软件包
clear package                      # 删除与软件包关联的所有数据
enable package_or_component        # 启用给定的软件包或组件(package/class形式)
disable package_or_component       # 金庸给定的软件包或组件(package/class形式)
disable-user --user [user_id] package_or_component      # 禁用用户对包或组件的访问
grant package_name permission      # 向应用授予权限
revoke package_name permission     # 从应用撤销权限
set-install-location location      # 更改默认安装位置。0:自动-让系统决定 1:内部-安装在内部存储空间  2:外部-安装在外部介质
get-install-location               # 返回当前安装位置,含义同上
set-permission-enforced permission [true | false]       # 指定是否应强制执行指定权限
trim-caches desired_free_space     # 减少缓存文件以达到给定的可用空间
create-user user_name              # 创建新用户
remove-user user_id                # 删除用户
get-max-users                      # 输出设备支持的最大用户数
get-app-links [options] [package]  # 输出给定package的域名验证状态,若未给定则输出所有包的域名验证状态
reset-app-links [options] [package]                     # 重置给定软件包的域名验证状态
verify-app-links [--re-verify] [package]                # 广播给定package的域名验证请求
set-app-links [--package package] state domains         # 设置软件包的域名状态
set-app-links-user-selection --user user_id [--package package] enabled domains     # 设置用户针对软件包的域名状态
set-app-links-allowed --user user_id [--package package] allowed                    # 切换软件包的自动验证链接处理
get-app-link-owners --user user_id [--package package] domains                      # 为给定用户输出特定域名的所有者

 

调用设备管理器(dpm)

adb shell dpm command

在shell中dpm语法为

dpm command

可用的dpm命令如下

set-active-admin [options] component          # 将component设为活跃管理员
set-profile-owner [options] component         # 将component设为活跃管理员,并将其软件包设为现有用户文件所有者
set-device-owner [options] component          # 将component设为活跃管理员,并将其软件包设为设备所有者
remove-active-admin [options] component       # 停用活跃管理员
clear-freeze-period-record                    # 清除设备系统OTA更新冻结期记录
force-network-logs                            # 强制系统让任何现有网络日志随时可供DPC检索
force-security-logs                           # 强制系统向DPC提供安全日志

 

屏幕截图

adb shell screencap /sdcard/screen.png

在shell中screencap语法为

screencap filename

 

录制视频

adb shell screenrecord /sdcard/demo.mp4

在shell中screenrecord语法为

screenrecord [options] filename

按Ctrl+C可停止录制,否则到3分钟或--time-limit所设置的时间限制时录制将自动停止。注意音频不与视频同时录制,且不支持录制时旋转屏幕,若发生旋转可能会导致录制内容被切断。

screenrecord选项如下:

--help                # 显示帮助
--size WIDTHxHeight   # 设置视频分辨率
--bit-rate rate       # 设置码率,默认为4Mbps
--time-limit time     # 设置最长录制时间,单位为秒,默认180
--rotate              # 将输出旋转90度
--verbose             # 在命令行显示日志信息,若不设置则运行时不会显示任何信息

 

读取应用的ART配置文件

从android7.0(API 24)开始,android运行时(ART)会收集已安装应用的执行配置文件,这些配置文件用于优化性能。可检查收集到的配置文件,了解应用启动期间,系统频繁执行了哪些方法和使用了哪些类。

adb shell cmd package dump-profiles package       # 生成文本格式的配置文件信息
adb pull /data/misc/profman/package.prof.txt      # 检索生成的文件

 

重置测试设备

adb shell cmd testharness enable

使用testharness恢复设备时,设备会自动将允许通过当前工作站调试设备的RSA密钥备份在持久性位置,这样重置设备后,工作站可继续调试设备并发出adb指令,无需手动注册新密钥。此外,testharness还会更改以下设备设置:1. 不出现初始设备设置向导;2. 停用锁定屏幕,停用紧急警报,停用账号自动同步,停用系统自动更新;3. 停用预安装的安全应用。

 

以上是adb的一些用法说明。

下面给出自动化测试工具UIAutomator2的一些说明和用法。

本来官方提供了UI Automator viewer用于界面测试,但目前官方标记为obsolete,改为采用espresso了。

一般测试采用python方式更简单方便,故此处介绍UI Automator2。

 

首先是安装

pip3 install --pre -U uiautomator2
pip3 install --pre -U weditor

 

初始化

python3 -m uiautomator2 init   # 安装com.github.uiautomator.test和atx
python3 -m weditor             # 打开weditor

注意需要在手机上打开atx启动uiautomator,此时可在浏览器界面操作连接并dump hierarchy将手机界面显示在浏览器中。

 

控制手机

import uiautomator2 as u2

c = u2.connect('192.168.1.101')    # wifi连接
c = u2.connect_usb('device serial_id')     # usb连接

c.app_install('http://domain.com/xxx.apk')   # 安装应用
c.app_start("app_package_name")              # 打开应用

c.press("recent")                          # 点击 recent 键
c(resourceId="com.alibaba.android.launcher:id/clear_all_recent_image_button").click()   # 点击某按键

# 元素定位的方法
element1 = c(resourceID="abc")
element2 = c(text="abc")
element3 = c(description=value)   # 通过description=value定位控件
element4 = c(className=value)     # 通过className=value定位控件

# 单击控件
element1[index].click()   # 定位控件有多个,通过index指定一个
element2.click()          # 定位控件只有一个
c.click(x, y)      # 点击具体座标

# 双击控件
element3[index].double_click()
element4.double_click()
c.double_click(x, y)     # 双击具体座标

# 长按控件
element1[index].long_click()
element2.long_click()
c.long_click(x, y, duration=0.5)  # 长按具体座标持续0.5秒

# 输入文本
element3[index].set_text("abc")
c.set_fastinput_ime(True)       # 打开输入法
c.send_keys(text)               # 输入文本
c.set_fastinput_ime(False)      # 关闭输入法

# 清除文本
element4.clear_text()

# 获取文本
element4.get_text()

# 手势控制
c.swipe(sx, sy, ex, ey, duration=0.5)  # 滑动
c.drag(sx, sy, ex, ey, duration=0.5)   # 拖拽

# 系统控制
key = {"back", "home", "recent", "power", "volume_up", "volume_down", "volume_mute", "enter"}
c.press(key[index])

# 旋转
orientation = c.orientation  # 获取屏幕方向,值为natural, left, right, upsidedown
c.freeze_rotation()   # 锁定屏幕方向
c.freeze_rotation(True)   # 解除锁定屏幕方向
c.set_orientation("left")   # 设置屏幕方向向左转为横屏
c.set_orientation("natural")   # 设置屏幕为正常状态

# 截屏
image = c.screenshot()
image.save("path.jpg")
c.screenshot("path.jpg")  # 截屏并保存

# 文件交换
c.push(pc_file_path, phone_dir_path)    # 将pc文件复制到设备,若设备目录不存在会自动创建
c.pull(phone_file_path, pc_dir_path)    # 将设备文件复制到pc端

c.app_stop("app_package_name")               # 关闭应用
c.app_stop_all()                           # 关闭所有应用

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章