2070 字
10 分钟
ADB常用命令使用总结
2024-07-25

前言#

介绍:

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

  • 客户端:用于发送命令。客户端在开发机器上运行。您可以通过发出 adb 命令从命令行终端调用客户端。
  • 守护程序 (adbd):用于在设备上运行命令。守护程序在每个设备上作为后台进程运行。
  • 服务器:用于管理客户端与守护程序之间的通信。服务器在开发机器上作为后台进程运行。

简单来说就是可以在电脑上使用adb命令控制android设备

安装:

下载地址: SDK 平台工具版本说明 | Android Studio | Android Developers

Android SDK Platform-Tools 中包含了许多android开发的相关工具, 当然也包括adb, 把adb.exe所在路径添加到系统环境变量中, 即可在任意目录使用adb了

可以输入以下验证是否安装成功

Terminal window
C:\Users\Tiam>adb version
Android Debug Bridge version 1.0.41
Version 33.0.3-8952118
Installed as <你的adb.exe安装位置>

连接设备:

手机中需要打开开发者设置, 打开USB调试, 使用USB连接android设备与电脑即可; 同在局域网下, 也可以使用无线调试;连接失败可以试试重新打开一下wifi;

(不同手机系统和adb版本可能方法不同)

image-20240714194145932

Terminal window
C:\Users\Tiam>adb pair 192.168.0.108:42937
Enter pairing code: 377379
Successfully paired to 192.168.0.108:42937 [guid=adb-febcc611-AtDaZh]
C:\Users\Tiam>adb devices
List of devices attached
C:\Users\Tiam>adb connect 192.168.0.108:43267
connected to 192.168.0.108:43267
# 查看已连接的设备
C:\Users\Tiam>adb devices
List of devices attached
192.168.0.108:43267 device

常用命令#

Terminal window
# 查看当前屏幕UI树, 实质是调用了Android系统下的/system/bin/uiautomator.jar dump的dumpWindowHierarchy方法。
adb shell uiautomator dump /sdcard/ui.xml
# 拉取到本地
adb pull /sdcard/uidump.xml
# 唤醒设备, 息屏唤醒
adb shell input keyevent KEYCODE_WAKEUP
# 启动APP
adb shell am start -n <package>/.<main_activity>
# 不知道 main_activity ? 这个命令可以查到
adb shell cmd package resolve-activity --brief <package> | tail -n 1
# 不知道 package ? 通常可以系统设置->应用管理->对应的APP->右上角应用信息中看到
# -W:这个选项告诉Activity Manager在启动Activity后等待结果,它会返回一个表示启动是否成功的确认码。
adb shell am start -W com.zhiliaoapp.musically/com.ss.android.ugc.aweme.main.MainActivity
# 查看当前Activity, 这里也会返回当前所打开的应用包名
adb shell dumpsys activity top | grep ACTIVITY | tail -n 1
# 查应用是否启动
adb shell pidof <package>
# 停止应用
adb shell am force-stop <package>
# 要查看已安装的第三方应用的包名列表 => package_name
adb shell pm list package -3 -f

1. adb#

adb help 可以获取如何使用的信息

2. push/pull#

用于在电脑与设备之间传输文件

push可以将电脑上的文件推送到android设备上

pull则是从android设备上拉取文件到电脑上

比如:

Terminal window
# 推送到 Download 目录下
adb push text.txt /sdcard/Download/
# 使用绝对路径
adb push "D:\Tiam\Downloads\payload" /data/local/tmp/
# 将 /sdcard/Download/text.txt 文件拉取到当前目录
adb pull /sdcard/Download/text.txt
# 将 /sdcard/Download/text.txt 文件拉取到指定目录
adb pull /sdcard/Download/text.txt "D:/Tiam\Downloads/"

关于android系统目录.

/data/local/tmp/: 这个tmp目录是一个Android系统的特殊目录。这个目录的特点是adb可以直接push写入。app都能读取,但不能写入。同时这个目录还是手机的内置存储,可以直接用DexClassLoader加载这个目录中的apk,而sdcard不行。所以,如果我们用adb push到sdcard上,想动态加载还要将apk再复制到应用的data目录才行。

/sdcard/Download/: 系统下载目录, 通常系统下载的文件都会保存到这里.

3. shell#

因为android是基于linux开发的, 所以很多linux上的命令, 在这里的shell中是通用的

Terminal window
# 查看可用的shell命令工具
adb shell ls /system/bin

直接 adb shell 进入 shell 控制台, 按 Control+D 或输入 exit 退出

What is toybox? (landley.net)查看shell命令帮助, 比如查看ls命令:

Terminal window
C:\Users\Tiam>adb shell toybox ls --help
usage: ls --color[=auto] [-ACFHLRSZacdfhiklmnpqrstux1] [directory...]
list files
what to show:
-a all files including .hidden -b escape nongraphic chars
-c use ctime for timestamps -d directory, not contents
-i inode number -p put a '/' after dir names
-q unprintable chars as '?' -s storage used (1024 byte units)
-u use access time for timestamps -A list all files but . and ..
-H follow command line symlinks -L follow symlinks
-R recursively list in subdirs -F append /dir *exe @sym |FIFO
-Z security context
output formats:
-1 list one file per line -C columns (sorted vertically)
-g like -l but no owner -h human readable sizes
-l long (show full details) -m comma separated
-n like -l but numeric uid/gid -o like -l but no group
-x columns (horizontal sort) -ll long with nanoseconds (--full-time)
sorting (default is alphabetical):
-f unsorted -r reverse -t timestamp -S size
--color device=yellow symlink=turquoise/red dir=blue socket=purple
files: exe=green suid=red suidfile=redback stickydir=greenback
=auto means detect if output is a tty.

1. Activity manager#

activity 管理器 (am) 使用帮助

adb shell am help

Terminal window
# 要启动一个应用
adb shell am start -n <package_name>/.<activity_name>
# -W:这个选项告诉Activity Manager在启动Activity后等待结果,它会返回一个表示启动是否成功的确认码。
adb shell am start -W com.zhiliaoapp.musically/com.ss.android.ugc.aweme.main.MainActivity
adb shell am start -W com.ss.android.ugc.trill/com.ss.android.ugc.aweme.main.MainActivity
# 停止应用
adb shell am force-stop <package_name>
# 打开语言设置界面
adb shell am start -a android.settings.LOCALE_SETTINGS
# 打开语言设置
adb shell am start -a android.settings.LOCALE_SETTINGS
# 打开日期和时间设置
adb shell am start -a android.settings.DATE_SETTINGS

2. Package manager#

软件包管理器 (pm)使用帮助

adb shell pm help

3. Settings provider#

adb shell settings help

Terminal window
# 设置时间同步
adb shell settings put global auto_time 1
# 时区同步
adb shell settings put global auto_time_zone 1
# 修改默认输入法 (输入法的具体设置值需要根据设备上安装的输入法应用的包名和类名进行替换)
adb shell settings put secure default_input_method "com.android.inputmethod.latin/.LatinIME"
# 获取android_id
adb shell settings get secure android_id

4. Android system property#

adb shell getprop --help

Terminal window
# 查看CPU架构类型(查指定属性)
adb shell getprop ro.product.cpu.abi # arm64-v8a
# 查看全部 prop 属性
adb shell getprop

ro 是 “read-only”(只读)的缩写, 通常带有ro的属性是出厂自带的, 不可更改的

persist 持久的, 一般都可以改

5. dumpsys#

adb shell dumpsys --help

Terminal window
# 获取当前的前台应用Activity => activity_name
adb shell dumpsys window w | grep mCurrent
# 激活的所有Activity, 最近任务列表
adb shell dumpsys activity top | grep ACTIVITY
# 查看当前Activity
adb shell dumpsys activity top | grep ACTIVITY | tail -n 1
adb shell dumpsys activity activities | grep mResumedActivity

4. install#

安装应用, 可以直接在电脑上下载apk文件后直接安装, 避免传输到手机上, 再通过手机安装程序安装的麻烦, 也避免了安装程序的检测提示, 麻烦得要死

然后下载的apk多了, 如果文件名没有命名好, 可能会不知道这个apk文件是什么, 什么版本的等等, 这里可以使用aapt工具

Terminal window
# 只查看前三行(可能信息比较多,太长, 如果你只关注版本等信息的话)
D:\Tiam\Downloads>aapt dump badging MT2.14.2-CoolApk-21048.apk | head -n 3
package: name='bin.mt.plus' versionCode='23110960' versionName='2.14.2' platformBuildVersionName='14' platformBuildVersionCode='34' compileSdkVersion='34' compileSdkVersionCodename='14'
sdkVersion:'21'
targetSdkVersion:'30'

如果出现了中文乱码的情况, 可以使用chcp 65001命令后, 重新执行aapt命令

Terminal window
install [-lrtsdg] [--instant] PACKAGE
push a single package to the device and install it
install-multiple [-lrtsdpg] [--instant] PACKAGE...
push multiple APKs to the device for a single package and install them
install-multi-package [-lrtsdpg] [--instant] PACKAGE...
push one or more packages to the device and install them atomically
-r: replace existing application
-t: allow test packages
-d: allow version code downgrade (debuggable packages only)
-p: partial application install (install-multiple only)
-g: grant all runtime permissions
--abi ABI: override platform's default ABI
--instant: cause the app to be installed as an ephemeral install app
--no-streaming: always push APK to device and invoke Package Manager as separate steps
--streaming: force streaming APK directly into Package Manager
--fastdeploy: use fast deploy
--no-fastdeploy: prevent use of fast deploy
--force-agent: force update of deployment agent when using fast deploy
--date-check-agent: update deployment agent when local version is newer and using fast deploy
--version-check-agent: update deployment agent when local version has different version code and using fast deploy
(See also `adb shell pm help` for more options.)

adb install 报错大全以及解决方法 - 简书 (jianshu.com)

平时最常用的还是 push/pull传输文件, install安装应用, 相比传统的方法可以提升很高的效率.

相关python库#

ADB用法文档#

ADB常用命令使用总结
https://blog.tiamix.cn/posts/adb使用/
作者
Tiam
发布于
2024-07-25
许可协议
CC BY-NC-SA 4.0