1.What is ADB?
参考Android 调试桥 (adb) | Android 开发者 | Android Developers (google.cn)
Android 调试桥 (adb) 是一种功能多样的命令行工具,可让您与设备进行通信。adb 命令可用于执行各种设备操作(例如安装和调试应用),并提供对 Unix shell(可用来在设备上运行各种命令)的访问权限。它是一种客户端-服务器程序,包括以下三个组件:
为何要用网络调试?
比如,手机调试,只需要usb 数据线连接,然后,手机开启debug 模式,就可以调试了,
但是,有些Android 终端,无法通过usb 数据线连接,此时,就可以通过adb连接,然后调试。
CTS/VTS/GTS 也需要网络连接。
2.怎么确认工作台上是否有可以使用的adb?
在工作台打开命令窗口,输入adb,如果显示下面的信息,就表示可以使用adb.
比如,我的台式机windows 命令窗口输入adb 后提示下面的信息,其表示我的windows 环境已经安装了adb,可以直接使用。
如果没有可用的adb ,则需要安装adb才能使用。adb
包含在 Android SDK 平台工具软件包中。您可以使用 SDK 管理器下载此软件包,该管理器会将其安装在 android_sdk/platform-tools/
下。或者,如果您需要独立的 Android SDK 平台工具软件包,也可以点击此处进行下载https://developer.android.google.cn/studio/releases/platform-tools?hl=zh-cn。
推荐使用Android SDK 管理器下载platform-tools。
3.adb 工作原理。
参考Android 调试桥 (adb) | Android 开发者 | Android Developers (google.cn)https://developer.android.google.cn/studio/command-line/adb?hl=zh-cn
当您启动某个 adb 客户端时,该客户端会先检查是否有 adb 服务器进程正在运行。如果没有,它会启动服务器进程。服务器在启动后会与本地 TCP 端口 5037 绑定,并监听 adb 客户端发出的命令 - 所有 adb 客户端均通过端口 5037 与 adb 服务器通信。
没有启动adb server 时,自动启动adb server。
启动adb server后,执行adb client 端命令,
然后,服务器会与所有正在运行的设备建立连接。它通过扫描 5555 到 5585 之间(该范围供前 16 个模拟器使用)的奇数号端口查找模拟器。服务器一旦发现 adb 守护程序 (adbd),便会与相应的端口建立连接。
android系统底层运行着一个adb守护进程(adbd),用于响应和管理在电脑端的adb命令连接,
这个服务在启动时会根据设备监听端口的配置,以确认监听USB连接还是网络连接。
服务器与所有设备均建立连接后,您便可以使用 adb 命令访问这些设备。由于服务器管理与设备的连接,并处理来自多个 adb 客户端的命令,因此您可以从任意客户端(或从某个脚本)控制任意设备。
如果连接失败,可以查看以下2个关键点是否正常?
(1)开发设备端口确认
端口配置的属性为:
service.adb.tcp.port <- 监听的网络端口
以上属性的值 > 0 : adbd将监听网络对应的端口(一般为5555)
以上属性的值 <=-1 : adbd将监听USB连接
5555端口是安卓adb服务默认监听的端口
比如我的开发设备获取的属性是5555,则表示开发设备在监听网络连接,监听网络端口为5555.
(实际测试结果是,只要start adbd 就可以adb connect ip 成功,默认会指定5555这个端口)
开发设备console 输入stop adbd, 然后pc 执行adb connect 就会failed.
开发设备console 输入start adbd, 然后pc 执行adb connect 就会成功.
adbd服务需要开启5555号端口来建立adb的连接,如果未开启5555端口,则不能通过网络调试。
怎么确认终端是否开启5555端口号?
#netstat
如果显示有5555端口号,则表示有开启,否则表示没有开启。
如果,没有开启,通过以下命令,可以开启5555端口号。
# stop adbd
# setprop service.adb.tcp.port 5555
# start adbd
然后,可以再输入#netstat,确认一下。
确认5555开启后,就可以通过 adb connect ip:5555 连接 终端设备了。
ps:adbd 开机时不一定会启动,不再详述。
130|console:/data # netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost:48064 localhost:5555 ESTABLISHED
tcp 0 0 192.168.10.3:35457 ecs-114-115-188-8:12998 ESTABLISHED
tcp 0 0 localhost:48196 localhost:5555 ESTABLISHED
tcp6 0 32 ::ffff:192.168.10:58582 ptr173.33.51ywx.c:https FIN_WAIT1
tcp6 0 32 ::ffff:192.168.10:32958 ecs-49-4-37-159.c:https FIN_WAIT1
tcp6 0 25 ::ffff:192.168.10:55872 ecs-49-4-35-104.c:https FIN_WAIT1
tcp6 0 0 ::ffff:192.168.10:37136 ::ffff:111.40.174:https ESTABLISHED
tcp6 0 0 localhost:5555 localhost:48064 ESTABLISHED
tcp6 0 32 ::ffff:192.168.10:51622 ecs-49-4-32-32.co:https FIN_WAIT1
tcp6 0 0 ::ffff:192.168.10:37138 ::ffff:111.40.174:https ESTABLISHED
tcp6 0 25 ::ffff:192.168.10:55618 ecs-49-4-35-104.c:https FIN_WAIT1
tcp6 0 0 2409:8a71:ac5:952:60810 240e:bf:b800:57c::https TIME_WAIT
tcp6 0 0 localhost:5555 localhost:48196 ESTABLISHED
tcp6 0 0 ::ffff:192.168.10.:5555 ::ffff:192.168.10:51007 ESTABLISHED
tcp6 0 32 ::ffff:192.168.10:60940 ecs-49-4-37-159.c:https FIN_WAIT1
tcp6 1 1 ::ffff:192.168.10:48992 :http LAST_ACK
tcp6 0 32 ::ffff:192.168.10:41130 ecs-117-78-15-45.:https FIN_WAIT1
tcp6 0 0 ::ffff:192.168.10:33558 ecs-49-4-33-122.c:https ESTABLISHED
tcp6 0 32 ::ffff:192.168.10:51618 ecs-49-4-32-32.co:https FIN_WAIT1
tcp6 0 32 ::ffff:192.168.10:51620 ecs-49-4-32-32.co:https FIN_WAIT1
tcp6 0 0 ::ffff:192.168.10:37132 ::ffff:111.40.174:https ESTABLISHED
tcp6 0 32 ::ffff:192.168.10:41140 ecs-117-78-15-45.:https FIN_WAIT1
tcp6 0 0 ::ffff:192.168.10:50956 ecs-117-78-15-45.:https ESTABLISHED
tcp6 0 32 ::ffff:192.168.10:47152 ecs-117-78-15-49.:https FIN_WAIT1
tcp6 1 1 ::ffff:192.168.10:49054 :http LAST_ACK
tcp6 0 0 ::ffff:192.168.10:53850 ::ffff:111.21.8.1:https ESTABLISHED
tcp6 0 0 ::ffff:192.168.10:32950 ::ffff:111.21.8.1:https ESTABLISHED
tcp6 0 0 2409:8a71:ac5:952:45570 2407:c080:1400:77::5223 ESTABLISHED
10424 是占用进程的pid,在任务管理器查到对应的pid ,可以看到正是工作站的adb 在使用。如果是其他程序在使用,需要把其他占用释放掉。
4.接下来,就可以连接,并且Debug了。
please reference at "通过 Wi-Fi 连接到设备"
Android 调试桥 (adb) | Android 开发者 | Android Developers (google.cn)
adb connect [ip]:[port]
adb shell logcat *:D (打开D以上的信息)
adb shell top
等等
adb shell 就可以进入到开发设备的console 界面,
要退出交互式 shell,请按 Ctrl + D 键或输入
exit
在设备console 输入exit可以退出。
5.常用命令解释
1)连接设备
adb connect [ip]:[port]
2)查看有哪些device 连接?
adb devices
3)查看adb version 信息
adb version
4)查看帮助信息
adb --help
5)指定特定设备
-s [ip]:[port]
6)断开连接
adb disconnect [ip]
7)更多命令,可以参考http://zmywly8866.github.io/2015/01/24/all-adb-command.html
6.如果工作站pc 的adb 端口被占用?
adb 默认端口号是5037.
当然,可以杀死占用的进程,但是,如果不能够杀死的话呢?
Linux:
~$ adb kill-server
~$ export ANDROID_ADB_SERVER_PORT=54321
~$ adb start-server
* daemon not running; starting now at tcp:54321
* daemon started successfully
windows:
新建一个环境变量,名字为ANDROID_ADB_SERVER_PORT,设定成新的端口号.
可以参考
https://blog.csdn.net/Jack_PengPeng/article/details/48895197
https://bbs.csdn.net/topics/390870699。
7.Reference
https://www.jianshu.com/p/225fef8c6e67
http://zmywly8866.github.io/2015/01/24/all-adb-command.html
https://www.jianshu.com/p/5980c8c282ef
8. 附上adb 命令的使用说明
Android Debug Bridge version 1.0.41
Version 29.0.5-5949299
Installed as D:\android_perf\platform-tools_r29.0.5-windows\platform-tools\adb.exe
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: ( may be "tcp:0" to pick any open port)
localabstract:
localreserved:
localfilesystem:
dev:
jdwp: (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: ( may be "tcp:0" to pick any open port)
localabstract:
localreserved:
localfilesystem:
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
(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
get-devpath print
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)