『移动端测试必备』ADB常用命令和使用
- 1 关于ADB
- 2 abd环境
- 3 常用命令
-
- 3.1 adb服务命令
- 3.2 查看连接的设备
- 3.3 获取设备的连接状态
- 3.4 复制文件PC
- 3.5 复制PC文件到设备
- 3.6 进入设备
- 3.7 查看应用
- 3.8 查看具体包
- 3.9 安装卸载APK
- 3.10 清除应用数据
- 3.11 查看耗电量和CPU信息
- 3.11 启动app
- 3.12 获取日志
- 3.13 事件操作
- 3.14 查看设备详细信息
- 4 adb --help
1 关于ADB
ADB-Android Debug Bridge
,起到调试桥的作用;
- 通过
adb
我们可以在Eclipse
中方便通过DDMS
来调试Android
程序;
adb
是一种客户端、服务器应用程序,采用CS
架构;
adb
包括三个组件:客户端、守护进程(adbd
)、服务器。
2 abd环境
- 需要将
adb
所在的目录加入系统环境变量中:
- 之前我们已经安装了
android-sdk-windows
,直接将platform-tools
目录加入系统环境变量即可;
D:\android-sdk-windows\platform-tools
- 命令行输入
adb
看到如下信息,说明adb
环境是ok
的:
C:\Users\Administrator>adb
Android Debug Bridge version 1.0.41
Version 29.0.6-6198805
Installed as D:\android-sdk-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:
3 常用命令
3.1 adb服务命令
adb version
adb start-server
adb kill-server
3.2 查看连接的设备
adb devices
- 这里我们启动了一个安卓模拟器,所以显示了这个设备是连接上的;
3.3 获取设备的连接状态
状态 |
说明 |
offline |
离线状态 |
device |
成功获取到设备 |
no device |
没有连接的设备 |
unauthorized |
没有授权 |
3.4 复制文件PC
- 如何复制设备中的文件到
PC
端?
- 先用以下命令进入设备:
adb shell
:
- 在
sdcard
中新建一个文件如test.txt
:
- 复制这个文件到
PC
端:
adb pull /sdcard/test.txt .
- 如下就会把设备中的
test.txt
复制PC
端当前目录:
3.5 复制PC文件到设备
- 在
PC
端当前目录新建一个test01.txt
;
- 使用命令:
adb push test01.txt /sdcard/
- 那么在设备中就存在了这个文件:
3.6 进入设备
- 上边已经提及过,就是使用:
adb shell
:
3.7 查看应用
adb shell pm list package
C:\Users\Administrator>adb shell pm list package
package:com.google.android.katniss
package:com.android.providers.telephony
package:com.android.providers.calendar
package:com.android.tv.settings
package:com.lego.android.tvleanback
package:com.android.providers.media
package:com.android.externalstorage
package:com.android.htmlviewer
package:com.android.providers.downloads
package:com.android.providers.tv
package:com.google.android.pano.packageinstaller
package:com.google.android.backdrop
package:com.google.android.configupdater
package:com.android.defcontainer
package:com.android.vending
package:com.android.pacprocessor
package:com.android.certinstaller
package:com.android.carrierconfig
package:com.google.android.marvin.talkback
package:com.google.android.leanback.ime
package:android
package:com.android.backupconfirm
package:com.google.android.tv.remotepairing
package:com.android.statementservice
package:com.google.android.tv
package:com.google.android.gsf.notouch
package:com.android.providers.settings
package:com.android.sharedstoragebackup
package:com.android.printspooler
package:com.android.dreams.basic
package:com.android.inputdevices
package:com.android.backuptester
package:com.android.sdksetup
package:com.google.android.webview
package:com.google.android.syncadapters.contacts
package:com.android.keychain
package:com.android.emulator.smoketests
package:com.google.android.gms
package:com.google.android.gsf
package:com.google.android.sss
package:com.google.android.tts
package:com.android.packageinstaller
package:com.android.proxyhandler
package:com.google.android.tungsten.overscan
package:com.android.netspeed
package:com.google.android.leanbacklauncher
package:com.google.android.backuptransport
package:com.google.android.sss.authbridge
package:com.android.dialogs
package:com.android.shell
package:com.android.providers.userdictionary
package:com.android.location.fused
package:com.android.systemui
package:com.android.bluetooth
package:com.android.development
package:com.android.providers.contacts
adb shell pm list package -s
adb shell pm list package -3
3.8 查看具体包
adb shell pm list package bluetooth
3.9 安装卸载APK
adb install [-lrtsdg] [--instant] PACKAGE
adb uninstall [-k] PACKAGE
3.10 清除应用数据
adb shell pm clear <pachagename>
3.11 查看耗电量和CPU信息
adb shell dumpsys batterystats
C:\Users\Administrator>adb shell dumpsys batterystats
Battery History (0% used, 580 used of 256KB, 5 strings using 322):
0 (9) RESET:TIME: 2023-11-27-06-46-44
0 (2) 050 status=charging health=good plug=ac temp=0 volt=0 +running +plugged +charging
+1s922ms (2) 050 +screen
+1s922ms (3) 050 +wake_lock=-1:"screen" brightness=bright
+1s922ms (2) 050 brightness=medium
+3s504ms (2) 050 +user=0:"0"
+3s504ms (2) 050 +userfg=0:"0"
+3s508ms (3) 050 +audio +top=u0a10:"com.google.android.leanbacklauncher"
+4s586ms (2) 050 -audio
+4s618ms (2) 050 +audio
+4s753ms (2) 050 -audio
+4s793ms (2) 050 +audio
+4s856ms (2) 050 -audio
+5s092ms (2) 050 +audio
+5s133ms (2) 050 -audio
+5s215ms (2) 050 +audio
+5s235ms (2) 050 -audio
+5s500ms (2) 050 +audio
+5s524ms (2) 050 -audio
+5s647ms (2) 050 +audio
+5s949ms (2) 050 -audio
+24s228ms (9) TIME: 2023-11-27-06-47-06
+26s153ms (2) 050 +tmpwhitelist=u0a8:"broadcast:u0a8:com.google.android.intent.action.GCM_RECONNECT"
+41s653ms (2) 050 -tmpwhitelist=u0a8:"broadcast:u0a8:com.google.android.intent.action.GCM_RECONNECT"
+58s370ms (3) 050 brightness=dark +tmpwhitelist=u0a8:"broadcast:u0a8:com.google.android.intent.action.GCM_RECONNECT"
+1m08s425ms (2) 050 -tmpwhitelist=u0a8:"broadcast:u0a8:com.google.android.intent.action.GCM_RECONNECT"
+1m58s462ms (2) 050 +tmpwhitelist=u0a8:"broadcast:u0a8:com.google.android.intent.action.GCM_RECONNECT"
+2m06s262ms (2) 050 brightness=medium
+2m08s519ms (2) 050 -tmpwhitelist=u0a8:"broadcast:u0a8:com.google.android.intent.action.GCM_RECONNECT"
+3m38s536ms (2) 050 +tmpwhitelist=u0a8:"broadcast:u0a8:com.google.android.intent.action.GCM_RECONNECT"
+3m48s600ms (2) 050 -tmpwhitelist=u0a8:"broadcast:u0a8:com.google.android.intent.action.GCM_RECONNECT"
+5m10s201ms (2) 050 brightness=dark
+5m31s586ms (2) 050 brightness=medium
+5m50s977ms (2) 050 -top=u0a10:"com.google.android.leanbacklauncher"
+5m50s977ms (2) 050 +top=1000:"com.android.tv.settings"
+6m18s901ms (2) 050 -top=1
adb shell dumpsys cpuinfo
C:\Users\Administrator>adb shell dumpsys cpuinfo
Load: 0.22 / 0.07 / 0.06
CPU usage from 704709ms to 682530ms ago:
1.3% 1208/surfaceflinger: 0.1% user + 1.1% kernel
0.9% 1548/system_server: 0% user + 0.9% kernel / faults: 120 minor
0.8% 1248/adbd: 0% user + 0.8% kernel / faults: 439 minor
0% 2140/com.google.android.gms: 0% user + 0% kernel / faults: 844 minor
0% 8/rcu_preempt: 0% user + 0% kernel
0% 1175/kworker/0:1H: 0% user + 0% kernel
0% 1186/logd: 0% user + 0% kernel / faults: 3 minor
0% 1200/healthd: 0% user + 0% kernel
0% 1202/lmkd: 0% user + 0% kernel
0% 1909/com.google.android.gms.persistent: 0% user + 0% kernel / faults: 3 minor
0.8% TOTAL: 0% user + 0.7% kernel + 0% iowait + 0% softirq
3.11 启动app
adb shell am start -n 包名
3.12 获取日志
adb shell logcat 存放路径
adb shell logcat > ./adb_log.txt
级别 |
说明 |
V(Verbose) |
级别最低,所有日志信息 |
D(Debug) |
调试日志 |
I(Info) |
一般日志 |
W(Warning) |
警告日志 |
E(Error) |
错误日志 |
S(Silent) |
最高级别,什么都不输出 |
3.13 事件操作
adb shell input keyevent keycode值
adb shell input swipe x1 y1 x2 y2
3.14 查看设备详细信息
adb shell getprop ro.product.model
adb shell wm size
adb shell wm density
adb shell dumpsys window displays
adb shell settings get secure android_id
adb shell getprop ro.build.version.release
adb shell ifconfig
4 adb --help
C:\Users\Administrator>adb --help
Android Debug Bridge version 1.0.41
Version 29.0.6-6198805
Installed as D:\android-sdk-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:<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)
acceptfd:<fd> (listen 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: allocate a pty if on a tty (-tt: 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 <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)