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

本文涉及的产品
阿里云百炼推荐规格 ADB PostgreSQL,4核16GB 100GB 1个月
日志服务 SLS,月写入数据量 50GB 1个月
简介: 『移动端测试必备』ADB常用命令和使用

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版本:
adb version

在这里插入图片描述

  • 启动adb服务:
adb start-server
  • 停止adb服务:
adb kill-server

3.2 查看连接的设备

adb devices

在这里插入图片描述

  • 这里我们启动了一个安卓模拟器,所以显示了这个设备是连接上的;
    在这里插入图片描述

    3.3 获取设备的连接状态

  • 使用adb devicesattached即表示连接状态;
    在这里插入图片描述

  • 连接状态有以下几种:

状态 说明
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.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
  • 查看系统应用:
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
  • 查看CPU信息:
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 事件操作

  • 比如完成鼠标键盘的操作,使用input命令:
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

在这里插入图片描述

  • 查看手机ID
adb shell settings get secure android_id

在这里插入图片描述

  • 查看系统版本:
adb shell getprop ro.build.version.release

在这里插入图片描述

  • 查看ip地址:
adb shell ifconfig

在这里插入图片描述

  • 获取网络连接信息:

4 adb --help

  • 所有命令可以使用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)
相关实践学习
阿里云百炼xAnalyticDB PostgreSQL构建AIGC应用
通过该实验体验在阿里云百炼中构建企业专属知识库构建及应用全流程。同时体验使用ADB-PG向量检索引擎提供专属安全存储,保障企业数据隐私安全。
AnalyticDB PostgreSQL 企业智能数据中台:一站式管理数据服务资产
企业在数据仓库之上可构建丰富的数据服务用以支持数据应用及业务场景;ADB PG推出全新企业智能数据平台,用以帮助用户一站式的管理企业数据服务资产,包括创建, 管理,探索, 监控等; 助力企业在现有平台之上快速构建起数据服务资产体系
目录
相关文章
|
17天前
|
机器学习/深度学习 弹性计算 自然语言处理
前端大模型应用笔记(二):最新llama3.2小参数版本1B的古董机测试 - 支持128K上下文,表现优异,和移动端更配
llama3.1支持128K上下文,6万字+输入,适用于多种场景。模型能力超出预期,但处理中文时需加中英翻译。测试显示,其英文支持较好,中文则需改进。llama3.2 1B参数量小,适合移动端和资源受限环境,可在阿里云2vCPU和4G ECS上运行。
|
27天前
|
弹性计算 安全 Linux
阿里云国际版使用ping命令测试ECS云服务器不通的排查方法
阿里云国际版使用ping命令测试ECS云服务器不通的排查方法
|
26天前
|
分布式计算 监控 Hadoop
Hadoop-29 ZooKeeper集群 Watcher机制 工作原理 与 ZK基本命令 测试集群效果 3台公网云服务器
Hadoop-29 ZooKeeper集群 Watcher机制 工作原理 与 ZK基本命令 测试集群效果 3台公网云服务器
35 1
|
2月前
|
关系型数据库 MySQL 数据库
6-2|测试连接数据库的命令
6-2|测试连接数据库的命令
|
2月前
|
人工智能 测试技术 Python
基于 LangChain 的自动化测试用例的生成与执行
本章节详细介绍了如何利用人工智能技术自动化完成Web、App及接口测试用例的生成与执行过程,避免了手动粘贴和调整测试用例的繁琐操作。通过封装工具包与Agent,不仅提升了测试效率,还实现了从生成到执行的一体化流程。应用价值在于显著节省时间并提高测试自动化水平。
82 1
|
2月前
|
测试技术
基于LangChain手工测试用例转App自动化测试生成工具
在传统App自动化测试中,测试工程师需手动将功能测试用例转化为自动化用例。市面上多数产品通过录制操作生成测试用例,但可维护性差。本文探讨了利用大模型直接生成自动化测试用例的可能性,介绍了如何使用LangChain将功能测试用例转换为App自动化测试用例,大幅节省人力与资源。通过封装App底层工具并与大模型结合,记录执行步骤并生成自动化测试代码,最终实现高效自动化的测试流程。
66 4
|
2月前
|
Shell
|
3月前
|
测试技术
基于LangChain手工测试用例转Web自动化测试生成工具
该方案探索了利用大模型自动生成Web自动化测试用例的方法,替代传统的手动编写或录制方式。通过清晰定义功能测试步骤,结合LangChain的Agent和工具包,实现了从功能测试到自动化测试的转换,极大提升了效率。不仅减少了人工干预,还提高了测试用例的可维护性和实用性。
76 4
|
3月前
【Application Insights】使用Powershell命令向Application Insgihts发送测试数据
【Application Insights】使用Powershell命令向Application Insgihts发送测试数据
|
3月前
|
JSON Kubernetes Linux
【Application Insights】使用CURL命令向Application Insgihts发送测试数据
【Application Insights】使用CURL命令向Application Insgihts发送测试数据