概念
Android 调试桥 (Android Debug Bridge) 是一种功能多样的命令行工具,可让您与设备进行通信。adb 命令可用于执行各种设备操作(例如安装和调试应用),并提供对 Unix shell(可用来在设备上运行各种命令)的访问权限。它是一种客户端-服务器程序,包括以下三个组件:
- 客户端:用于发送命令。客户端在开发计算机上运行。您可以通过发出 adb 命令来从命令行终端调用客户端。
- 守护进程 (adbd):在设备上运行命令。守护进程在每个设备上作为后台进程运行。
- 服务器:管理客户端和守护进程之间的通信。服务器在开发机器上作为后台进程运行。
adb
包含在 Android SDK 平台工具软件包中。您可以使用 SDK 管理器下载此软件包,该管理器会将其安装在/android_sdk/platform-tools/
下。
Android 调试桥 (adb) 是一种功能多样的命令行工具,可让您与设备进行通信。adb 命令可用于执行各种设备操作(例如安装和调试应用),并提供对 Unix shell(可用来在设备上运行各种命令)的访问权限。它是一种客户端-服务器程序,包括以下三个组件:
- 客户端:用于发送命令。客户端在开发计算机上运行。您可以通过发出 adb 命令来从命令行终端调用客户端。
- 守护进程 (adbd):在设备上运行命令。守护进程在每个设备上作为后台进程运行。
- 服务器:管理客户端和守护进程之间的通信。服务器在开发机器上作为后台进程运行。
adb
包含在 Android SDK 平台工具软件包中。您可以使用 SDK 管理器下载此软件包,该管理器会将其安装在 android_sdk/platform-tools/
下。
adb 的工作原理
当您启动某个 adb 客户端时,客户端会先检查是否有 adb 服务器进程正在运行。如果没有,它将启动服务器进程。服务器在启动后会与本地 TCP 端口 5037 绑定,并监听 adb 客户端发出的命令 - 所有 adb 客户端均通过端口 5037 与 adb 服务器通信。
然后,服务器会与所有正在运行的设备建立连接。它通过扫描 5555 到 5585 之间(该范围供前 16 个模拟器使用)的奇数号端口查找模拟器。服务器一旦发现 adb 守护进程 (adbd),便会与相应的端口建立连接。请注意,每个模拟器都使用一对按顺序排列的端口 - 用于控制台连接的偶数号端口和用于 adb 连接的奇数号端口。例如:
模拟器 1,控制台:5554
模拟器 1,adb:5555
模拟器 2,控制台:5556
模拟器 2,adb:5557
依此类推
如上所示,在端口 5555 处与 adb 连接的模拟器与控制台监听端口为 5554 的模拟器是同一个。
服务器与所有设备均建立连接后,您便可以使用 adb 命令访问这些设备。由于服务器管理与设备的连接,并处理来自多个 adb 客户端的命令,因此您可以从任意客户端(或从某个脚本)控制任意设备。
在设备上启用 adb 调试
要在通过 USB 连接的设备上使用 adb,您必须在设备的系统设置中启用 USB 调试(位于开发者选项下)。
在搭载 Android 4.2 及更高版本的设备上,“开发者选项”屏幕默认情况下处于隐藏状态。如需将其显示出来,请依次转到设置 > 关于手机,然后点按版本号七次。返回上一屏幕,在底部可以找到开发者选项。
在某些设备上,“开发者选项”屏幕所在的位置或命名方式可能有所不同。
现在,您已经可以通过 USB 连接设备。您可以通过从 android_sdk/platform-tools/ 目录执行 adb devices 来验证设备是否已连接。如果已连接,您将看到设备名称以“设备”形式列出。
通过 WLAN 连接到设备
一般情况下,adb 通过 USB 与设备进行通信,但您也可以在通过 USB 完成一些初始设置后通过 WLAN 使用 adb,如下所述。不过,如果您开发的是 Wear OS 应用,则应参阅调试 Wear OS 应用指南,其中提供了有关如何通过 WLAN 和蓝牙使用 adb 的特别说明。
- 将 Android 设备和 adb 主机连接到这两者都可以访问的同一 WLAN 网络。请注意,并非所有接入点都适用;您可能需要使用防火墙已正确配置为支持 adb 的接入点。
- 如果您要连接到 Wear OS 设备,请关闭与该设备配对的手机上的蓝牙。
- 使用 USB 数据线将设备连接到主机。
- 设置目标设备以监听端口 5555 上的 TCP/IP 连接。
adb tcpip 5555
- 拔掉连接目标设备的 USB 数据线。
- 找到 Android 设备的 IP 地址。例如,对于 Nexus 设备,您可以在设置 > 关于平板电脑(或关于手机)> 状态 > IP 地址下找到 IP 地址。或者,对于 Wear OS 设备,您可以在设置 > WLAN 设置 > 高级 > IP 地址下找到 IP 地址。
- 通过 IP 地址连接到设备。
adb connect device_ip_address
- 确认主机已连接到目标设备:
$ adb devices
--------------
List of devices attached
device_ip_address 5555 device
现在,您可以开始操作了!
如果 adb 连接断开:
- 确保主机仍与 Android 设备连接到同一个 WLAN 网络。
- 通过再次执行
adb connect
步骤重新连接。 - 如果上述操作未解决问题,重置 adb 主机:
adb kill-server
然后,从头开始操作。
一.基本命令
1.启动adb服务
adb start-server
2.停止adb服务
adb kill-server
3.查看adb版本
adb version
-----------
Android Debug Bridge version 1.0.41
Version 29.0.5-5949299
Installed as /Users/xxx/Library/Android/sdk/platform-tools/adb
4.以root权限重启adb
adb root
5.查看连接设备
adb devices
-----------
List of devices attached
ZL5228NGMC device
adb devices -l
--------------------
List of devices attached
ZL5228NGMF device usb:338821120X product:deen model:motorola_one device:deen_sprout transport_id:3
6.重启设备
adb reboot
7.重启到bootloader,即刷机模式
adb reboot bootloader
8.重启到recovery,即恢复模式
adb reboot recovery
9.获取mac地址(废弃)
adb shell cat /sys/class/net/wlan0/address
10.获取CPU序列号
adb shell cat /proc/cpuinfo
---------------------------
processor : 0
BogoMIPS : 38.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 1
BogoMIPS : 38.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 2
BogoMIPS : 38.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 3
BogoMIPS : 38.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 4
BogoMIPS : 38.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 5
BogoMIPS : 38.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 6
BogoMIPS : 38.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 7
BogoMIPS : 38.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
Hardware : Qualcomm Technologies, Inc MSM8953
Revision : c100
Serial : ec16b17c00000000
Device : deen
Radio : APAC
MSM Hardware : MSM8953
11.查看手机分辨率
adb shell wm size
-----------------
Physical size: 720x1520
adb shell "dumpsys window|grep mUnrestrictedScreen"
---------------------------------------------------
mUnrestrictedScreen=(0,0) 720x1520
12.获取手机物理密度
adb shell wm density
--------------------
Physical density: 320
13.查看手机系统android版本
adb shell getprop ro.build.version.release
------------------------------------------
8.1.0
adb shell getprop|grep version
------------------------------
[gsm.version.baseband]: [M8953_09.03.02.03R DEEN_APACDSDS_CUST]
[gsm.version.baseband1]: [M8953_09.03.02.03R DEEN_APACDSDS_CUST]
[gsm.version.ril-impl]: [Qualcomm RIL 1.0]
[persist.radio.mcfg_version]: [null,null]
[ril.baseband.config.version]: [DEEN_APACDSDS_CUST]
[ro.build.version.all_codenames]: [REL]
[ro.build.version.base_os]: [motorola/deen/deen_sprout:8.1.0/OPK28.63-18/141f:user/release-keys]
[ro.build.version.ci]: [3]
[ro.build.version.codename]: [REL]
[ro.build.version.full]: [Blur_Version.28.201.3.deen.retail.en.US]
[ro.build.version.incremental]: [1bd2]
[ro.build.version.preview_sdk]: [0]
[ro.build.version.qcom]: [LA.UM.6.6.r1-09000-89xx.0]
[ro.build.version.release]: [8.1.0]
[ro.build.version.sdk]: [27]
[ro.build.version.security_patch]: [2018-10-01]
[ro.com.google.gmsversion]: [8.1_201806]
[ro.hw.boardversion]: [PVT]
[ro.mot.build.version.release]: [28.201]
[ro.mot.build.version.sdk_int]: [28]
[ro.property_service.version]: [2]
14.获取手机系统API版本
adb shell getprop ro.build.version.sdk
--------------------------------------
27
15.查看手机型号
adb shell getprop ro.product.model
------------------------------
motorola one
adb shell getprop|grep product
------------------------------
[ro.boot.hab.product]: [deen]
[ro.build.product]: [deen_sprout]
[ro.mot.build.oem.product]: [deen]
[ro.mot.build.product.increment]: [201]
[ro.product.board]: [msm8953]
[ro.product.brand]: [motorola]
[ro.product.cpu.abi]: [arm64-v8a]
[ro.product.cpu.abilist]: [arm64-v8a,armeabi-v7a,armeabi]
[ro.product.cpu.abilist32]: [armeabi-v7a,armeabi]
[ro.product.cpu.abilist64]: [arm64-v8a]
[ro.product.device]: [deen_sprout]
[ro.product.display]: [motorola one]
[ro.product.first_api_level]: [27]
[ro.product.locale]: [en-US]
[ro.product.manufacturer]: [motorola]
[ro.product.model]: [motorola one]
[ro.product.name]: [deen]
[ro.vendor.product.brand]: [motorola]
[ro.vendor.product.device]: [deen_sprout]
[ro.vendor.product.manufacturer]: [motorola]
[ro.vendor.product.model]: [motorola one]
[ro.vendor.product.name]: [deen_retail]
16.获取手机支持的CPU架构
adb shell getprop ro.product.cpu.abilist
----------------------------------------
arm64-v8a,armeabi-v7a,armeabi
adb shell getprop ro.product.cpu.abilist32
------------------------------------------
armeabi-v7a,armeabi
adb shell getprop ro.product.cpu.abilist64
------------------------------------------
arm64-v8a
17.获取手机厂商名称
adb shell getprop ro.product.brand
--------------------------------------
motorola
18.获取序列号
adb get-serialno
----------------
ZL5228NGMC
19.获取设备id(android id)
adb shell settings get secure android_id
----------------------------------------
945c35f45e0e2ab1
20.获取手机存储信息
adb shell df
------------
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/root 2499580 1957736 525460 79% /
tmpfs 1816724 904 1815820 1% /dev
/dev/block/dm-1 499888 417452 72116 86% /vendor
tmpfs 1816724 0 1816724 0% /mnt
/dev/block/mmcblk0p20 96688 72884 21756 78% /firmware
/dev/block/mmcblk0p24 12016 7044 4648 61% /dsp
/dev/block/mmcblk0p31 7252 7244 0 100% /vendor/fsg
/dev/block/dm-2 186348 280 182200 1% /oem
/dev/block/mmcblk0p32 28144 608 26884 3% /persist
/dev/block/dm-3 53811180 32947380 20509516 62% /data
/data/media 53811180 32947380 20509516 62% /storage/emulated
二.应用操作相关
1.安装应用
adb install a.apk
adb -d install -r a.apk
adb -d DEVICE_ID install -r a.apk
注意:如果您在多个设备可用时发出命令但未指定目标设备,则 adb 会生成错误。
2.卸载应用
adb uninstall -k com.adb.test
adb shell pm uninstall --user 0 com.adb.test
3.获取应用信息
adb shell dumpsys package com.adb.test
4.查看应用安装路径
adb shell pm path com.adb.test
------------------------------
package:/data/app/com.adb.test-pzQ7XcpLM0vpKpfMvkosVg==/base.apk
5.查看应用版本信息
adb shell dumpsys package {your package name} | grep version
------------------------------------------------------------
versionCode=28 minSdk=21 targetSdk=28
versionName=0.7.0
6.启动应用
adb shell am start com.adb.test/.MainActivity
7.关闭应用
adb shell am force-stop com.adb.test
8.删除应用所有数据
adb shell pm clear com.adb.test
9.查看所有应用包名
adb shell pm list package
10.查看单个应用最大内存
adb shell getprop|grep heapgrowthlimit
--------------------------------------
[dalvik.vm.heapgrowthlimit]: [192m]
11,查看某个应用内存占用
adb shell dumpsys meminfo com.adb.test
--------------------------------------
Applications Memory Usage (in Kilobytes):
Uptime: 294830498 Realtime: 321900291
** MEMINFO in pid 9906 [com.adb.test] **
Pss Private Private SwapPss Heap Heap Heap
Total Dirty Clean Dirty Size Alloc Free
------ ------ ------ ------ ------ ------ ------
Native Heap 43168 43152 0 15384 108544 79642 28901
Dalvik Heap 3684 3524 112 23 6505 3253 3252
Dalvik Other 3984 3984 0 104
Stack 1756 1756 0 444
Ashmem 12 0 0 0
Gfx dev 18716 18716 0 0
Other dev 46 0 8 0
.so mmap 11309 4060 6280 250
.apk mmap 1 0 0 0
.ttf mmap 32 0 0 0
.dex mmap 2727 0 728 4
.oat mmap 355 0 76 0
.art mmap 4320 3952 32 507
Other mmap 155 8 144 0
EGL mtrack 13320 13320 0 0
Unknown 7276 7276 0 1683
TOTAL 129260 99748 7380 18399 115049 82895 32153
App Summary
Pss(KB)
------
Java Heap: 7508
Native Heap: 43152
Code: 11144
Stack: 1756
Graphics: 32036
Private Other: 11532
System: 22132
TOTAL: 129260 TOTAL SWAP PSS: 18399
Objects
Views: 74 ViewRootImpl: 2
AppContexts: 2 Activities: 1
Assets: 2 AssetManagers: 3
Local Binders: 28 Proxy Binders: 32
Parcel memory: 15 Parcel count: 60
Death Recipients: 1 OpenSSL Sockets: 2
WebViews: 0
SQL
MEMORY_USED: 0
PAGECACHE_OVERFLOW: 55 MALLOC_SIZE: 0
DATABASES
pgsz dbsz Lookaside(b) cache Dbname
4 44 33 18/25/3
/data/user/0/com.adb.test/databases/pri_wxop_tencent_analysis.db
4 44 109 47/74/20
/data/user/0/com.adb.test/databases/wxop_tencent_analysis.db
4 28 45
1329/22/2 /data/data/com.adb.test/database/pushinfo.db
三.文件操作相关
1.拷贝文件/目录到设备
adb push <local>...<remote>
2.从设备拷贝文件/目录,-a参数保留了文件的时间戳和模式
adb pull [-a] <remote>...<local>
3.查看设备log,和studio和eclipse的logcat相同,可通过参数控制输出的日志
adb logcat -s 过滤指定参数log -v time 保留日志时间 >> 追加写 > 覆盖写
下面的命令含义为:打印出log信息中的时间并且包含关键字“Test” 的所有log以覆盖写的方式保存到test文件
adb logcat -v time -s Test > test.txt
4.列出目录下的文件和文件夹,可选参数-al可查看文件和文件夹的详细信息
adb shell ls [-al]
5.进入文件夹
adb shell cd <folder>
6.查看文件
adb shell cat <filename>
7.重命名文件
adb shell rename path/oldfilename path/newfilename
8.删除文件/文件夹
adb shell rm path/filename -r 可选参数用于删除文件夹及下面的所有文件 eg:adb shell rm -r <folder>
9.移动文件
adb shell mv path/filename newpath/filename
10.拷贝文件
adb shell cp file newpath/file1
11.创建目录
adb shell mkdir path/folder
12.拷贝PC文件到sdcard
adb shell push /Users/xxx/Desktop/test.txt /sdcard/0
----------------------------------------------------
/Users/xxx/Desktop/test?...e pushed. 0.0 MB/s (669 bytes in 0.058s)
13.拷贝sdcard中文件到PC
adb pull /sdcard/0/test.txt /Users/xxx/Desktop/test/
------------------------------------------------
/sdcard/0/test.txt: 1 file pulled. 0.2 MB/s (669 bytes in 0.003s)
14、设置文件最高读写权限
adb shell chmod 777 filename
adb帮助
adb help
--------
Android Debug Bridge version 1.0.41
Version 29.0.5-5949299
Installed as /Users/xxx/Library/Android/sdk/platform-tools/adb
global options:
-a listen on all network interfaces, not just localhost
-d use USB device (error if multiple devices connected)
-e use TCP/IP device (error if multiple TCP/IP devices available)
-s SERIAL use device with given serial (overrides $ANDROID_SERIAL)
-t ID use device with given transport id
-H name of adb server host [default=localhost]
-P port of adb server [default=5037]
-L SOCKET listen on given socket for adb server [default=tcp:localhost:5037]
general commands:
devices [-l] list connected devices (-l for long output)
help show this help message
version show version num
networking:
connect HOST[:PORT] connect to a device via TCP/IP
disconnect [[HOST]:PORT] disconnect from given TCP/IP device, or all
forward --list list all forward socket connections
forward [--no-rebind] LOCAL REMOTE
forward socket connection using:
tcp:<port> (<local> may be "tcp:0" to pick any open port)
localabstract:<unix domain socket name>
localreserved:<unix domain socket name>
localfilesystem:<unix domain socket name>
dev:<character device name>
jdwp:<process pid> (remote only)
forward --remove LOCAL remove specific forward socket connection
forward --remove-all remove all forward socket connections
ppp TTY [PARAMETER...] run PPP over USB
reverse --list list all reverse socket connections from device
reverse [--no-rebind] REMOTE LOCAL
reverse socket connection using:
tcp:<port> (<remote> may be "tcp:0" to pick any open port)
localabstract:<unix domain socket name>
localreserved:<unix domain socket name>
localfilesystem:<unix domain socket name>
reverse --remove REMOTE remove specific reverse socket connection
reverse --remove-all remove all reverse socket connections from device
file transfer:
push [--sync] LOCAL... REMOTE
copy local files/directories to device
--sync: only push files that are newer on the host than the device
pull [-a] REMOTE... LOCAL
copy files/dirs from device
-a: preserve file timestamp and mode
sync [all|data|odm|oem|product|system|system_ext|vendor]
sync a local build from $ANDROID_PRODUCT_OUT to the device (default all)
-l: list files that would be copied, but don't copy them
shell:
shell [-e ESCAPE] [-n] [-Tt] [-x] [COMMAND...]
run remote shell command (interactive shell if no command given)
-e: choose escape character, or "none"; default '~'
-n: don't read from stdin
-T: disable PTY allocation
-t: force PTY allocation
-x: disable remote exit codes and stdout/stderr separation
emu COMMAND run emulator console command
app installation (see also `adb shell cmd package help`):
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
--local-agent: locate agent files from local source build (instead of SDK location)
(See also `adb shell pm help` for more options.)
uninstall [-k] PACKAGE
remove this app package from the device
'-k': keep the data and cache directories
debugging:
bugreport [PATH]
write bugreport to given PATH [default=bugreport.zip];
if PATH is a directory, the bug report is saved in that directory.
devices that don't support zipped bug reports output to stdout.
jdwp list pids of processes hosting a JDWP transport
logcat show device log (logcat --help for more)
security:
disable-verity disable dm-verity checking on userdebug builds
enable-verity re-enable dm-verity checking on userdebug builds
keygen FILE
generate adb public/private key; private key stored in FILE,
scripting:
wait-for[-TRANSPORT]-STATE
wait for device to be in the given state
STATE: device, recovery, rescue, sideload, bootloader, or disconnect
TRANSPORT: usb, local, or any [default=any]
get-state print offline | bootloader | device
get-serialno print <serial-number>
get-devpath print <device-path>
remount [-R]
remount partitions read-write. if a reboot is required, -R will
will automatically reboot the device.
reboot [bootloader|recovery|sideload|sideload-auto-reboot]
reboot the device; defaults to booting system image but
supports bootloader and recovery too. sideload reboots
into recovery and automatically starts sideload mode,
sideload-auto-reboot is the same but reboots after sideloading.
sideload OTAPACKAGE sideload the given full OTA package
root restart adbd with root permissions
unroot restart adbd without root permissions
usb restart adbd listening on USB
tcpip PORT restart adbd listening on TCP on PORT
internal debugging:
start-server ensure that there is a server running
kill-server kill the server if it is running
reconnect kick connection from host side to force reconnect
reconnect device kick connection from device side to force reconnect
reconnect offline reset offline/unauthorized devices to force reconnect
environment variables:
$ADB_TRACE
comma-separated list of debug info to log:
all,adb,sockets,packets,rwx,usb,sync,sysdeps,transport,jdwp
$ADB_VENDOR_KEYS colon-separated list of keys (files or directories)
$ANDROID_SERIAL serial number to connect to (see -s)
$ANDROID_LOG_TAGS tags to be used by logcat (see logcat --help)
$ADB_LOCAL_TRANSPORT_MAX_PORT max emulator scan port (default 5585, 16 emus)
参考:https://developer.android.google.cn/studio/command-line/adb
网友评论