ADB工具

adb 的运行原理是 PC 端的 adb server 与手机端的守护进程 adbd 建立连接, 然后 PC 端的 adb client 通过 adb server 转发命令,adbd 接收命令后解析运行。

基本命令

adb devices
adb [-s <serialNumber>] <command>

adb -P <port> start-server  # 默认端口为 5037
adb kill-server  # 重启

# 获取错误报告
adb bugreport E:\Reports\MyBugReports

adb详细用法

pm list packages

# 列出所有已安装
adb shell pm list packages

# 只显示第三方的包名,-s 代表系统应用
adb shell pm list packages -3"|cut -f 2 -d ":

# 查看应用安装路径
adb shell pm path <PACKAGE>

# getprop 用法
adb shell getprop|findstr ro.build.id
adb shell getprop ro.product.model
  • -s 系统应用
  • -3 第三方应用
  • -f apk文件位置
  • -d 已停用应用

信息

# 查看前台Activity
adb shell dumpsys activity activities | findstr mResumedActivity


# 系统版本
adb shell getprop ro.build.version.release
adb shell settings get secure android_id

# IP地址,可能需要权限
adb shell ifconfig | grep Mask
adb shell ifconfig wlan0
# MAC地址
adb shell cat /sys/class/net/wlan0/address

# 在部分系统
adb shell netcfg

键盘事件

adb shell input


# 输入文本(当焦点在某文本框)
input text hello

input keyevent 3  #  HOME/主页
input keyevent 4  #  返回
input keyevent 26  #  电源键
input keyevent 66  # 输入回车/Enter
input keyevent 82  #  菜单
input keyevent 187 # 最近/切换应用
input keyevent 224  #  点亮屏幕
# 屏幕解锁
input swipe

WiFi连接

adb tcpip 5555   # 可以是任意端口
# device_ip可以从[关于手机]-[状态消息]-[IP地址]查看
adb connect <device_ip>:5555

# 确认连接状态
adb devices
# 关闭adb wifi
adb disconnect

日志过滤

adb logcat *:W
# 可以由多个 <tag>[:priority] 组成
adb logcat ActivityManager:I MyApp:D *:S

# 清空日志
adb logcat -c

# 获取日志
adb logcat -s  <serialID> -d > D://logcat1.txt

与应用交互

# 启动应用,指定Activity名称
adb shell am start [options] <INTENT>  
# adb shell am start -n com.tencent.mm/.ui.LauncherUI

# 启动主Activity
adb shell monkey -p <packagename> -c android.intent.category.LAUNCHER 1

Tips

  • 关闭手机助手
  • 关闭安全输入
    • 华为: [系统和更新]-[语言和输入法]-关闭"安全输入"
    • VIVO: 关闭"防恶意截屏录屏"
  • 小米: 允许模拟点击

常见错误

# Q: INSTALL_FAILED_TEST_ONLY
# A 1.  android:testOnly="false"
#   2.
adb install -t *.apk

# Q: INSTALL_FAILED_USER_RESTRICTED
# A: 解决方式: 开发者选项->打开"USB安装"

参考