adb 操作命令集合

什么是 adb 命令?

adb 工具即 Android Debug Bridge(安卓调试桥) tools。它就是一个命令行窗口,用于通过电脑端与模拟器或者真实设备交互。在某些特殊的情况下进入不了系统,adb 就派上用场啦!

源码传送地址

分类命令

ADB Debugging

  • adb devices
    主要是用于打印当前连接的所有模拟器或者设备。

    《adb 操作命令集合》

  • adb forward
    端口映射,将 PC 端的某端口数据重定向到手机端的一个端口。

adb forward <local> <remote>

  • adb kill-server
    终止 adb 进程。

adb kill-server

Wireless

  • adb connect
    无限调试必备命令,需要保证设备和 PC 在同一局域网内,所以可通过远程桌面达到远程调试的结果。

adb connect <host>[:<port>]

需要保证设备的 /system/build.prop 文件中有命令 service.adb.tcp.port=5555,否则会遭到拒绝。

《adb 操作命令集合》

此处安利一下无限调试设置方法:

  1. 打开设备的调试模式,允许 USB 以 MTP 模式传输,确保设备和 PC 能正常连接,可以通过 adb shell 或者 adb devices 等进行验证。
  2. 确保已连接后,依次执行以下命令:
    adb root
    adb remount
    adb pull /system/build.prop ./
  3. 在 adb 命令执行的文件夹下的 build.prop 中加入命令 service.adb.tcp.port=5555
  4. 执行 adb push ./build.prop /system/ 后重启设备
    结束后断开 USB 连接线,输入 adb connect 设备IP:5555 确认可以正常连接。
  • adb usb
    设置设备以 USB 形式连接 PC。

Package Manager

  • adb install
    主要用于往 Android 设备 push 应用。

adb install [option] <path>

其中的 option 也是比较有讲究的,下面就只说最常用的。

  • adb install test.apk 直接安装应用
  • adb install -r test.apk 替代存在的应用,不会删除应用数据,用于更新应用特别方便。

其余的不是很常用的就不多提了,感兴趣的可以自行了解。

  • adb uninstall
    从设备或者模拟器卸载应用。

adb uninstall [options] <package>

两种情况,假设我们的应用 APP 包名是 com.example.application

adb uninstall com.example.application 直接删除应用和所有数据
adb uninstall -k com.example.application 删除应用,但会保留应用数据和缓存数据。

  • adb shell pm list packages
    打印设备下面的所有应用包名。

adb shell pm list packages [options] <FiLTER>

《adb 操作命令集合》

其他的过滤方式和限定条件这里也不举例了。

  • adb shell pm path
    打印 apk 的路径。

adb shell pm path <package>

  • adb shell pm clear
    清除应用缓存。

adb shell pm clear <package>

File Manager

  • adb pull
    从 Android 设备下载文件到 PC。

adb pull <remote> [local]

其中 <remote> 代表文件在设备中的地址,[local] 代表存放目录。

  • adb push
    把 PC 的文件存放到 Android 设备。

adb push <local> <remote>

  • adb shell ls
    列出目录内容。

adb shell ls [options] <directory>

  • adb shell cd
    和一般的 PC 的 cd 差不多,主要用于切换目录。

adb shell cd <directory>

  • adb shell rm
    删除文件或者目录

adb shell rm [options] <file or directory>

  • adb shell mkdir
    创建一个文件夹

adb shell mkdir [options] <directory name>

  • adb shell touch
    创建一个新文件或者改变文件修改时间

adb shell touch [options] <file>

  • adb shell pwd
    定位当前的操作位置

adb shell pwd

  • adb shell cp
    字面意思,很好理解,复制。

adb shell cp [options] <source> <dest>

  • adb shell mv
    移动或者更名文件

adb shell mv [options] <source> <dest>

Network

  • adb shell netstat
    主要用于网络统计。

  • adb shell ping
    没啥好说的,和 PC 的 ping 命令一样的。

  • adb shell netcfg
    通过配置文件配置和管理网络连接。

adb shell netcfg [<interface> {dhcp|up|down}]

  • adb shell ip
    主要用于显示一些数据

adb shell ip [OPTIONS] OBJECT

《adb 操作命令集合》

Logcat

  • adb logcat
    打印日志文件。

adb logcat [options] [filter-specs]

当然可以像 Android Studio 一样只打印固定的日志

adb logcat *:V lowest priority, filter to only show Verbose level
adb logcat *:D filter to only show Debug level
adb logcat *:I filter to only show Info level
adb logcat *:W filter to only show Warning level
adb logcat *:E filter to only show Error level
adb logcat *:F filter to only show Fatal level
adb logcat *:S Silent, highest priority, on which nothing is ever printed

adb logcat -b <Buffer>

adb logcat -b radio View the buffer that contains radio/telephony related messages.
adb logcat -b event View the buffer containing events-related messages.
adb logcat -b main default
adb logcat -c Clears the entire log and exits.
adb logcat -d Dumps the log to the screen and exits.
adb logcat -f test.logs Writes log message output to test.logs .
adb logcat -g Prints the size of the specified log buffer and exits.
adb logcat -n <count> *Sets the maximum number of rotated logs to <count>. *

  • adb shell dumpsys
    获取系统数据。

adb shell dumpsys [options]

其中有个非常好用的是,当你在新接触一个项目的时候,不熟悉项目流程,此时正好可以用这个直接定位到你的 Activity 位置。

adb shell dumpsys activity activities

《adb 操作命令集合》

如图,直接在打印出来内容的后半段找到了当前 Activity 的定位,是 NewLoginActivity

  • adb shell dumpstate
    和命令直译差不多,dumps state。

Screenshot

  • adb shell screencap
    一般的手机都有截图功能(一般下拉菜单中有),但不代表所有 Android 设备都在可视化中开启了这个功能,所以这时候这个 adb 命令就显得特别重要。

adb shell screencap <filename>

结合前面的 pull 命令,就可以让我们轻松拿到屏幕截图。

adb shell screencap /sdcard/test.png 截图存放
adb pull /sdcard/test.png 取到 PC 当前文件夹

  • adb shell screencord
    有了屏幕截图,自然也得有屏幕录制,可惜这个必须在 Android 4.4 (API level 19) 以上才可使用。

adb shell screencord /sdcard/test.mp4

这个还可以对大小 size 和 时间做限制,感兴趣的可以自行了解。

System

  • adb root
    获取 root 权限。

  • adb sideload

  • adb shell ps
    打印进程状态。

  • adb shell top
    展现上层 CPU 进程信息。

  • adb shell getprop
    获取 Android 系统服务属性

  • adb shell setprop
    设置服务属性。

    原文作者:nanchen2251
    原文地址: https://www.jianshu.com/p/a94e5bf10087
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞