盘点Python编程的简易版自动化工具——ADB史上全操作

开发 后端 自动化
ADB,中文名安卓调试桥,它是一种功能多样的命令行工具,可用于执行各种设备操作(例如安装和调试应用),并提供对 Unix shell(可用来在设备上运行各种命令)的访问权限。

[[397623]]

前言

ADB,中文名安卓调试桥,它是一种功能多样的命令行工具,可用于执行各种设备操作(例如安装和调试应用),并提供对 Unix shell(可用来在设备上运行各种命令)的访问权限。它是一种客户端-服务器程序,包括以下三个组件:

客户端:用于发送命令。客户端在开发计算机上运行。您可以通过发出 ADB命令来从命令行终端调用客户端。

守护进程:在设备上运行命令。守护进程在每个设备上作为后台进程运行。

服务器:管理客户端和守护进程之间的通信。服务器在开发机器上作为后台进程运行。

通过这个命令行工具我们可以使用它来操作手机上的资源,而且不需要借助Android SDK就可以轻松操控手机里的应用。

一、工作原理

当您启动某个ADB客户端时,客户端会先检查是否有 ADB服务器进程正在运行。如果没有,它将启动服务器进程。服务器在启动后会与本地 TCP 端口 5037 绑定,并监听 ADB 客户端发出的命令 - 所有 ADB客户端均通过端口 5037 与 ADB 服务器通信。

然后,服务器会与所有正在运行的设备建立连接。它通过扫描 5555 到 5585 之间(该范围供前 16 个模拟器使用)的奇数号端口查找模拟器。服务器一旦发现ADB守护进程 (ADBD),便会与相应的端口建立连接。请注意,每个模拟器都使用一对按顺序排列的端口 - 用于控制台连接的偶数号端口和用于ADB 连接的奇数号端口。

二、下载ADB

这里小编已经给大家准备好了,下载地址:https://u062.com/file/7715018-452566390,如果你想在任何文件目录使用ADB的话,必须把ADB的安装目录加入环境变量,然后我们可以使用ADB的界面工具,它可以很直观方便的执行ADB的命令并显示结果,这个工具就是APKInstaller,下载地址:https://u062.com/file/7715018-452566398,如图:

三、基本操作

1.设备的启动关闭操作

这里我选择连接模拟器,看看连接有什么区别。

1).连接设备

  1. adb connect 127.0.0.1:62001#连接一个模拟器 

2).启动服务

  1. adb start-server#开启服务 

3).查看设备

  1. adb devices #查看设备信息 
  2. adb version #设备版本 
  3. adb help #帮助文档  
  4. adb get-state #查看设备状态 device(正常连接),offline(连接异常),unknown(没有连接) 
  5. adb get-serialno #获取序列号 
  6. adb get-devpath #获取设备路径 
  7. adb shell cat /system/build.prop #获取设备名称 

4).操作多个设备

  1. adb —a 监听所有网络接口,而不仅仅是localhost 
  2.   -d 使用USB设备(如果多个设备连接错误) 
  3.   -e 使用TCP / IP设备(如果可用多个TCP / IP设备错误) 
  4.   -s 使用给定的序列号(覆盖ANDROID_SERIAL) 
  5.   -t 使用给定设备ID 
  6.   -h adb服务器主机名称(默认= localhost) 
  7.   -p adb服务器的端口(默认= 5037) 
  8.   -l 监听来自套接字的adb服务器(默认= tcp: localhost: 5037) 
  9.   例如:adb -s 127.0.0.1:62001 shell 

这样我们呢就进入了模拟器的Shell界面了。

5).关闭服务

  1. adb kill-server 

6).设置监听TCP/IP的端口

  1. adb tcpip 5555 

7).断开连接

  1. adb disconnect 127.0.0.1:62001 

8).关机与重启

  1. adb reboot #设备重启 
  2. adb shutdown #设备关机 

9).Root权限

  1. adb root   # 正常输出:restarting adbd as root 
  2. adb unroot # 取消root权限 

10).刷机模式

  1. adb reboot bootloader #重启到bootloader,即刷机模式 
  2. adb reboot recovery #重启到recovery,即恢复模式 
  3. adb sideload <path-to-update.zip>#更新系统 

11).命令转载

  1. adb wait-for-device # 在模拟器/设备连接之前把命令转载在adb的命令器中 

2.设备应用操作

1).查看应用

  1. adb shell pm list packages #所有应用 
  2. adb shell pm list package -f # 
  3. adb shell pm list packages -s #系统应用 
  4. adb shell pm list packages -3 #三方应用 
  5. adb shell pm list packages | findstr bluetooth #过滤应用  管道符进行搜索,Linux下使用grep 

2).查看应用的Activity信息

  1. adb shell dumpsys package com.android.bluetooth #查看系统应用蓝牙的包名信息 
  2. adb shell dumpsys activity activities #查看所有的活动程序包名 
  3. adb shell dumpsys activity | findstr mFocusedActivity #查看当前重启的是哪个包 
  4. adb shell dumpsys activity top | findstr activity #查找设备活动程序的父窗口 

3).清除应用数据和缓存

  1. adb shell pm clear 

3.安装卸载应用

  1. adb install D:/aa.apk #安装在电脑上的apk 
  2. adb install -r D:/aa.apk#覆盖安装 保留数据和缓存文件 -g 授予所有运行时权限 
  3. adb shell pm install /scard/picture#安装在手机上的apk 
  4. adb uninstall -k 包名 
  5. 在模拟器或者真机中需要使用 -s来指定 

4.日志

  1. adb logcat#查看日志 
  2. adb logcat -v time #打印详情日志,会记录当前的所有操作行为以及产生的结果,默认持续监听,按下Ctrl+c即可结束 
  3. adb logcat -v time >D:\log.txt #保存日志到电脑 
  4. adb logcat -f /sdcard/1.txt    #保存日志到手机 
  5. adb logcat | findstr com.android.bluetooth #保存指定包名的日志 
  6. adb logcat -c  #清除之前的日志输出 
  7. adb logcat | findstr ActivityManager  #查看当前正在运行的Activity 
  8. adb logcat | findstr Displayed        #查看当前正在运行的Activity 
  9. adb bugreport #查看bug报告 
  10. adb logcat -b radio #无线通讯的日志 
  11. adb shell dmesg #内核日志 

5.设备文件操作

  1. #把电脑上的文件传到手机储存卡中 
  2. adb push C:\Users\Administrator\Desktop\1.gif /sdcard/ 
  3.  
  4. #把手机存储卡里的文件传到电脑 
  5. adb pull /sdcard/1.gif C:\Users\Administrator\Desktop\ 

6.截屏,录屏

  1. adb shell screencap /sdcard/1.png #当前窗口截屏保存到手机 
  2. adb shell /system/bin/screencap -p /sdcard/2.png 
  3. adb exec-out screencap -p >1.png #截图保存到电脑 
  4. adb shell screenrecord >1.mp4  #屏幕录像,Ctrl+c停止录制 
  5. --size #视频大小 
  6. --bit-rate #比特率 
  7. --time-limit #持续时间 
  8. --verbose #命令行显示log信息 
  9. 注:模拟器和安卓4.4以下版本不支持录屏 

7.Shell

Shell里有很多命令,我们简单列举下:

命令 功能
cat 显示文件内容
cd 切换目录
chmod 改变文件的存取模式/访问权限
df 查看磁盘空间使用情况
grep 过滤输出
kill 杀死指定 PID 的进程
ls 列举目录内容
mount 挂载目录的查看和管理
mv 移动或重命名文件
ps 查看正在运行的进程
rm 删除文件
top 查看进程的资源占用情况

1).进入退出

  1. adb shell #进入shell 返回$ 则没有root权限  #有root权限 
  2. exit #退出shell 

2).设备的相关信息

  1. adb shell getprop ro.build.version.release #安卓系统版本 
  2. adb shell getprop ro.product.model #查看设备型号 
  3. adb shell cat /sys/class/net/wlan0/address #查看MAC地址 
  4. adb shell wm size #设备屏幕分辨率 
  5. adb shell wm size 400X654 #设置屏幕分辨率 
  6. adb shell wm size reset #恢复原屏幕分辨率 
  7. adb shell wm density #设备屏幕密度 
  8. adb shell wm density 100 #修改屏幕密度为100dpi 
  9. adb shell wm density reset #恢复原屏幕密度 
  10. adb shell wm overscan 10,20,30,100 #显示区域 
  11. adb shell wm overscan reset #恢复原显示区域 
  12. adb shell dumpsys window displays #显示屏参数 
  13. adb shell service list  #查看后台services信息 
  14. adb shell settings put global adb_enabled 0 #关闭 USB 调试模式 
  15. adb shell uiautomator dump   #获取当前界面的控件信息 
  16. adb shell ime list -s  #设备上的输入法 

3).进程

  1. adb shell ps #查看手机正在运行的进程 adb shell ps | findstr bluetooth  
  2. adb shell ps -x pid #查看指定pid的进程状态信息 
  3. adb shell kill pid #根据进程号杀进程 
  4. adb shell procrank #杀进程  
  5. adb shell start adbd #启动守护进程 
  6. adb shell stop adbd  #关闭守护进程 

4).性能分析

  1. adb shell cat /proc/cpuinfo #获取CPU序列号 
  2. adb shell cat /proc/meminfo #查看当前内存占用 
  3. adb shell cat /proc/iomem #查看IO内存分区 
  4. adb remount #将system分区重新挂载为可读写分区 
  5. adb shell dumpsys meminfo bluetooth #查看蓝牙占用的内存 
  6. adb shell dumpsys cpuinfo | findstr bluetooth #获取CPU 
  7. adb shell top #查看实时资源占用情况 
  8. adb shell top -n 1 | findstr bluetooth #刷新一次内存信息,然后返回蓝牙内存占用 
  9. adb shell top #查看设备cpu和内存占用情况 
  10. adb shell top -m 6 #查看占用内存前6的app 
  11. adb shell dumpsys gfxinfo bluetooth #获取流畅度相关 
  12. adb shell netcfg #查看设备的网络连接情况 
  13. adb shell ifconfig wlan0 #获取wlan0的IP地址和子网掩码 

5).文件操作

  1. adb shell ls #列出目录下的文件和文件夹 
  2. adb shell cd sys #切换当前目录为sys 
  3. adb shell rename 旧文件名 新文件名 #重命名文件名 
  4. adb shell rm /sys/1.apk #删除指定目录下的文件 
  5. adb shell rm -r #删除指定目录下的文件夹及其子目录 
  6. adb shell mv 旧文件名 新文件名 #移动文件 
  7. adb shell chmod 777 1.jpg #设置文件权限 
  8. adb shell mkdir 文件夹名 #新建文件夹 
  9. adb shell cat 文件 #查看文件内容 
  10. adb shell cat /data/misc/wifi/*.conf #查看WiFi密码 

6).按键

  1. adb shell input keyevent 3 # HOME 键 
  2. adb shell input keyevent 4 # 返回键 
  3. adb shell input keyevent 5 # 拨号 
  4. adb shell input keyevent 6 # 挂断 
  5. adb shell input keyevent 24 # 音量+ 
  6. adb shell input keyevent 25 # 音量- 
  7. adb shell input keyevent 26 # 电源键 
  8. adb shell input keyevent 27 # 拍照 
  9. adb shell input keyevent 64 # 打开浏览器 
  10. adb shell input keyevent 82 # 菜单键 
  11. adb shell input keyevent 85 # 播放/暂停 
  12. adb shell input keyevent 86 # 停止播放 
  13. adb shell input keyevent 87 # 播放下一首 
  14. adb shell input keyevent 88 # 播放上一首 
  15. adb shell input keyevent 122 #移动光标到行首或列表顶部  
  16. adb shell input keyevent 123 #移动光标到行尾或列表底部 
  17. adb shell input keyevent 126 # 恢复播放 
  18. adb shell input keyevent 127 # 暂停播放 
  19. adb shell input keyevent 164 # 静音  
  20. adb shell input keyevent 176 # 打开系统设置  
  21. adb shell input keyevent 187 # 切换应用 
  22. adb shell input keyevent 207 # 打开联系人  
  23. adb shell input keyevent 208 # 打开日历 
  24. adb shell input keyevent 209 # 打开音乐 
  25. adb shell input keyevent 210 # 打开计算器  
  26. adb shell input keyevent 220 # 降低屏幕亮度 
  27. adb shell input keyevent 221 # 提高屏幕亮度 
  28. adb shell input keyevent 223 # 休眠 
  29. adb shell input keyevent 224 # 点亮屏幕 
  30. adb shell input keyevent 231 # 打开语音助手 
  31. adb shell input keyevent 276 # 如果没有 wakelock 则让系统休眠 

7).点击,滑动屏幕

  1. adb shell input tap 100 300 #在(100,300)处点击 
  2. adb shell input swipe 100 1200 100 200 #上滑 
  3. adb shell input swipe 100 200 100 1200  #下滑 

8).输入

  1. adb shell input text hello  #输入hello 

9).电池

  1. adb shell dumpsys battery 

10).设备ID

  1. adb shell settings get secure android_id 

11).无线网络

在操作前必须获得Root权限。

  1. adb shell svc wifi enable  #开启WiFi 
  2. adb shell svc wifi disable #关闭WiFi 

8.端口转发

  1. adb forward tcp:60 tcp:70 #将60端口转到70端口 
  2. adb forward tcp:60 local:logd # 将60端口转到local:logd的转发 

9.Activity 管理器

  1. adb shell am start -n activity路径 #启动某一个activity 
  2. adb shell am start -a android.intent.action.VIEW -d www.baidu.com#启动默认浏览器打开一个网页 
  3. adb shell am start -n com.android.camera/.Camera #启动相机 
  4. adb shell am start -a android.intent.action.CALL -d tel:10086#启动拨号10086 
  5. adb shell am startservice -n 服务 #开启服务 
  6. adb shell am stopservice  服务 #停止服务 
  7. adb shell am force-stop bluetooth #杀死蓝牙进程 
  8. adb shell am kill 进程号  #杀掉进程 
  9. adb shell am broadcast -a android.intent.action.BOOT_COMPLETED #向所有组件广播设备启动完毕 

10.调用软件包管理器

  1. adb shell pm list permissions #查看权限 
  2. adb shell pm list permission-groups #输出所有已知的权限组 
  3. adb shell pm list permissions -d -g -f #查看系统危险权限并按组输出所有信息 
  4. adb shell pm list instrumentation#列出所有测试软件包,-f列出测试软件包的APK文件 
  5. adb shell pm path com.android.bluetooth #查看软件安装路径 
  6. adb shell pm list features  #输出系统的所有功能 
  7. adb shell pm list libraries #输出当前设备支持的所有库 
  8. adb shell pm list users #输出系统中的所有用户 
  9. adb shell pm enable ** #启用给定的软件包或组件(写为“package/class”) 
  10. adb shell pm disable ** #停用给定的软件包或组件(写为“package/class”) 
  11. adb shell pm get-max-users #输出设备支持的最大用户数 

11.备份

adb backup -all #备份所有数据

12.压力测试Monkey

  1. 格式:adb shell monkey -v -p your.package.name 500 
  2. adb shell monkey -v -p com.tencent.weishi 500 

四、总结

以上就是ADB的全部内容了,通过对ADB的了解我相信大家应该能通过Python编程做一个简易版的自动化工具了,不再依靠Airtest。

本文转载自微信公众号「IT共享之家」,可以通过以下二维码关注。转载本文请联系IT共享之家公众号。

 

责任编辑:武晓燕 来源: IT共享之家
相关推荐

2010-05-26 16:21:25

2022-07-20 12:18:36

Python自动化工具tox

2016-09-08 15:20:04

JavascriptNodeGulp

2021-05-19 17:04:29

Python阿里自动化工具

2023-12-29 08:31:49

Spring框架模块

2010-12-06 09:56:52

数据中心网络

2017-01-13 08:37:57

PythonAlphaGoMuGo

2021-05-08 09:00:53

AI 工具人工智能

2021-07-12 15:50:55

Go 语言netstat命令

2020-03-31 10:58:35

网络自动化SD-WAN软件定义网络

2024-03-08 13:11:05

前端自动化工具

2024-04-08 11:45:11

Pythonpyinfra编程语言

2019-12-11 11:54:37

IT工具云计算

2023-01-07 23:24:00

python格式化工具

2022-02-11 13:44:56

fiber架构React

2013-12-19 09:56:12

云计算自动化工具云计算管理

2022-10-20 11:49:49

JS动画帧,CSS

2019-12-04 08:00:00

IT基础架构自动化工具

2020-12-23 10:43:40

云计算基础设施自动化工具

2020-04-26 15:31:58

DevOps自动化工具
点赞
收藏

51CTO技术栈公众号