adb介绍和命令使用

一、adb 的概念

ADB 全名 Android Debug Bridge,是一个调试工具。

二、adb 使用

        下载安装Android SDK并配置path路径,在终端输入adb出现如下图所示即安装配置成功,具体安装方法可以在本站搜索。

三、adb 的构成和工作原理

adb包含三个部分:

  • client端:运行在开发机器中,即你的开发电脑,用来发送adb命令;
  • Daemon 守护进程:运行在调试设备中,手机或模拟器,用来接收并执行adb命令;
  • Server端:同样运行在开发机器中,用来管理client端和手机端Daemon之间的通信。

四、adb 常用命令

1、获取包名和界面名

  • 包名(package):决定程序的唯一性(不是应用的名字)
  • 界面名(activity):程序每一个界面对应一个界面名

使用方法:先在模拟器或者手机打开要查看的应用程序,输入对应系统的命令 

命令格式:

  • Mac/Linux:
adb shell dumpsys window windows | grep mFocusedApp
  •  Windows:
adb shell dumpsys window windows | findstr mFocusedApp

实例:打开模拟器的设置程序,在终端输入命令获取包名和界面名

输入:adb shell dumpsys window windows | findstr mFocusedApp

输出:mFocusedApp=AppWindowToken{7db1bd6 token=Token{11ec57b ActivityRecord{cc6790a u0 com.android.settings/.Settings t3}}}
  • 包名: com.android.settings(/ 左边)
  • 界面名:.Settings(/ 右边)

2、文件传输

发送文件到手机:

adb push 电脑的⽂件路径 ⼿机的⽂件夹路径

从手机中拉取文件:

adb pull ⼿机的⽂件路径 电脑的⽂件夹路径

3、获取app启动时间

命令格式:

adb shell am start -W 包名/启动名

实例:使用该命令打开模拟器设置主界面

输入:adb shell am start -W com.android.settings/.Settings

输出:
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.android.settings/.Settings }
Status: ok
Activity: com.android.settings/.Settings
ThisTime: 254
TotalTime: 254
WaitTime: 258
Complete

解释:

  • ThisTime:该界面(activity)启动耗时(毫秒)
  • TotalTime:应用自身启动耗时 = ThisTime + 应用 application 等资源启动时间(毫秒)
  • WaitTime:系统启动应用耗时 = TotalTime + 系统资源启动时间(毫秒)

4、获取手机日志

命令格式:

adb logcat

使用步骤:

  • 打开需要测试的应用程序
  • 找到触发bug的位置
  • 使用日志查看命令
  • 触发bug
  • 获取日志信息(日志中大写E开头的信息是报错信息)

5、获取app占用手机CPU情况

命令格式:

# 获取瞬时数据
adb shell top | findstr 包名

# 获取一段时间内的数据
adb shell dumpsys cpuinfo findstr 包名

实例:使用命令打开模拟器设置主界面

6、获取app占用手机内存情况

命令格式:

# 获取所有应用的内存占用情况
adb shell dumpsys meminfo

# 获取指定APP的内存占用详细情况
adb shell dumpsys meminfo 包名

实例:使用命令打开模拟器设置主界面

7、获取app使用流量情况

命令格式:

# 获取当前APP的进程号PID
adb shell ps | findstr 包名

# 根据进程号获取流量信息
adb shell cat /proc/{pid}/net/dev

实例:使用命令打开模拟器设置主界面

8、获取手机电量数据

命令格式:

# 查看手机的电池信息
adb shell dumpsys battery

# 关闭USB充电模式(同理可以设置其他信息)
adb shell dumpsys battery set usb 0    # 0为关闭 1为开启

# 查看手机的电池详细信息
adb shell dumpsys batterystats

# 重置电池详情信息
adb shell dumpsys batterystats --reset

实例:

9、其他命令

命令 说明
adb install 路径/xx.apk Install the app to the mobile phone (note the format of the installation package)
adb uninstall packagename Uninstall the app on the phone, you need to specify the package name
adb devices Obtain the device connected to the current computer and the corresponding device number
adb shell Enter the command line of the linux system inside the Android phone
adb start-server Start the adb server, use it to restart the server when there is a bug, close it first and then start it
adb kill-server Stop the adb server, use it to restart the server when there is a bug, close it first and start it again
adb --help View adb help, you can use it when you can’t remember the command

expand:

adb command is called in Python code

import os

os.popen(command)

Notice:

  • command is the adb command
  • The result returned is an object
  • Print command execution results must use loop output

おすすめ

転載: blog.csdn.net/ouihsiad/article/details/127209429