网络知识 娱乐 adb(安卓调试桥)命令-新手入门自学笔记

adb(安卓调试桥)命令-新手入门自学笔记

ADB,即 Android Debug Bridge,它是 Android 开发/测试人员不可替代的强大工具,也是 Android 设备玩家的好玩具。安卓调试桥 (Android Debug Bridge, adb),是一种可以用来操作手机设备或模拟器的命令行工具。

一、打开手机端USB调试授权

首先我们需要进入开发者模式,以华为mate30手机为例,打开“设置”,拉到最下方,点击“关于手机”,连续点击五次“版本号”,手机会弹出提示您已进入开发者模式(图1)。然后返回上一层“设置”界面,点击“系统和更新”,再找到“开发人员选项”点击进入(图2),向下滑动找到“USB调试”选项并开启,再点击“确定”就OK了(图3)。

           

当手机通过USB线连接到电脑时,手机通知栏会有“已连接USB调试”的提示(上图,图4)(不过并不确定是不是所有手机都会有这个提示)。如果想查看手机是否已经成功能进行USB调试,还有其他方法,即在电脑上cmd如下命令:

adb devices

此时,窗口中会列出设备的序列号,此时就可以使用adb命令对手机进行调试了。

二、常用adb命令

1、打开adb帮助

adb help

然后窗口中会展示超长一段adb的命令用法介绍,如下所示(不过对于新手来说,看这些命令不如查CSDN来的快):

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 [default port=5555]
 disconnect [HOST[:PORT]]
     disconnect from given TCP/IP device [default port=5555], or all
 pair HOST[:PORT]         pair with a device for secure TCP/IP communication
 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)
       acceptfd: (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: ( 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] [-zZ] LOCAL... REMOTE
     copy local files/directories to device
     --sync: only push files that are newer on the host than the device
     -z: enable compression
     -Z: disable compression
 pull [-azZ] REMOTE... LOCAL
     copy files/dirs from device
     -a: preserve file timestamp and mode
     -z: enable compression
     -Z: disable compression
 sync [-lzZ] [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
     -z: enable compression
     -Z: disable compression

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 a 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)

2、查看已连接到电脑的设备

adb devices

 3、重启设备

adb reboot

4、查看和储存日志

adb logcat

 手机内的日志分为四个缓冲区,分别为main,system,radio和event。main区域中为手机应用软件日志,system中为系统组件日志,radio中为通信日志,event中为手机按键输入等事件日志。我们通常使用如下命令使屏幕实时显示指定缓冲区的日志:

若想停止获取日志,按"Ctrl+C"即可终止。

adb logcat -b main
adb logcat -b system
adb logcat -b radio
adb logcat -b event

若想要将日志保存至电脑指定位置,可使用以下命令(此时,窗口不会实时显示日志内容):

adb logcat -b main > 

为储存日志的地址,示例如下:

adb logcat -b radio > D:log.txt

当想要停止获取日志,按"Ctrl+C"即可,此时打开日志存储路径便可查看获取到的日志内容。

5、安装程序至手机

adb install 

为手机程序安装包所在地址,可直接将安装包用鼠标拖至窗口中,安装包地址便会自动呈现在窗口上,示例如下:

adb install D:baidu.apk

6、卸载手机中的程序

adb uninstall 
adb uninstall com.huawei.health

这个命令与安装程序有很大的区别,为程序的包名,包名无法直接在手机上查到,当然可以通过安装一些插件或小程序查到,不过显然通过adb命令查包名会更加容易,方法如7中所示。

7、查手机程序包名

adb shell am monitor

这个命令可以实时查看打开的程序的包名,窗口会实时显示你在手机中打开的程序的包名,想卸载哪个程序先运行一下就知道他的包名了。

adb shell pm list packages

这个命令可以查看手机上所有已安装程序的包名,不过缺点就是一次性显示所有的包名会有点混乱......你不一定能找得到你想要找的程序包名

8、电脑传文件到手机中

adb push  

是电脑端要传的文件路径

是移动端存储文件的路径

9、手机传文件到电脑中

adb push  

是电脑端要传的文件路径

是移动端存储文件的路径

作为一个入门级新手来说,这些adb命令够玩好久了。那就开始吧!!