ADB详细(从下载安装到灵活运用)

ADB下载

Windows版本:https://dl.google.com/android/repository/platform-tools-latest-windows.zip

Mac版本:https://dl.google.com/android/repository/platform-tools-latest-windows.zip

Linux版本:https://dl.google.com/android/repository/platform-tools-latest-linux.zip

安装步骤

解压文件夹

配置环境变量

在环境变量中添加adb路径。

验证是否安装成功

win+r输入cmd

输入adb 

输入adb --version

 什么是ADB

ADB(Android Debug Bridge)是一种允许模拟器或已连接的 Android 设备进行通信的命令行工具,它可为各种设备操作提供便利,如安装和调试应用,并提供对 Unix shell(可用来在模拟器或连接的设备上运行各种命令)的访问。可以在Android SDK/platform-tools中找到 adb 工具或下载ADB Kits。

ADB用处

ADB是Android SDK里的一个工具,用这个工具可以直接操作管理Android模拟器或者真实的Android设备。它的主要功能有:

1.在设备上运行Shell命令;将本地APK软件安装至模拟器或Android设备;

2.管理设备或手机模拟器上的预定端口;

3.在设备或手机模拟器上复制或粘贴文件。

ADB是一个客户端-服务器程序,包含三个组件:

客户端:该组件发送命令。客户端在开发计算机上运行。可以通过发出adb命令从命令行终端调用客户端。

后台程序:该组件在设备上运行命令。后台程序在每个模拟器或设备实例上作为后台进程运行。

服务器:该组件管理客户端和后台程序之间的通信。服务器在开发计算机上作为后台进程运行。

ADB命令语法

adb命令的基础语法:

adb [-d|-e|-s <serial-number>] <command>

单一设备/模拟器连接

如果只有一个设备/模拟器连接时,可以省略掉[-d|-e|-s <serial-number>]这一部分,直接使用adb <command>

多个设备/模拟器连接

如果有多个设备/模拟器连接,则需要为命令指定目标设备,下表是指定目标设备的命令:

参数含义
-d指定当前唯一通过USB连接的Android设备为命令目标
-e指定当前唯一运行的模拟器为命令目标
-s<serial-number>指定相应设备序列号的设备/模拟器为命令目标
在多个设备/模拟器连接的情况下较常用的是-s <serial-number>参数,serial-number是指设备的序列号,可以通过adb devices命令获取

ADB常用命令

基本命令

查看adb的版本信息

adb version

启动adb

adb start-server

一般不需要执行adb start-server命令,因为在运行adb命令时如果发现adb server没有启动会自动调用。

停止adb

adb kill-server

使用root权限运行adb

adb root

指定adb server的网络端口

adb -P <port> start-server

ADB的默认端口号为5037。

查询已连接的设备/模拟器列表

adb devices

设备连接管理

USB连接

通过USB连接使用ADB需要以下步骤:

  1. 确定硬件状态正常(包括Android设备处于正常开机状态,USB连接线和各种接口完好)。
  2. Android设备的开发者选项和USB调试模式已经开启(在[设置]-[开发者选项]-[USB调试]打开USB调试)。
  3. 确认设备驱动状态正常(安装ADB驱动程序)。
  4. 通过USB线连接点好和设备后确认状态。
  5. 通过adb devices命令查看设备连接情况。

WIFI连接(需要USB线)

借助USB通过WIFI连接来正常使用ADB需要一下步骤:

  1. 将Android设备与要运行adb的电脑连接到同一个WIFI。
  2. 将设备与电脑通过USB线连接(可以通过adb devices命令查看设备连接情况)。
  3. 通过adb tcpip 5555命令让设备在5555端口监听TCP/IP连接。
  4. 断开USB连接。
  5. 找到设备的IP地址(可以在[设置]-[关于手机]-[状态信息]-[IP地址]查看IP地址)。
  6. 通过adb connect <device-ip-address>命令使用IP地址将Android设备与电脑连接。
  7. 通过adb devices命令查看设备连接情况。
  8. 使用完毕后可通过adb disconnect <device-ip-address>命令断开无线连接。

WIFI连接(无需USB线)

注意:需要使用root权限。

不借助USB通过WIFI连接来使用ADB需要以下步骤:

        1.在Android设备上安装一个终端模拟器(可以通过Terminal Emulator for Android Downloads下载)。

        2.将Android设备与要运行adb的电脑连接到同一个WiFI。

        3.打开Android设备上的终端模拟器,依次运行命令:

su
setprop service.adb.tcp.port 5555

        4.找到设备的IP地址([设置]-[关于手机]-[状态信息]-[IP地址])。

        5.通过adb connect <device-ip-address>命令使用IP地址将Android设备与电脑连接。

        6.通过adb devices命令查看设备连接情况。 

WIFI连接转为USB连接

通过adb usb命令以USB模式重新启动ADB:

adb usb

 注意:如果之前通过WIFI连接需要先断开连接(adb disconnect <device-ip-address>),再执行adb usb才可以。

应用管理

查看应用列表

查看应用列表的基本命令格式是:

adb shell pm list packages [-f] [-d] [-e] [-s] [-3] [-i] [-u] [--user USER_ID] [FILTER]

adb shell pm list packages后面可以跟一些可选参数进行过滤。 

参数显示列表
显示所有应用
-f显示应用关联的apk文件
-d显示disabled的应用
-e显示enabled的应用
-s显示系统应用
-3显示第三方应用
-i显示应用的installer
-u显示已卸载应用
<filter>包名包含<filter>字符串

查看所有应用

adb shell pm list packages

 查看第三方应用

adb shell pm list packages -3

根据包名模糊查询

比如我们要查询包含xiaomi字符串的应用列表:

adb shell pm list packages xiaomi

安装应用

安装应用的基本命令格式是:

adb install [-l] [-r] [-t] [-s] [-d] [-g] <apk-file>

adb install后面可以跟一些参数来控制安装APK的行为: 

参数含义
-l将应用安装到保护目录/mnt/asec
-r允许覆盖安装
-t运行安装AndroidManifest.xml里application指定android:testOnlt="true"的应用
-s将应用安装到sdcard
-d允许降级覆盖安装
-g授予所有运行时权限

运行命令后可以看到输出内容,包含安装进度和状态:

Success:代表安装成功。

Failure:代表安装是啊比。APK安装失败的情况有很多,Failure状态之后会有安装失败输出代码。常见的安装失败输代码、含义和解决办法:

输出代码含义解决办法
INSTALL_FAILED_ALREADY_EXISTS应用已经存在,或卸载了但没卸载干净adb install 时使用 -r 参数,或者先 adb uninstall 再安装
INSTALL_FAILED_INVALID_APK无效的 APK 文件
INSTALL_FAILED_INVALID_URI无效的 APK 文件名确保 APK 文件名里无中文
INSTALL_FAILED_INSUFFICIENT_STORAGE
No space left on device
空间不足清理空间
INSTALL_FAILED_DUPLICATE_PACKAGE已经存在同名程序
INSTALL_FAILED_NO_SHARED_USER请求的共享用户不存在
INSTALL_FAILED_UPDATE_INCOMPATIBLE以前安装过同名应用,但卸载时数据没有移除;或者已安装该应用,但签名不一致先 adb uninstall 再安装
INSTALL_FAILED_SHARED_USER_INCOMPATIBLE请求的共享用户存在但签名不一致
INSTALL_FAILED_MISSING_SHARED_LIBRARY安装包使用了设备上不可用的共享库
INSTALL_FAILED_REPLACE_COULDNT_DELETE替换时无法删除
INSTALL_FAILED_DEXOPTdex 优化验证失败或空间不足
INSTALL_FAILED_OLDER_SDK设备系统版本低于应用要求
INSTALL_FAILED_CONFLICTING_PROVIDER设备里已经存在与应用里同名的 content provider
INSTALL_FAILED_NEWER_SDK设备系统版本高于应用要求
INSTALL_FAILED_TEST_ONLY应用是 test-only 的,但安装时没有指定 -t 参数
INSTALL_FAILED_CPU_ABI_INCOMPATIBLE包含不兼容设备 CPU 应用程序二进制接口的 native code
INSTALL_FAILED_MISSING_FEATURE应用使用了设备不可用的功能
INSTALL_FAILED_CONTAINER_ERROR1. sdcard 访问失败;2. 应用签名与 ROM 签名一致,被当作内置应用。1. 确认 sdcard 可用,或者安装到内置存储;2. 打包时不与 ROM 使用相同签名。
INSTALL_FAILED_INVALID_INSTALL_LOCATION1. 不能安装到指定位置;2. 应用签名与 ROM 签名一致,被当作内置应用。1. 切换安装位置,添加或删除 -s 参数;2. 打包时不与 ROM 使用相同签名。
INSTALL_FAILED_MEDIA_UNAVAILABLE安装位置不可用一般为 sdcard,确认 sdcard 可用或安装到内置存储
INSTALL_FAILED_VERIFICATION_TIMEOUT验证安装包超时
INSTALL_FAILED_VERIFICATION_FAILURE验证安装包失败
INSTALL_FAILED_PACKAGE_CHANGED应用与调用程序期望的不一致
INSTALL_FAILED_UID_CHANGED以前安装过该应用,与本次分配的 UID 不一致清除以前安装过的残留文件
INSTALL_FAILED_VERSION_DOWNGRADE已经安装了该应用更高版本使用-d参数
INSTALL_FAILED_PERMISSION_MODEL_DOWNGRADE已安装 target SDK 支持运行时权限的同名应用,要安装的版本不支持运行时权限
INSTALL_PARSE_FAILED_NOT_APK

指定路径不是文件,或不是以.apk结尾

INSTALL_PARSE_FAILED_BAD_MANIFEST无法解析的 AndroidManifest.xml 文件
INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION解析器遇到异常
INSTALL_PARSE_FAILED_NO_CERTIFICATES安装包没有签名
INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES已安装该应用,且签名与 APK 文件不一致先卸载设备上的该应用,再安装
INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING解析 APK 文件时遇到 CertificateEncodingException
INSTALL_PARSE_FAILED_BAD_PACKAGE_NAMEmanifest 文件里没有或者使用了无效的包名
INSTALL_PARSE_FAILED_BAD_SHARED_USER_IDmanifest 文件里指定了无效的共享用户 ID
INSTALL_PARSE_FAILED_MANIFEST_MALFORMED解析manifest文件时遇到结构性错误
INSTALL_PARSE_FAILED_MANIFEST_EMPTY在 manifest 文件里找不到找可操作标签(instrumentation 或 application)
INSTALL_FAILED_INTERNAL_ERROR因系统问题安装失败
INSTALL_FAILED_USER_RESTRICTED用户被限制安装应用
INSTALL_FAILED_DUPLICATE_PERMISSION应用尝试定义一个已经存在的权限名称
INSTALL_FAILED_NO_MATCHING_ABIS应用包含设备的应用程序二进制接口不支持的 native code
INSTALL_CANCELED_BY_USER应用安装需要在设备上确认,但未操作设备或点了取消在设备上同意安装
INSTALL_FAILED_ACWF_INCOMPATIBLE应用程序与设备不兼容
INSTALL_FAILED_TEST_ONLYAPK 文件是使用 Android Studio 直接 RUN 编译出来的文件通过 Gradle 的 assembleDebug 或 assembleRelease 重新编译,或者 Generate Signed APK
does not contain AndroidManifest.xml
is not a valid zip file
无效的 APK 文件
Permission denied … sdcard …sdcard不可用
Offline设备未连接成功先将设备与 adb 连接成功
unauthorized设备未授权允许调试
error: device not found没有连接成功的设备先将设备与 adb 连接成功
protocol failure设备已断开连接先将设备与 adb 连接成功
Unknown option: -sAndroid 2.2 以下不支持安装到 sdcard不使用 -s 参数
signatures do not match the previously installed version; ignoring!已安装该应用且签名不一致先卸载设备上的该应用,再安装

参考GITHUB上的platform_frameworks_base/core/java/android/content/pm/PackageManager.java at master · aosp-mirror/pl

adb install 实际时分三步完成:

  1. push apk文件到/data/local/tmp

  2. 调用pm install安装。

  3. 删除/data/local/tmp下的对应apk文件。

卸载应用

卸载应用的基本命令格式时:

adb uninstall [-k] <package-name>

<package-name>表示应用的包名,-k参数可选,表示卸载应用但保留数据和缓存数据。

清楚应用数据与缓存

adb shell pm clear <package-name>

<package-name>表示应用包名,这条命令的效果相当于在设置里的应用信息界面点击了[清除缓存]和[清除数据]。

查看前台的Activity

adb shell dumpsys activity activities | grep mFocusedActivity

查看正在运行的Services

adb shell dumpsys activity services [<package-name>]

<package-name>参数不是必须的,指定<package-name>表示查看与某个包名相关的Services,不指定表示查看所有Services。<package-name>不一定要给出完整的包名,可以只给一部分,那么所给包名相关的Services都会列出来。

查看应用详细信息

adb shell dumpsys package <package-name>

<package-name>表示应用包名。运行次命令的输出中包含很多信息,包括Activity Resolver Table、Registered Content Providers、包名、userid、安装后的文件资源代码等路径、版本信息、权限信息、授权状态和签名版本信息等。

查看应用安装路径

adb shell pm path <package-name>

C:\Users\Mi>adb shell pm path com.android.calendar

package:/data/app/MIUICalendar/base.apk

与应用交互

与应用交互主要是使用 am <command> 命令,常用的 <command> 如下:

command

用途

start [options] <intent>

启动<intent>指定的Activity

Startservice [options] <intent>

启动<intent>指定的Service

Broadcast [options] <intent>

发送<intent>指定的广播

Force-stop <package-name>

停止<package-name> 相关的进程

<intent> 参数很灵活,和写 Android 程序时代码里的 Intent 相对应。

用于决定 intent 对象的选项如下:

参数

含义

-a <action>

指定action,比如android.intent.action.VIEW

-c <category>

指定category,比如Android.intent.category.APP_CONTACTS

-n <component>

指定完整component名,用于明确指定启动哪个activity

<intent> 里还能带数据,就像写代码时的 Bundle 一样:

参数

含义

--esn <extra-key>

null 值(只有 key 名)

`-e

–es `

--ez <extra-key> <extra-boolean-value>

boolean 值

--ei <extra-key> <extra-int-value>

integer 值

--el <extra-key> <extra-long-value>

long 值

--ef <extra-key> <extra-float-value>

float 值

--eu <extra-key> <extra-uri-value>

URI

--ecn <extra-key> <extra-component-name-value>

component name

--eia <extra-key> <extra-int-value>[,<extra-int-value...]

integer 数组

启动应用/ 调起 Activity

adb shell am start [options] <intent>

例如:

adb shell am start -a android.settings.SETTINGS                   # 打开系统设置页面
adb shell am start -a android.intent.action.DIAL -d tel:10086     # 打开拨号页面
adb shell am start -n com.android.mms/.ui.ConversationList        # 打开短信会话列表

options时一些改变其行为的选择,支持的可选参数及含义如下:

选项

含义

-D

启用调试

-W

等待启动完成

–start-profiler file

启动分析器并将结果发送到 file

-P file

类似于 --start-profiler,但当应用进入空闲状态时分析停止

-R count

重复 Activity 启动次数

-S

启动 Activity 前强行停止目标应用

–opengl-trace

启用 OpenGL 函数的跟踪

–user user_id

current

调用Service

adb shell am startservice [options] <intent>

一个典型的用例是如果设备上原本应该显示虚拟按键但是没有显示,可以试试这个:

adb shell am startservice -n com.android.systemui/.SystemUIService

停止Service

adb shell am stopservice [options] <intent>

发送广播

adb shell am broadcast [options] <INTENT>

可以向所有组件广播,也可以只向指定组件广播。

例如,向所有组件广播BOOT_COMPLETED:

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

又例如,只向com.android.receiver.test/.BootCompletedReceive广播BOOT_COMPLETED

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -n com.android.receiver.test/.BootCompletedReceiver

这类用法在测试的时候很实用,比如某个广播的场景很难制造,可以考虑通过这种方式来发送广播。 既能发送系统预定义的广播,也能发送自定义广播。如下是部分系统预定义广播及正常触发时机:

action

触发时机

android.net.conn.CONNECTIVITY_CHANGE

网络连接发生变化

android.intent.action.SCREEN_ON

屏幕点亮

android.intent.action.SCREEN_OFF

屏幕熄灭

android.intent.action.BATTERY_LOW

电量低,会弹出电量低提示框

android.intent.action.BATTERY_OKAY

电量恢复了

android.intent.action.BOOT_COMPLETED

设备启动完毕

android.intent.action.DEVICE_STORAGE_LOW

存储空间过低

android.intent.action.DEVICE_STORAGE_OK

存储空间恢复

android.intent.action.PACKAGE_ADDED

安装了新的应用

android.net.wifi.STATE_CHANGE

WiFi连接状态发生变化

android.net.wifi.WIFI_STATE_CHANGED

WiFi状态变为启用/关闭/正在启动/正在关闭/未知

android.intent.action.BATTERY_CHANGED

电池电量发生变化

android.intent.action.INPUT_METHOD_CHANGED

系统输入法发生变化

android.intent.action.ACTION_POWER_CONNECTED

外部电源连接

android.intent.action.ACTION_POWER_DISCONNECTED

外部电源断开连接

android.intent.action.DREAMING_STARTED

系统开始休眠

android.intent.action.DREAMING_STOPPED

系统停止休眠

android.intent.action.WALLPAPER_CHANGED

壁纸发生变化

android.intent.action.HEADSET_PLUG

插入耳机

android.intent.action.MEDIA_UNMOUNTED

卸载外部介质

android.intent.action.MEDIA_MOUNTED

挂载外部介质

android.os.action.POWER_SAVE_MODE_CHANGED

省电模式开启

(以往广播均可以使用adb触发)

强制停止应用

adb shell am force-stop <packagename>

收紧内存

adb shell am send-trim-memory <pid> <level>

参数说明:

pid:进程ID

level:HIDDEN、RUNNING_MODERATE、BACKGROUND、RUNNING_LOW、MODERATE、RUNNING_CRITICAL、COMPLETE

文件管理

从模拟器/设备下载指定的文件到计算机的基本命令格式

adb pull <remote> [local]

参数说明:

  • remote:模拟器/设备里的文件路径

  • Local:计算机上的目录,参数可以省略,默认复制到当前目录

例如,将/sdcard/gao.mp4下载到计算的当前目录(目录需要存在):

adb pull /sdcard/music/mp4

将指定的文件从计算机上传到模拟器/设备

adb push <local> <remote>

参数说明:

  • local:计算机上的文件路径

  • remote:模拟器/设备里的目录

例如,将D:\android-studio\bin下载到设备的/sdcard/music/目录:

adb push D:\android-studio\bin /sdcard/music/

列出指定目录的内容

adb shell ls [options] <directory>

<directory>表示指定目录,可以省略,表示列出根目录下的所有文件和目录。

adb shell ls后面可以跟一些可选参数进行过滤查看不同的列表,可用参数以及含义如下:

参数

显示列表

列出目录下的所有文件和目录

-a

列出目录下的所有文件(包含隐藏的)

-i

列出目录下的所有文件和索引编号

-s

列出目录下的所有文件和文件大小

-n

列出目录下的所有文件及其UID和GID

-R

列出目录下的所有子目录中的文件

切换到目标目录

adb shell 
cd <directory>

第一步:执行adb shell 命令;

第二步:执行cd <directory>命令切换到目标目录。

删除文件或目录

adb shell 
rm [options] <files or directory>

第一步:执行adb shell命令;

第二步:执行rm [options] <files or directory>命令删除文件或目录。

rm后面可以跟一些可选参数进行不同的操作,可用参数及含义如下:

参数

含义

删除文件

-f

强制删除文件,系统不提示

-r

强制删除指定目录中的所有文件和子目录

-d

删除指定目录,即使它是一个非空目录

-i

交互式删除,删除前提示

rm -d等同于rmdir命令,有些版本不包含-d参数。

创建目录

adb shell 
mkdir [options] <directory-name>

第一步:执行adb shell命令;

第二步:执行mkdir [options] <directory-name>命令创建目录。

mkdir后面可以跟一些可选参数进行不同的操作,可用参数及含义如下:

参数

含义

创建指定目录

-m

创建指定目录并赋予读写权限

-p

创建指定目录及其父目录

创建空文件或改变文件时间戳

adb shell 
touch [options] <file>

第一步:执行adb shell命令;

第二部:执行touch [options] <file>命令创建空文件或改变文件时间戳。

可以通过ls -n <directory>命令查看文件的时间。

输出当前目录路径

adb shell pwd
或者
adb shell
pwd

复制文件和目录

adb shell
cp [options] <source> <dest>

参数说明:

source:源文件路径

dest:目标文件路径

移动或重命名文件

adb shell
mv [options] <source> <dest>

参数说明:

source:源文件路径

dest:目标文件路径

网络管理

查看网络统计信息

adb shell netstat

也可以将网络统计信息输出到指定文件:

adb shell netstat><file-path>

例如,可以通过adb shell netstat>D:\netstat.log将日志输出到D:\netstat.log中。

测试两个网络间的连接和延迟

ping命令的格式如下:

adb shell ping [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
[-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
[-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
[-w deadline] [-W timeout] [hop1 ...] destination

例如,ping一个域名:

adb shell ping www.google.com

不结束的话会一直ping下去,可以按 Ctrl + C 停止ping操作。

也可以指定ping的次数:

adb shell ping -c 4 www.google.com

通过配置文件配置和管理网络连接

netcfg命令的格式如下:

adb shell netcfg [<interface> {dhcp|up|down}]

输出示例:

rmnet_ims10 DOWN                                   0.0.0.0/0   0x00001002
rmnet_ims00 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun04 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun03 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun02 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun01 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun00 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun14 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun13 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun12 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun11 DOWN                                   0.0.0.0/0   0x00001002
rmnet_tun10 DOWN                                   0.0.0.0/0   0x00001002
rmnet1   DOWN                                   0.0.0.0/0   0x00001002
rmnet0   DOWN                                   0.0.0.0/0   0x00001002
rmnet4   DOWN                                   0.0.0.0/0   0x00001002
rmnet3   DOWN                                   0.0.0.0/0   0x00001002
rmnet2   DOWN                                   0.0.0.0/0   0x00001002
rmnet6   DOWN                                   0.0.0.0/0   0x00001002
rmnet5   DOWN                                   0.0.0.0/0   0x00001002
dummy0   UP                                     0.0.0.0/0   0x000000c3
rmnet_r_ims10 DOWN                                   0.0.0.0/0   0x00001002
rmnet_r_ims00 DOWN                                   0.0.0.0/0   0x00001002
rmnet_emc0 DOWN                                   0.0.0.0/0   0x00001002
lo       UP                                   127.0.0.1/8   0x00000049
sit0     DOWN                                   0.0.0.0/0   0x00000080
wlan0    UP                                 10.0.38.176/23  0x00001043

这个命令是用来查看和配置Android设备的网络接口的。下面是一些常用的选项:

参数

含义

不带参数

显示所有网络接口的状态信息

<interface>

指定要显示或配置的网络接口名称

dhcp

为指定的网络接口启用DHCP协议

up

启用指定的网络接口

down

禁用指定的网络接口

显示、操作路由、设备、策略路由和隧道

ip命令的格式如下:

adb shell ip [ options ] object
  • options := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve]|-f[amily] { inet | inet6 | ipx | dnet | link } |-l[oops] {maximum-addr-flush-attempts } |-o[neline] | -t[imestamp] | -b[atch][filename] |-rc[vbuf] [size]}

  • object := { link | addr | addrlabel | route | rule | neigh | ntable|tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |netns |l2tp }

Options是一些修改ip行为或改变其输出的选项。所有的选项都是以-字符开头,分为长、短两种格式,支持的可选参数及含义如下:

选项

含义

-V,-Version

打印ip的版本并退出

-s,-stats,-statistics

输出更为详尽的信息(如果这个选项出现两次或者多次,输出的信息将更为详尽)

-f,-family

强调使用的协议种类(包括:inet、inet6或者link)

-4

是-family inet的简写

-6

是-family inet6的简写

-0

是-family link的简写

-o,-oneline

对每行记录都使用单行输出,回行用字符代替

-r,-resolve

查询域名解析系统,用获得的主机名代替主机IP地址

object是你要管理或获取信息的对象。目前ip认识的对象包括:

参数

显示列表

link

网络设备

address

一个设备的协议(IP或IPV6)地址

neighbour

ARP或者NDISC缓冲区条目

route

路由表条目

rule

路由策略数据库中的规则

maddress

多播地址

mroute

多播路由缓冲区条目

tuntap

管理TUN/TAP设备

netns

管理网络空间

例如:查看WIFI IP 地址:

adb shell ip -f inet addr show wlan0

模拟按键/输入

adb shell里有个很实用的命令叫input,通过它可以做一些有趣的事情。

可以执行adb shell input命令查看你完整help信息如下:

Usage: input [<source>] [-d DISPLAY_ID] <command> [<arg>...]

The sources are:
      touchnavigation
      touchscreen
      joystick
      stylus
      touchpad
      gamepad
      dpad
      mouse
      keyboard
      trackball

-d: specify the display ID.
      (Default: -1 for key event, 0 for motion event if not specified.)
The commands and default sources are:
      text <string> (Default: touchscreen)
      keyevent [--longpress|--doubletap] <key code number or name> ... (Default: keyboard)
      tap <x> <y> (Default: touchscreen)
      swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)
      draganddrop <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)
      press (Default: trackball)
      roll <dx> <dy> (Default: trackball)
      motionevent <DOWN|UP|MOVE|CANCEL> <x> <y> (Default: touchscreen)
      keycombination [-t duration(ms)] <key code 1> <key code 2> ... (Default: keyboard, the key order is important here.)

比如使用adb shell input keyevent <keycode>命令,不同的keycode能实现不同的功能,完整的keycode列表详见KeyEvent

电源键

adb shell input keyevent 26

菜单键

adb shell input keyevent 82

HOME 键

adb shell input keyevent 3

返回键

adb shell input keyevent 4

音量控制

增加音量:

adb shell input keyevent 24

降低音量:

adb shell input keyevent 25

静音:

adb shell input keyevent 164

媒体控制

播放/暂停:

adb shell input keyevent 85

停止播放:

adb shell input keyevent 86

播放下一首:

adb shell input keyevent 87

播放上一首:

adb shell input keyevent 88

恢复播放:

adb shell input keyevent 126

暂停播放:

adb shell input keyevent 127

点亮/熄灭屏幕

点亮屏幕:

adb shell input keyevent 224

熄灭屏幕:

adb shell input keyevent 223

滑动解锁

如果锁屏没有密码,是通过滑动手势解锁,那么可以通过 input swipe 来解锁。

命令(参数以机型 Nexus 5,向上滑动手势解锁举例):

adb shell input swipe 300 1000 300 500

参数 300 1000 300 500 分别表示起始点x坐标 起始点y坐标 结束点x坐标 结束点y坐标。

输入文本

在焦点处于某文本框时,可以通过 input 命令来输入文本。

adb shell input text hello

日志打印

Android 系统的日志分为两部分,底层的 Linux 内核日志输出到 /proc/kmsg,Android 的日志输出到 /dev/log

Android日志

查看Android系统属性的基本命令格式是:

adb logcat [option] [filter-speces]

如果需要停止logcat日志打印,可以按Ctrl + C停止日志监控。

按级别过滤日志

按界别过滤日志的基本命令格式是:

adb logcat [filter-specs]

Android的日志分为如下几个优先级(priority):

级别

含义

*:V

过滤只显示 Verbose 及以上级别(优先级最低)

*:D

过滤只显示 Debug 及以上级别

*:I

过滤只显示 Info 及以上级别

*:W

过滤只显示 Warning 及以上级别

*:E

过滤只显示 Error 及以上级别

*:F

过滤只显示 Fatal 及以上级别

*:S

过滤只显示 Silent 及以上级别(优先级最高,什么也不输出)

按某级别过滤日志则会将该级别及以上的日志输出。 比如,命令:

adb logcat *:W

会将 Warning、Error、Fatal 和 Silent 日志输出。 (注: 在 macOS 下需要给 *:W 这样以 * 作为 tag 的参数加双引号,如 adb logcat "*:W",不然会报错 no matches found: *:W。)

按tag和级别过滤日志

按 tag 和级别过滤日志的基本命令格式是:

adb logcat [tag:level] [tag:level] ...

比如,命令:

adb logcat ActivityManager:I MyApp:D *:S

表示输出 tag ActivityManager 的 Info 以上级别日志,输出 tag MyApp 的 Debug 以上级别日志,及其它 tag 的 Silent 级别日志(即屏蔽其它 tag 日志)。

将日志格式化输出

可以用 adb logcat -v <format> 选项指定日志输出格式。

日志支持按以下几种 <format>

参数

显示格式

brief

<priority>/<tag>(<pid>): <message>

process

<priority>(<pid>) <message>

tag

<priority>/<tag>: <message>

raw

<message>

time

<datetime> <priority>/<tag>(<pid>): <message>

threadtime

<datetime> <pid> <tid> <priority> <tag>: <message>

long

[ <datetime> <pid>:<tid> <priority>/<tag> ] <message>

日志格式默认为 brief,指定格式可与上面的过滤同时使用。比如:

adb logcat -v long ActivityManager:I *:S
清空已存在的日志
adb logcat -c
将日志显示在控制台
adb logcat -d
将日志输出到文件
adb logcat > file-path
加载一个可以使用的日志缓冲区供查看
adb logcat -b <Buffer>

Android log 输出量巨大,特别是通信系统的log,因此,Android把log输出到不同的缓冲区中,目前定义了四个log缓冲区:

缓冲区

含义

Radio

输出通信系统的log

System

输出系统组件的log

Event

输出event模块的log

Main

所有Java层的log以及不属于上面3层的log

缓冲区主要给系统组件使用,一般的应用不需要关心,应用的log都输出到main缓冲区中。默认log输出(不指定缓冲区的情况下)是输出System和Main缓冲区的log。

打印指定日志缓冲区的大小
adb logcat -g

内核日志

adb shell dmesg

输出示例:

[29319.543712] mt6375-chg 11280000.i2c:mt6375@34:chg: mt6375_chg_field_get F_CHG_EN, reg=0x01, val=1
[29319.543738] swpm_sp_routine regular update(216), total_suspend(319563053085)
[29319.543754] mt6375-chg 11280000.i2c:mt6375@34:chg: mt6375_chg_get_property psp=0
[29319.544148] mt6375-chg 11280000.i2c:mt6375@34:chg: mt6375_chg_field_get F_IC_STAT, reg=0x04, val=4
[29319.551883] mt6375-chg 11280000.i2c:mt6375@34:chg: get mt6375 charger status=4 vbus = 4987
[29319.554143] instant_current: cic_idx:0
[29319.559571] instant_current: cic_idx:0
[29319.564031] verify_get_property:5
[29319.564055] diff_fv_val:0, and will clear diff_fv_val as 0
[29319.564084] [CHARGE_LOOP] TYPE = [6 0 3], BMS = [100 4438 455 280], FULL = [1 0 0 0], scrn =0, low_fast[0 0 0], thermal_temp=279, thermal_level=0, FFC = 0, sw_cv=0, gauge_authentic=1, warm_term=0, smart_batt:0
[29319.564151] check_full_recharge diff_fv_val = 0, iterm = 250, iterm_effective = 100, fv_effective = 4480, full_count = 0, recharge_count = 0, eoc_count=0
[29319.564438] mt6375-chg 11280000.i2c:mt6375@34:chg: mt6375_chg_field_get F_IC_STAT, reg=0x04, val=4
[29319.564467] read F_IC_STAT success stat=4
[29319.564491] eoc_stat_count=0
[29319.564516] real_type:3, not support psy_type to check charger parameters
[29319.564527] get_index: value = 4438, index[0] = 2, index[1] = 2
[29319.564564] get_index: value = 4438, index[0] = 2, index[1] = 2, new_index = 2
[29319.564590] handle_step_charge index = 2
[29319.564599] get_index: value = 280, index[0] = 4, index[1] = 4
[29319.564632] get_index: value = 280, index[0] = 4, index[1] = 4, new_index = 4

中括号里的[29319.543712]代表内核开始启动后的时间,单位为秒。 通过内核日志我们可以做一些事情,比如衡量内核启动时间,在系统启动完毕后的内核日志里找到 Freeing init memory 那一行前面的时间就是。

查看Android设备系统属性

查看Android设备系统属性的基本命令格式是:

adb shell getprop [options]

除了可以查看Android设备系统属性之外,还可以设置系统属性,设置系统属性的基本命令格式是:

adb shell setprop <key> <value>

查看设备型号

adb shell getprop ro.product.model

输出示例:

24090RA29C

查看设备电池情况

adb shell dumpsys battery

输出示例:

Current Battery Service state:
  AC powered: false
  USB powered: true
  Wireless powered: false
  Dock powered: false
  Max charging current: 500000
  Max charging voltage: 5000000
  Charge counter: 5500000
  status: 2
  health: 2
  present: true
  level: 100
  scale: 100
  voltage: 4470
  temperature: 280
  technology: Li-poly

其中 scale 代表最大电量,level 代表当前电量。上面的输出表示还剩下 44% 的电量。

查看设备屏幕分辨率

adb shell wm size

输出示例:

Physical size: 1220x2712

该设备屏幕分辨率为 1220px * 2712px。

如果使用命令修改过,那输出可能是:

Physical size: 1220x2712
Override size: 480x1024

表明设备的屏幕分辨率原本是 1220px * 2712px,当前被修改为 480px * 1024px。

查看设备屏幕密度

adb shell wm density

输出示例:

Physical density: 480

查看设备显示屏参数

adb shell dumpsys window displays

输出示例:

Dump time : 2024-07-29 06:44:34.483
WINDOW MANAGER DISPLAY CONTENTS (dumpsys window displays)
  Display: mDisplayId=0 (organized)
    init=1220x2712 480dpi mMinSizeOfResizeableTaskDp=200 cur=1220x2712 app=1220x2586 rng=1220x1062-2634x2554
    deferred=false mLayoutNeeded=false mTouchExcludeRegion=SkRegion((0,0,1220,2712))

  mLastOrientationSource=WindowedMagnification:0:31@241761711
  deepestLastOrientationSource=Window{9e77f87 u0 NotificationShade}
  overrideConfig={1.0 ?mcc0mnc [zh_CN] ldltr sw407dp w407dp h851dp 480dpi nrml long widecg port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1220, 2712) mAppBounds=Rect(0, 78 - 1220, 2664) mMaxBounds=Rect(0, 0 - 1220, 2712) mDisplayRotation=ROTATION_0 mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=undefined mAlwaysOnTop=undefined mRotation=ROTATION_0 mInSplitScreen=false letterBoxed=false foScaledfalse screenType=0} s.29 fontWeightAdjustment=0/d/0 themeChanged=0 themeChangedFlags=0 extraData = Bundle[{}] screenType=0}
  mLayoutSeq=1696
  mCurrentFocus=Window{9e77f87 u0 NotificationShade}
  mFocusedApp=ActivityRecord{be9873f u0 com.miui.home/.launcher.Launcher t2}

  mHoldScreenWindow=null
  mObscuringWindow=Window{d131fde u0 com.miui.miwallpaper.wallpaperservice.MiuiKeyguardPictorialWallpaper}
  mLastWakeLockHoldingWindow=null
  mLastWakeLockObscuringWindow=Window{d131fde u0 com.miui.miwallpaper.wallpaperservice.MiuiKeyguardPictorialWallpaper}

  displayId=0
  mWallpaperTarget=Window{9e77f87 u0 NotificationShade}
  mLastWallpaperX=0.0 mLastWallpaperY=0.5

  AppResurDevice:false
  AppResurName:local
  AppResurVer:0
  AppResurEnable:true
  AppResurCPU:Unknown
  AppResurLaunchThres:3500
  AppResurMaxChildCount:3
  AppResurInactiveDuraH:36
  AppResurPkg2MaxChildD:{"com.tencent.qqlive":2}
  AppResurPkgDArray:com.youku.phone, com.qiyi.video, com.hunantv.imgo.activity, tv.danmaku.bili, com.tencent.qqlive, com.qiyi.video.lite,
  AppResurActivityDArray:com.tencent.qqlive/.ona.activity.VideoDetailActivity,
  ReResurEnable:true
  ReResurPkgDArray:com.youku.phone, com.tencent.qqlive, com.hunantv.imgo.activity, tv.danmaku.bili, com.qiyi.video.lite,
  ReResurActivityDArray:tv.danmaku.bili/.ui.video.VideoDetailsActivity, tv.danmaku.bili/com.bilibili.ship.theseus.all.UnitedBizDetailsActivity, tv.danmaku.bili/com.bilibili.ship.theseus.detail.UnitedBizDetailsActivity, com.hunantv.imgo.activity/com.mgtv.ui.player.VodPlayerPageActivity, com.hunantv.imgo.activity/com.mgtv.ui.videoplay.MGVideoPlayActivity, com.youku.phone/com.youku.ui.activity.DetailActivity, com.tencent.qqlive/.ona.activity.VideoDetailActivity, com.qiyi.video.lite/.videoplayer.activity.PlayerV2Activity,

  Display areas in top down Z order:
    * Leaf:36:36
    * HideDisplayCutout:32:35
      * OneHanded:34:35
        * FullscreenMagnification:34:35
          * Leaf:34:35
      * FullscreenMagnification:33:33
        * Leaf:33:33
      * OneHanded:32:32
        * Leaf:32:32
    * WindowedMagnification:0:31
      * HideDisplayCutout:26:31
        * OneHanded:26:31
          * FullscreenMagnification:29:31
            * Leaf:29:31
          * Leaf:28:28
          * FullscreenMagnification:26:27
            * Leaf:26:27
      * Leaf:24:25
      * HideDisplayCutout:18:23
        * OneHanded:18:23
          * FullscreenMagnification:18:23
            * Leaf:18:23
      * OneHanded:17:17
        * FullscreenMagnification:17:17
          * Leaf:17:17
      * HideDisplayCutout:16:16
        * OneHanded:16:16
          * FullscreenMagnification:16:16
            * Leaf:16:16
      * OneHanded:15:15
        * FullscreenMagnification:15:15
          * Leaf:15:15
      * HideDisplayCutout:0:14
        * OneHanded:0:14
          * ImePlaceholder:13:14
            * ImeContainer
          * FullscreenMagnification:0:12
            * Leaf:3:12
            * DefaultTaskDisplayArea (organized)
            * Leaf:0:1

  Task display areas in top down Z order:
    TaskDisplayArea DefaultTaskDisplayArea
      overrideConfig={0.0 ?mcc0mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?ldr ?wideColorGamut ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/? winConfig={ mBounds=Rect(0, 0 - 0, 0) mAppBounds=null mMaxBounds=Rect(0, 0 - 0, 0) mDisplayRotation=undefined mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=undefined mAlwaysOnTop=undefined mRotation=undefined mInSplitScreen=false letterBoxed=false foScaledfalse screenType=-1} ?fontWeightAdjustment/0/0 themeChanged=0 themeChangedFlags=0 extraData = Bundle[{}] screenType=-1}
      mLastFocusedRootTask=Task{fff089a #1 type=home}
      Application tokens in top down Z order:
      * Task{fff089a #1 type=home U=0 visible=true visibleRequested=false mode=fullscreen translucent=false sz=1}
        bounds=[0,0][1220,2712]
        * Task{35df80c #2 type=home I=com.miui.home/.launcher.Launcher U=0 rootTaskId=1 visible=true visibleRequested=false mode=fullscreen translucent=false sz=1}
          bounds=[0,0][1220,2712]
          * ActivityRecord{be9873f u0 com.miui.home/.launcher.Launcher t2}
      * Task{e612649 #82 type=standard A=10134:com.android.camera U=0 visible=false visibleRequested=false mode=fullscreen translucent=true sz=1}
        bounds=[0,0][1220,2712]
        * ActivityRecord{fb1c450 u0 com.android.camera/.Camera t82}
      * Task{27a553c #80 type=standard A=10123:android.task.browser U=0 visible=false visibleRequested=false mode=fullscreen translucent=true sz=1}
        bounds=[0,0][1220,2712]
        * ActivityRecord{3555b2f u0 com.android.browser/.BrowserActivity t80}
      * Task{185e901 #79 type=standard A=10153:com.xiaomi.market U=0 visible=false visibleRequested=false mode=fullscreen translucent=true sz=2}
        bounds=[0,0][1220,2712]
        * ActivityRecord{6d01a4e u0 com.xiaomi.market/.ui.FloatWebActivity t79}
        * ActivityRecord{2e97ee8 u0 com.xiaomi.market/.ui.MarketTabActivity t79}
      * Task{36bf3e5 #61 type=standard A=10124:android.task.contacts U=0 visible=false visibleRequested=false mode=fullscreen translucent=true sz=1}
        bounds=[0,0][1220,2712]
        * ActivityRecord{56f16dc u0 com.android.contacts/.activities.TwelveKeyDialer t61}
      * Task{674cc62 #76 type=standard A=10246:miui.task.player U=0 visible=false visibleRequested=false mode=fullscreen translucent=true sz=1}
        bounds=[0,0][1220,2712]
        * ActivityRecord{efc3f2d u0 com.miui.player/.ui.MusicBrowserActivity t76}
      * Task{38718f4 #3 type=undefined U=0 visible=false visibleRequested=false mode=fullscreen translucent=true sz=2}
        bounds=[0,0][1220,2712]
        * Task{a78528d #5 type=undefined U=0 rootTaskId=3 visible=false visibleRequested=false mode=multi-window translucent=true sz=0}
          bounds=[0,2712][1220,4068]
        * Task{afffc63 #4 type=undefined U=0 rootTaskId=3 visible=false visibleRequested=false mode=multi-window translucent=true sz=0}
          bounds=[0,0][1220,2712]
      * Task{7744b25 #6 type=undefined U=0 visible=false visibleRequested=false mode=fullscreen translucent=true sz=2}
        bounds=[0,0][1220,2712]
        * Task{dd7089e #8 type=undefined U=0 rootTaskId=6 visible=false visibleRequested=false mode=multi-window translucent=true sz=0}
          bounds=[0,2712][1220,4068]
        * Task{a81b9dd #7 type=undefined U=0 rootTaskId=6 visible=false visibleRequested=false mode=multi-window translucent=true sz=0}
          bounds=[0,0][1220,2712]

  no ScreenRotationAnimation

  rootHomeTask=Task=1

  PinnedTaskController
    mIsImeShowing=false
    mImeHeight=0
    mMinAspectRatio=0.41841003
    mMaxAspectRatio=2.39

  DisplayFrames w=1220 h=2712 r=0

  DisplayPolicy
    mCarDockEnablesAccelerometer=true mDeskDockEnablesAccelerometer=true
    mDockMode=EXTRA_DOCK_STATE_UNDOCKED mLidState=LID_ABSENT
    mAwake=false mScreenOnEarly=false mScreenOnFully=false
    mKeyguardDrawComplete=false mWindowManagerDrawComplete=false
    mHdmiPlugged=false
    mLastBehavior=SHOW_TRANSIENT_BARS_BY_SWIPE
    mShowingDream=false mDreamingLockscreen=true
    mStatusBar=Window{a554895 u0 StatusBar}
    mExpandedPanel=Window{9e77f87 u0 NotificationShade}
    isKeyguardShowing=true
    mNavigationBar=Window{5e53d01 u0 NavigationBar0}
    mNavBarOpacityMode=0
    mNavigationBarCanMove=false
    mNavigationBarPosition=4
    mTopGestureHost=Window{a554895 u0 StatusBar}
    mBottomGestureHost=Window{5e53d01 u0 NavigationBar0}
    mFocusedWindow=Window{9e77f87 u0 NotificationShade}
    mLastStatusBarAppearanceRegions=
    mLastLetterboxDetails=
    mTopIsFullscreen=false
    mForceShowNavigationBarEnabled=false mAllowLockscreenWhenOn=false
    mRemoteInsetsControllerControlsSystemBars=false
    mDecorInsetsInfo:
      ROTATION_0={nonDecorInsets=[0,78][0,48], configInsets=[0,110][0,48], nonDecorFrame=[0,78][1220,2664], configFrame=[0,110][1220,2664]}
      ROTATION_90={nonDecorInsets=[78,0][0,48], configInsets=[78,110][0,48], nonDecorFrame=[78,0][2712,1172], configFrame=[78,110][2712,1172]}
      ROTATION_180={nonDecorInsets=[0,0][0,126], configInsets=[0,110][0,126], nonDecorFrame=[0,0][1220,2586], configFrame=[0,110][1220,2586]}
      ROTATION_270={nonDecorInsets=[0,0][78,48], configInsets=[0,110][78,48], nonDecorFrame=[0,0][2634,1172], configFrame=[0,110][2634,1172]}
    SystemGestures:
      mDisplayCutoutTouchableRegionSize=36
      mSwipeStartThreshold=Rect(72, 114 - 72, 72)
      mSwipeDistanceThreshold=72
    Looper state:
      Looper (android.ui, tid 30) {7b47a1}
        (Total messages: 0, polling=true, quitting=false)
        Dump time: 2024-07-29 18:44:34.485 GMT+08:00
        Package: system_server
        Current looper: Looper (android.ui, tid=1780)
        History of long time messages on Looper (android.ui, tid=1780):


  DisplayRotation
    mCurrentAppOrientation=SCREEN_ORIENTATION_NOSENSOR
    mLastOrientation=5
    mRotation=0 mDeferredRotationPauseCount=0
    mLandscapeRotation=ROTATION_90 mSeascapeRotation=ROTATION_270
    mPortraitRotation=ROTATION_0 mUpsideDownRotation=ROTATION_180
    mSupportAutoRotation=true
    WindowOrientationListener
      mEnabled=false
      mCurrentRotation=ROTATION_0
      mSensorType=null
      mSensor={Sensor name="dev_orient", vendor="mtk", version=1, type=27, maxRange=3.0, resolution=1.0, power=0.001, minDelay=0}
      mRate=2
      OrientationSensorJudge
        mDesiredRotation=ROTATION_0
        mProposedRotation=ROTATION_0
        mTouching=false
        mTouchEndedTimestampNanos=344748526855798
        mLastRotationResolution=0

    mCarDockRotation=-1 mDeskDockRotation=-1
    mUserRotationMode=USER_ROTATION_LOCKED mUserRotation=ROTATION_0 mCameraRotationMode=0 mAllowAllRotations=unknown
    mDemoHdmiRotation=ROTATION_90 mDemoHdmiRotationLock=false mUndockedHdmiRotation=-1
    mLidOpenRotation=-1
    mFixedToUserRotation=false

  InputConsumers:
    name=recents_animation_input_consumer pid=2688 user=UserHandle{0}

  WindowInsetsStateController
    InsetsState
      mDisplayFrame=Rect(0, 0 - 1220, 2712)
      mDisplayCutout=DisplayCutout{insets=Rect(0, 78 - 0, 0) waterfall=Insets{left=0, top=0, right=0, bottom=0} boundingRect={Bounds=[Rect(0, 0 - 0, 0), Rect(550, 0 - 670, 78), Rect(0, 0 - 0, 0), Rect(0, 0 - 0, 0)]} cutoutPathParserInfo={CutoutPathParserInfo{displayWidth=1220 displayHeight=2712 physicalDisplayWidth=1220 physicalDisplayHeight=2712 density={3.0} cutoutSpec={M 0,0 H -33 V 110 H 33 V 0 H 0 Z} rotation={0} scale={1.0} physicalPixelDisplaySizeRatio={1.0}}}}
      mRoundedCorners=RoundedCorners{[RoundedCorner{position=TopLeft, radius=104, center=Point(104, 104)}, RoundedCorner{position=TopRight, radius=104, center=Point(1116, 104)}, RoundedCorner{position=BottomRight, radius=104, center=Point(1116, 2608)}, RoundedCorner{position=BottomLeft, radius=104, center=Point(104, 2608)}]}
      mRoundedCornerFrame=Rect(0, 0 - 0, 0)
      mPrivacyIndicatorBounds=PrivacyIndicatorBounds {static bounds=Rect(36, 0 - 76, 110) rotation=0}
      mDisplayShape=DisplayShape{ spec=1406003047 displayWidth=1220 displayHeight=2712 physicalPixelDisplaySizeRatio=1.0 rotation=0 offsetX=0 offsetY=0 scale=1.0}
      mSeq=0
        InsetsSource id=996a0001 type=navigationBars frame=[0,2664][1220,2712] visible=true flags= insetsRoundedCornerFrame=false
        InsetsSource id=996a0004 type=systemGestures frame=[0,0][0,0] visible=true flags= insetsRoundedCornerFrame=false
        InsetsSource id=996a0005 type=mandatorySystemGestures frame=[0,2664][1220,2712] visible=true flags= insetsRoundedCornerFrame=false
        InsetsSource id=996a0006 type=tappableElement frame=[0,0][0,0] visible=true flags= insetsRoundedCornerFrame=false
        InsetsSource id=996a0024 type=systemGestures frame=[0,0][0,0] visible=true flags= insetsRoundedCornerFrame=false
        InsetsSource id=3 type=ime frame=[0,0][0,0] visibleFrame=[0,1597][1220,2712] visible=false flags= insetsRoundedCornerFrame=false
        InsetsSource id=27 type=displayCutout frame=[0,0][1220,78] visible=true flags= insetsRoundedCornerFrame=false
        InsetsSource id=32b10000 type=statusBars frame=[0,0][1220,110] visible=true flags= insetsRoundedCornerFrame=false
        InsetsSource id=32b10005 type=mandatorySystemGestures frame=[0,0][1220,114] visible=true flags= insetsRoundedCornerFrame=false
        InsetsSource id=32b10006 type=tappableElement frame=[0,0][1220,110] visible=true flags= insetsRoundedCornerFrame=false
    Control map:
      Window{9e77f87 u0 NotificationShade}:
        InsetsSourceControl: {996a0001 mType=navigationBars initiallyVisible mSurfacePosition=Point(0, 2664) mInsetsHint=Insets{left=0, top=0, right=0, bottom=48}}
        InsetsSourceControl: {32b10000 mType=statusBars initiallyVisible mSurfacePosition=Point(0, 0) mInsetsHint=Insets{left=0, top=110, right=0, bottom=0}}
      Window{306d963 u0 com.miui.home/com.miui.home.launcher.Launcher}:
        InsetsSourceControl: {3 mType=ime mSurfacePosition=Point(0, 110) mInsetsHint=Insets{left=0, top=0, right=0, bottom=1115}}
    InsetsSourceProviders:
      InsetsSourceProvider
        mSource=InsetsSource id=32b10006 type=tappableElement frame=[0,0][1220,110] visible=true flags= insetsRoundedCornerFrame=false
        mSourceFrame=Rect(0, 0 - 1220, 110)
        mIsLeashReadyForDispatching=true
        mWindowContainer=Window{a554895 u0 StatusBar}
      InsetsSourceProvider
        mSource=InsetsSource id=32b10005 type=mandatorySystemGestures frame=[0,0][1220,114] visible=true flags= insetsRoundedCornerFrame=false
        mSourceFrame=Rect(0, 0 - 1220, 114)
        mIsLeashReadyForDispatching=true
        mWindowContainer=Window{a554895 u0 StatusBar}
      InsetsSourceProvider
        mSource=InsetsSource id=32b10000 type=statusBars frame=[0,0][1220,110] visible=true flags= insetsRoundedCornerFrame=false
        mSourceFrame=Rect(0, 0 - 1220, 110)
        mControl=InsetsSourceControl mId=32b10000 mType=statusBars mLeash=Surface(name=Surface(name=a554895 StatusBar#98)/@0x6ca7d9b - animation-leash of insets_animation#2626)/@0x7812931 mInitiallyVisible=true mSurfacePosition=Point(0, 0) mInsetsHint=Insets{left=0, top=110, right=0, bottom=0} mSkipAnimationOnce=false
        mIsLeashReadyForDispatching=true
        mWindowContainer=Window{a554895 u0 StatusBar}
        mAdapter=ControlAdapter mCapturedLeash=Surface(name=Surface(name=a554895 StatusBar#98)/@0x6ca7d9b - animation-leash of insets_animation#2626)/@0x7812931
        mControlTarget=Window{9e77f87 u0 NotificationShade}
      ImeInsetsSourceProvider
        mSource=InsetsSource id=3 type=ime frame=[0,0][0,0] visibleFrame=[0,1597][1220,2712] visible=false flags= insetsRoundedCornerFrame=false
        mSourceFrame=Rect(0, 1597 - 1220, 2712)
        mControl=InsetsSourceControl mId=3 mType=ime mLeash=Surface(name=Surface(name=467ee4a InputMethod#138)/@0x6a956d8 - animation-leash of insets_animation#2201)/@0x291f66d mInitiallyVisible=false mSurfacePosition=Point(0, 110) mInsetsHint=Insets{left=0, top=0, right=0, bottom=1115} mSkipAnimationOnce=false
        mIsLeashReadyForDispatching=true
        mWindowContainer=Window{467ee4a u0 InputMethod}
        mAdapter=ControlAdapter mCapturedLeash=Surface(name=Surface(name=467ee4a InputMethod#138)/@0x6a956d8 - animation-leash of insets_animation#2201)/@0x291f66d
        mControlTarget=Window{306d963 u0 com.miui.home/com.miui.home.launcher.Launcher}
        mImeShowing=false
      InsetsSourceProvider
        mSource=InsetsSource id=996a0024 type=systemGestures frame=[0,0][0,0] visible=true flags= insetsRoundedCornerFrame=false
        mSourceFrame=Rect(0, 0 - 0, 0)
        mIsLeashReadyForDispatching=true
        mWindowContainer=Window{5e53d01 u0 NavigationBar0}
      InsetsSourceProvider
        mSource=InsetsSource id=996a0006 type=tappableElement frame=[0,0][0,0] visible=true flags= insetsRoundedCornerFrame=false
        mSourceFrame=Rect(0, 0 - 0, 0)
        mIsLeashReadyForDispatching=true
        mWindowContainer=Window{5e53d01 u0 NavigationBar0}
      InsetsSourceProvider
        mSource=InsetsSource id=996a0005 type=mandatorySystemGestures frame=[0,2664][1220,2712] visible=true flags= insetsRoundedCornerFrame=false
        mSourceFrame=Rect(0, 2664 - 1220, 2712)
        mIsLeashReadyForDispatching=true
        mWindowContainer=Window{5e53d01 u0 NavigationBar0}
      InsetsSourceProvider
        mSource=InsetsSource id=996a0004 type=systemGestures frame=[0,0][0,0] visible=true flags= insetsRoundedCornerFrame=false
        mSourceFrame=Rect(0, 0 - 0, 0)
        mIsLeashReadyForDispatching=true
        mWindowContainer=Window{5e53d01 u0 NavigationBar0}
      InsetsSourceProvider
        mSource=InsetsSource id=996a0001 type=navigationBars frame=[0,2664][1220,2712] visible=true flags= insetsRoundedCornerFrame=false
        mSourceFrame=Rect(0, 2664 - 1220, 2712)
        mOverrideFrames={2011=Rect(0, 2664 - 1220, 2712)}
        mControl=InsetsSourceControl mId=996a0001 mType=navigationBars mLeash=Surface(name=Surface(name=5e53d01 NavigationBar0#93)/@0x236b5e7 - animation-leash of insets_animation#2627)/@0xd9f7597 mInitiallyVisible=true mSurfacePosition=Point(0, 2664) mInsetsHint=Insets{left=0, top=0, right=0, bottom=48} mSkipAnimationOnce=false
        mIsLeashReadyForDispatching=true
        mWindowContainer=Window{5e53d01 u0 NavigationBar0}
        mAdapter=ControlAdapter mCapturedLeash=Surface(name=Surface(name=5e53d01 NavigationBar0#93)/@0x236b5e7 - animation-leash of insets_animation#2627)/@0xd9f7597
        mControlTarget=Window{9e77f87 u0 NotificationShade}

其中 mDisplayId 为 显示屏编号,init 是初始分辨率和屏幕密度,app 的高度比 init 里的要小,表示屏幕底部有虚拟按键,高度为 2712 - 2586= 126px 合 42dp。

查看设备IMEI

在Android4.4及以下版本可通过如下命令获取IMEI:

adb shell dumpsys iphonesubinfo

在Android5.0及其以上版本这个命令输出为空,通过其他方式获取(需要使用root权限):

adb shell
su
service call iphonesubinfo 1

service call iphonesubinfo 1

Result: Parcel( fffffffc ffffffff 00000000 '............')

查看设备Android系统版本

adb shell getprop ro.build.version.release

输出示例:

14

查看设备IP地址

adb shell ifconfig | grep Mask

在有的设备上这个命令没有输出,如果设备连着 WiFi,可以使用如下命令来查看局域网 IP:

adb shell ifconfig wlan0

如果以上命令仍然不能得到期望的信息,那可以试试以下命令(部分系统版本里可用):

adb shell netcfg

查看设备Mac地址

adb shell cat /sys/class/net/wlan0/address

输出示例:

9e:03:bd:2a:23:bc

这查看的是局域网 Mac 地址,移动网络或其它连接的信息可以通过前面的小节「IP 地址」里提到的 adb shell netcfg 命令来查看。

查看设备CPU信息

adb shell cat /proc/cpuinfo

查看设备内存信息

adb shell cat /proc/meminfo

查看设备更多硬件与系统属性

adb shell cat /system/build.prop

这会输出很多信息,包括前面几个小节提到的「型号」和「Android 系统版本」等。 输出里还包括一些其它有用的信息,它们也可通过 adb shell getprop <属性名> 命令单独查看,列举一部分属性如下:

屏幕密度

含义

ro.build.version.sdk

SDK 版本

ro.build.version.release

Android 系统版本

ro.build.version.security_patch

Android 安全补丁程序级别

ro.product.model

型号

ro.product.brand

品牌

ro.product.name

设备名

ro.product.board

处理器型号

ro.product.cpu.abilist

CPU 支持的 abi 列表[节注一]

persist.sys.isUsbOtgEnabled

是否支持 OTG

dalvik.vm.heapsize

每个应用程序的内存上限

ro.sf.lcd_density

屏幕密度

注意: 一些小厂定制的 ROM 可能修改过 CPU 支持的 abi 列表的属性名,如果用 ro.product.cpu.abilist 属性名查找不到,可以这样试试:

adb shell cat /system/build.prop | grep ro.product.cpu.abi

修改设置

注: 修改设置之后,运行恢复命令有可能显示仍然不太正常,可以运行 adb reboot 重启设备,或手动重启。

修改设置的原理主要是通过 settings 命令修改 /data/data/com.android.providers.settings/databases/settings.db 里存放的设置值。

修改分辨率

adb shell wm size 5000x1024

表示将分辨率修改为5000px*1024px

恢复原分辨率命令:

adb shell wm reset

修改屏幕密度

adb shell wm density 999

表示将密度修改为999dpi。

恢复原密度命令:

adb shell wm density reset

修改显示区域

adb shell wm overscan 0,0,0,300

四个数字分别表示距离左、上、右、下边缘的留白像素,以上命令表示将屏幕底部300px留白。

恢复原显示区域命令:

adb shell wm overscan reset

修改关闭USB调试模式

adb shell settings put global adb_enabled 0

用命令恢复不了,因为关闭了USB调试adb就连不上Android设备。

只能在设备上手动恢复:设置-开发者选项-Android调试。

修改允许/禁止访问非SDK API

允许访问非SDK API:

adb shell settings put global hidden_api_policy_pre_p_apps 1
adb shell settings put global hidden_api_policy_p_apps 1

禁止访问非SDK API:

adb shell settings delete global hidden_api_policy_pre_p_apps
adb shell settings delete global hidden_api_policy_p_apps

不需要设备获得root权限。

命令最后的数字含义:

含义

0

禁止检测非 SDK 接口的调用。该情况下,日志记录功能被禁用,并且令 strict mode API,即 detectNonSdkApiUsage() 无效。不推荐。

1

仅警告——允许访问所有非 SDK 接口,但保留日志中的警告信息,可继续使用 strick mode API。

2

禁止调用深灰名单和黑名单中的接口。

3

禁止调用黑名单中的接口,但允许调用深灰名单中的接口。

修改状态栏和导航栏的显示隐藏

adb shell settings put global policy_control <key-values>

<key-values> 可由如下几种键及其对应的值组成,格式为 <key1>=<value1>:<key2>=<value2>

key

含义

immersive.full

同时隐藏

immersive.status

隐藏状态栏

immersive.navigation

隐藏导航栏

immersive.preconfirms

这些键对应的值可则如下值用逗号组合:

value

含义

apps

所有应用

*

所有界面

package-name

指定应用

-package-name

排除指定应用

例如:

adb shell settings put global policy_control immersive.full=miui

表示设置在所有界面下都同时隐藏状态栏和导航栏。

adb shell settings put global policy_control immersive.status=com.package1,com.package2:immersive.navigation=apps,-com.package3

表示设置在包名为 com.package1 com.package2 的应用里隐藏状态栏,在除了包名为 com.package3 的所有应用里隐藏导航栏。

实用功能

截屏

截图保存到电脑:

adb exec-out screencap -p > sc.png

如果 adb 版本较老,无法使用 exec-out 命令,这时候建议更新 adb 版本。无法更新的话可以使用以下麻烦点的办法: 先截图保存到设备里:

adb shell screencap -p /sdcard/sc.png

然后将 png 文件导出到电脑:

adb pull /sdcard/sc.png

可以使用 adb shell screencap -h 查看 screencap 命令的帮助信息,下面是两个有意义的参数及含义:

参数

含义

-p

指定保存文件为png格式

-d display -id

指定截图的显示屏编号(有多显示屏的情况下)

实测如果指定文件名以 .png 结尾时可以省略 -p 参数;否则需要使用 -p 参数。如果不指定文件名,截图文件的内容将直接输出到 stdout。 另外一种一行命令截图并保存到电脑的方法: Linux 和 Windows

adb shell screencap -p | sed "s/\r$//" > sc.png

Mac OS X

adb shell screencap -p | gsed "s/\r$//" > sc.png

这个方法需要用到 gnu sed 命令,在 Linux 下直接就有,在 Windows 下 Git 安装目录的 bin 文件夹下也有。如果确实找不到该命令,可以下载 sed for Windows 并将 sed.exe 所在文件夹添加到 PATH 环境变量里。

而在 Mac 下使用系统自带的 sed 命令会报错:

sed: RE error: illegal byte sequence

需要安装 gnu-sed,然后使用 gsed 命令:

brew install gnu-sed

录屏

录制屏幕以 mp4 格式保存到 /sdcard:

adb shell screenrecord /sdcard/filename.mp4

需要停止时按 Ctrl-C,默认录制时间和最长录制时间都是 180 秒。 如果需要导出到电脑:

adb pull /sdcard/filename.mp4

可以使用 adb shell screenrecord --help 查看 screenrecord 命令的帮助信息,下面是常见参数及含义:

参数

含义

–size WIDTHxHEIGHT

视频的尺寸,比如 1280x720,默认是屏幕分辨率。

–bit-rate RATE

视频的比特率,默认是 4Mbps。

–time-limit TIME

录制时长,单位秒。

–verbose

输出更多信息。

查看连接过的WIFI密码

注意:需要root权限。

adb shell
su
cat /data/misc/wifi/*.conf

设置系统日期和时间

注意:需要root权限。

adb shell
su
data -s 20280303.131400

重启手机

adb reoot

检测设备是否已root

adb shell
su

此时命令行提示符是 $ 则表示没有 root 权限,是 # 则表示已 root。

使用Monkey进行压力测试

Monkey可以生产伪随机用户事件来模拟点击、触摸、手势等操作,可以对正在开发中的程序进行随机压力测试。

简单用法:

adb shell monkey -p <packagename> -v 500

表示向 <packagename> 指定的应用程序发送 500 个伪随机事件。

adb shell monkey -p com.xunmeng.pinduoduo -v 500

Monkey文档

开启/关闭WiFi

注意:需要root权限。

开启WiFi:
adb root
adb shell svc wifi enable
关闭wifi:
adb root
adb shell svc wifi disable

若执行成功,输出为空;若未取得 root 权限执行此命令,将执行失败,输出 Killed。

刷机相关指令

重启到Recovery模式

adb reboot recovery

从Recovery重启到Android

adb reboot

重启到Fastboot模式

adb reboot bootloader

通过sideload更新系统

如果我们下载了 Android 设备对应的系统更新包到电脑上,那么也可以通过 adb 来完成更新。

以 Recovery 模式下更新为例:

重启到 Recovery 模式。

adb reboot recovery

在设备的 Recovery 界面上操作进入 Apply update-Apply from ADB。注:不同 的 Recovery 菜单可能与此有差异,有的是一级菜单就有 Apply update from ADB

通过 adb 上传和更新系统。

adb sideload <path-to-update.zip>

安全相关命令

启用/禁用SELinux

启用SELinux
adb root
adb shell setenforce 1
禁用SELinux
adb root
adb shell setenforce 0

启用/禁用dm_verity

启动dm_verity
adb root
adb enable-verity
禁用dm_verity
adb root
adb disable-verity

adb shell命令

Android 系统是基于 Linux 内核的,所以 Linux 里的很多命令在 Android 里也有相同或类似的实现,在 adb shell 里可以调用。本文档前面的部分内容已经用到了 adb shell 命令。

查看进程状态

adb shell ps

输出信息各列含义:

列名

含义

USER

所属用户

PID

进程ID

PPID

父进程ID

NAME

进程名

查看处理器实时状态

adb shell top [-m max_procs] [-n iterations] [-d delay] [-s sort_column] [-t] [-h]

adb shell top 后面可以跟一些可选参数进行过滤查看不同的列表,可用参数及含义如下:

参数

含义

-m

最多显示多少个进程

-n

刷新多少次后退出

-d

刷机时间间隔(单位秒,默认值为5)

-s

按某列排序(可用col值:cpu,vss,rss,thr)

-t

显示线程信息

-h

显示帮助文档

输出信息各列含义:

列名

含义

-

-

PID

进程ID

PR

优先级

CPU%

当前瞬间占用CPU百分比

S

进程状态(R=运行,S=睡眠,T=跟踪/停止,Z=僵尸进程)

#THR

线程数

VSS

Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)

RSS

Resident Set Size 实际使用物理内存(包含共享库占用的内存)

PCY

调度策略优先级,SP_BACKGROUND/SPFOREGROUND

UID

进程所有者的用户ID

NAME

进程名

查看进程UID

有两种方案:

  1. adb shell dumpsys package <packagename> | grep userId= 如:adb shell dumpsys package org.mazhuang.guanggoo | grep userId=userId=10394

  2. 通过 ps 命令找到对应进程的 pid 之后 adb shell cat /proc/<pid>/status | grep Uid如:

adb shell
gemini:/ $ ps | grep org.mazhuang.guanggoo
u0_a394 28635 770 1795812 78736 SyS_epoll_ 0000000000 S org.mazhuang.guanggoo
gemini:/ $ cat /proc/28635/status | grep Uid
Uid: 10394 10394 10394 10394
gemini:/ $

adb shell

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值