adb必须记住的命令

ADB,即 Android Debug Bridge,它是 Android 开发/测试人员不可替代的强大工具,也是 Android 设备玩家的好玩具。
完整的ADB工具包,总共有四个文件,两个exe后缀,两个dll后缀。
完整的工具包里面会有fastboot文件,是exe后缀的。使用之前,打开工具包看一下就行了,这就叫确认有fastboot,如果没有,那就不是完整的工具包
这个是Google提供的ADB工具包,

  • adb.exe
  • AdbWinApi.dll
  • AdbWinUsbApi.dll
  • fastboot.exe

前提:安装好手机驱动,打开开发者选项,点允许usb调试
说明:只是记录本人掌握的命令,没有掌握的则用英文代替了。
参考资料:https://github.com/mzlogin/awesome-adb
https://developer.android.com/studio/command-line/adb?hl=zh-cn

全局的参数:

  • -a: listen on all network interfaces, not just localhost
  • -d:如果出现多个设备连接的话,使用这个会指定其中一个设备
  • -e: 如果出现多个设备连接的话,使用ip的设备
  • -s SERIAL:使用序列号来连接设备,经常用(adb -s xxx shell)。
  • -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]

如果是模拟器的话,会有emulator这个标识。

基本命令

  • adb help :adb帮助

  • adb devices [-l]: 查看连接的设备(设备使用的序列号,是唯一的),如果没有开启服务的话,会自动开启,并查看设备。这样可以查看到对应tcp端口,一般是5037端口。使用-l会有更多的输出,比如品牌手机型号,传输id

  • adb version :查看adb的版本信息和安装文件的路径,当前版本是1.0.40

  • adb get-state , 获取设备的状态
    设备的状态有 3 钟,device , offline , unknown

    • device:设备正常连接
    • offline:连接出现异常,设备无响应
    • unknown:没有连接设备

adb网络命令

  • adb connect HOST[:PORT] : 通过TCP/IP连接到设备,默认端口都是5555,连接前如果出现计算机积极拒绝,无法连接10061,需要使用adb tcpip 5555,重置下设备的端口号,再重新连接。必须先用usb线连接上,再使用ip连接。
  • adb disconnect [HOST[:PORT]] : 断开指定设备,或者全部。
  • 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

adb文件命令

  • adb push : 复制电脑上的文件到目标设备上。
    • –sync: only push files that are newer on the host than the device
    • 例子:adb push D:\test.txt /data/local/tmp/
  • adb pull []:从设备上复制文件下来
    • -a: preserve file timestamp and mode
    • 例子:adb pull /data/local/tmp/test.txt D:/1/6
    • 下载下拉的window目录一定是存在目录,因为不会自己自动创建的
  • adb sync [] [-l] : 同步目录,该操作只针对电脑到设备上的同步,
    • -l代表只罗列不复制。

adb shell 执行远程命令

  • adb shell :运行shell脚本,command为参数
  • 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
    此时命令行提示符是 $ 则表示没有 root 权限,是 # 则表示已 root。

adb 相关app安装卸载命令

  • adb install [文件名字]
    • 不需要root权限。
    • 说明:安装应用包的apk文件。adb install [E:\demo.apk]
    • 如果已经安装了,就可以使用adb install -r [E:\demo.apk],就相当于升级安装了,
  • adb uninstall [文件包名]
    • 不需要root权限。
    • 说明:卸载应用包的apk文件。adb install [com.zsj.demo]

adb backup/restore

  • to show usage run “adb shell bu help”

adb debugging

  • adb logcat:查看设备的日志,会一直不停的输出,取消使用Ctrl+C就会停止。

    • -b 加载一个可使用的日志缓冲区供查看,比如event 和radio . 默认值是main 。具体查看Viewing Alternative Log Buffers.
      -c 清楚屏幕上的日志.
      -d 输出日志到屏幕上.
      -f 指定输出日志信息的 ,默认是stdout .
      -g 输出指定的日志缓冲区,输出后退出.
      -n 设置日志的最大数目 .,默认值是4,需要和 -r 选项一起使用。
      -r 每 时输出日志,默认值为16,需要和-f 选项一起使用.
      -s 设置默认的过滤级别为silent.
      -v 设置日志输入格式,默认的是brief 格式
  • bugreport [PATH] :查看bug报告
    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

adb 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,
    public key stored in FILE.pub (existing files overwritten)

adb scripting脚本命令

  • wait-for[-TRANSPORT]-STATE
    wait for device to be in the given state
    State: device, recovery, sideload, or bootloader
    Transport: usb, local, or any [default=any]
  • get-state print offline | bootloader | device
  • get-serialno print
  • get-devpath print
  • adb remount :重新挂载系统 分区 用于读写
    remount /system, /vendor, and /oem partitions read-write
  • adb 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
  • sideload_miui OTAPACKAGE [*] install official miui rom when the device is in recovery mode.
  • format-data [*] format data partition on xiaomi devices in recovery mode. Does wipe apps and confguration, doesn’t wipe files and photo located in internal storage.

root restart adbd with root permissions
unroot restart adbd without root permissions
usb restart adb server listening on USB
tcpip PORT restart adb server listening on TCP on PORT

adb internal debugging

  • adb start-server:开启adb的服务,确保adb服务已经运行
  • adb kill-server:杀死adb的服务器,通常重启adb服务用
  • 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

adb 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)
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐