『移动端测试必备』ADB常用命令和使用

2023-11-28 09:25:37 浏览数 (1)

1 关于ADB

  • ADB-Android Debug Bridge,起到调试桥的作用;
  • 通过adb我们可以在Eclipse中方便通过DDMS来调试Android程序;
  • adb是一种客户端、服务器应用程序,采用CS架构;
  • adb包括三个组件:客户端、守护进程(adbd)、服务器。

2 abd环境

  • 需要将adb所在的目录加入系统环境变量中:
  • 之前我们已经安装了android-sdk-windows,直接将platform-tools目录加入系统环境变量即可;
代码语言:python代码运行次数:0复制
D:android-sdk-windowsplatform-tools
在这里插入图片描述在这里插入图片描述
  • 如下:
在这里插入图片描述在这里插入图片描述
  • 命令行输入adb看到如下信息,说明adb环境是ok的:
代码语言:python代码运行次数:0复制
C:UsersAdministrator>adb
Android Debug Bridge version 1.0.41
Version 29.0.6-6198805
Installed as D:android-sdk-windowsplatform-toolsadb.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版本:
代码语言:python代码运行次数:0复制
adb version
在这里插入图片描述在这里插入图片描述
  • 启动adb服务:
代码语言:python代码运行次数:0复制
adb start-server
  • 停止adb服务:
代码语言:python代码运行次数:0复制
adb kill-server

3.2 查看连接的设备

代码语言:python代码运行次数:0复制
adb devices
在这里插入图片描述在这里插入图片描述
  • 这里我们启动了一个安卓模拟器,所以显示了这个设备是连接上的; 在这里插入图片描述在这里插入图片描述

3.3 获取设备的连接状态

  • 使用adb devicesattached即表示连接状态; 在这里插入图片描述在这里插入图片描述
  • 连接状态有以下几种:

状态

说明

offline

离线状态

device

成功获取到设备

no device

没有连接的设备

unauthorized

没有授权

3.4 复制文件PC

  • 如何复制设备中的文件到PC端?
  • 先用以下命令进入设备:adb shell在这里插入图片描述在这里插入图片描述
  • sdcard中新建一个文件如test.txt: 在这里插入图片描述在这里插入图片描述
  • 复制这个文件到PC端:
代码语言:python代码运行次数:0复制
adb pull /sdcard/test.txt .
  • 如下就会把设备中的test.txt复制PC端当前目录: 在这里插入图片描述在这里插入图片描述
在这里插入图片描述在这里插入图片描述

3.5 复制PC文件到设备

  • PC端当前目录新建一个test01.txt
  • 使用命令:
代码语言:python代码运行次数:0复制
adb push test01.txt /sdcard/
  • 那么在设备中就存在了这个文件: 在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

3.6 进入设备

  • 上边已经提及过,就是使用:adb shell在这里插入图片描述在这里插入图片描述

3.7 查看应用

  • 查看所有应用:
代码语言:python代码运行次数:0复制
adb shell pm list package
代码语言:python代码运行次数:0复制
C:UsersAdministrator>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.vpndialogs
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
  • 查看系统应用:
代码语言:python代码运行次数:0复制
adb shell pm list package -s
  • 查看第三方应用:
代码语言:python代码运行次数:0复制
adb shell pm list package -3
在这里插入图片描述在这里插入图片描述

3.8 查看具体包

  • 比如通过过滤我们查看某些应用列表:
代码语言:python代码运行次数:0复制
adb shell pm list package bluetooth
在这里插入图片描述在这里插入图片描述

3.9 安装卸载APK

  • 命令为:
代码语言:python代码运行次数:0复制
 adb install [-lrtsdg] [--instant] PACKAGE
在这里插入图片描述在这里插入图片描述
  • 卸载应用使用:
代码语言:python代码运行次数:0复制
 adb uninstall [-k] PACKAGE
在这里插入图片描述在这里插入图片描述

3.10 清除应用数据

  • 直接使用:
代码语言:python代码运行次数:0复制
adb shell pm clear <pachagename>

3.11 查看耗电量和CPU信息

  • 查看耗电量:
代码语言:python代码运行次数:0复制
adb shell dumpsys batterystats
代码语言:python代码运行次数:0复制
C:UsersAdministrator>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
  • 查看CPU信息:
代码语言:python代码运行次数:0复制
adb shell dumpsys cpuinfo
代码语言:python代码运行次数:0复制
C:UsersAdministrator>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.12 启动app

代码语言:python代码运行次数:0复制
adb shell am start -n 包名

3.13 获取日志

代码语言:python代码运行次数:0复制
adb shell logcat 存放路径
代码语言:python代码运行次数:0复制
adb shell logcat > ./adb_log.txt
在这里插入图片描述在这里插入图片描述
  • 关于日志的级别如下:

级别

说明

V(Verbose)

级别最低,所有日志信息

D(Debug)

调试日志

I(Info)

一般日志

W(Warning)

警告日志

E(Error)

错误日志

S(Silent)

最高级别,什么都不输出

3.14 事件操作

  • 比如完成鼠标键盘的操作,使用input命令:
代码语言:python代码运行次数:0复制
adb shell input keyevent keycode值
adb shell input swipe x1 y1 x2 y2

3.15 查看设备详细信息

  • 设备型号:
代码语言:python代码运行次数:0复制
adb shell getprop ro.product.model
在这里插入图片描述在这里插入图片描述
  • 查看分辨率:
代码语言:python代码运行次数:0复制
adb shell wm size
在这里插入图片描述在这里插入图片描述
  • 查看屏幕密度:
代码语言:python代码运行次数:0复制
adb shell wm density
在这里插入图片描述在这里插入图片描述
  • 查看屏幕参数:
代码语言:python代码运行次数:0复制
adb shell dumpsys window displays
在这里插入图片描述在这里插入图片描述
  • 查看手机ID
代码语言:python代码运行次数:0复制
adb shell settings get secure android_id
在这里插入图片描述在这里插入图片描述
  • 查看系统版本:
代码语言:python代码运行次数:0复制
adb shell getprop ro.build.version.release
在这里插入图片描述在这里插入图片描述
  • 查看ip地址:
代码语言:python代码运行次数:0复制
adb shell ifconfig
在这里插入图片描述在这里插入图片描述
  • 获取网络连接信息:

4 adb --help

  • 所有命令可以使用adb --help查看:
代码语言:python代码运行次数:0复制
C:UsersAdministrator>adb --help
Android Debug Bridge version 1.0.41
Version 29.0.6-6198805
Installed as D:android-sdk-windowsplatform-toolsadb.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)

我正在参与2023腾讯技术创作特训营第三期有奖征文,组队打卡瓜分大奖!

0 人点赞