Android 调试桥 (adb) 是一种功能多样的命令行工具,可让您与设备进行通信。adb 命令可用于执行各种设备操作,例如安装和调试应用。adb 提供对 Unix shell(可用来在设备上运行各种命令)的访问权限。
- 基本用法
# 查看连接设备
adb devices
# 开启root权限
adb root
# 设置文件系统/system部分置于可写入的模式(适用于已被root的设备)
adb remount
# 从本地复制文件到设备
adb push <local> <devices>
# 从设备复制文件到本地
adb pull <devices> <local>
# 重启设备
adb reboot
# 设备关机
adb reboot -p
# 进入boot
adb reboot bootloader
# 打开shell命令
adb shell
- 截图录屏
# 截图
adb shell screencap -p /sdcard/screen.png
adb pull /sdcard/screen.png /home/user/
# 录制
adb shell screenrecord /sdcard/screenrecord.mp4
adb pull /sdcard/screenrecord.mp4 /home/user/
- 抓取日志log
# 抓取所有log
adb logcat -b all
adb logcat -b all | grep -i AndroidRuntime
# 抓取crash
adb logcat -b crash
# 抓取log为e部分
adb logcat -s *:e
# 同时过滤key0和key1二个关键词
adb logcat -b all | grep -E "key0|key1"
- 屏幕相关
# 屏幕分辨率
adb shell wm size
adb shell wm size 720x1600
adb shell wm size reset
# 屏幕密度
adb shell wm density
adb shell wm density 320
adb shell wm density reset
- 屏幕亮度
# 自动亮度调节
adb shell settings put system screen_brightness_mode 0
# 设置屏幕亮度值
adb shell settings put system screen_brightness 150
# 获取当前亮度值
adb shell settings get system screen_brightness
- 属性相关
# 读取
adb shell getprop <属性名>
adb shell getprop ro.debuggable
# 查看设备信息
adb shell getprop | grep product
# 设置
adb shell setprop ro.debuggable 1
# 查看selinux权限
adb shell getenforce
# 关闭Selinuxu权限
adb shell setenforce 0
- adb dumpsys
# 查看activity
adb shell dumpsys activity | grep Resume
# 列出所有的安装应用的信息
adb shell dumpsys package
# 查看某个包的具体信息
adb shell dumpsys package com.android.XXX
- adb 模拟电量
# 查看手机电量信息
adb shell dumpsys battery
# 模拟手机充电状态
adb shell dumpsys battery set status 2
# 模拟手机为非充电状态
adb shell dumpsys battery set status 1
# 模拟手机断开充电
adb shell dumpsys battery unplug
# 模拟手机电量
adb shell dumpsys battery set level X (X 代表电量)
# 复位,恢复实际状态
adb shell dumpsys battery reset
- adb 服务
# 关闭adb服务
adb kill-server
# 开启adb服务
adb start-server
- adb settings
# 查看所有设置
adb shell settings list system
adb shell settings list global
adb shell settings list secure
# get方法
adb shell settings get global bluetooth_on
# set方法
adb shell settings put global bluetooth_on 1
- adb pm
# 列出所有的包名
adb shell pm list packages
# 查看安装路径
adb shell pm path com.android.XXX
# 清理数据缓存
adb shell pm clear com.android.settings.intelligence
# 查看版本号
adb shell pm dump com.android.XXX | grep -i version
- adb am
# 启动应用界面
adb shell am start -n 包名/包名+类名
adb shell am start -n com.example.mytest/com.example.mytest.MainActivity
adb shell am start -n com.example.app/.UserActivity -e username "John" --ei age 30
# 启动 Inent Action
adb shell am start -a <ACTION_NAME> [OPTIONS]
adb shell am start -a com.example.ACTION_TEST
adb shell am start -a android.intent.action.VIEW -d "https://example.com"
adb shell am start -a android.intent.action.DIAL -d tel:10086
- 显式 Intent:通过
-n
指定组件- 隐式 Intent:通过
-a
指定 Action,系统匹配组件
-a <ACTION_NAME>
:指定 Intent 的 Action(如android.intent.action.VIEW
)-n <COMPONENT>
:指定目标组件(如com.example.app/.MainActivity
)-d <DATA_URI>
:附加数据 URI(如tel:10086
用于拨号)-e <KEY> <VALUE>
:传递额外数据(字符串、整型等)
- adb 发送广播
adb shell am broadcast -a <ACTION> [OPTIONS]
-a <ACTION>
:指定广播的 Action(如com.example.ACTION_TEST
)-n <COMPONENT>
:可选,指定接收广播的包名和类名(如com.example.app/.MyReceiver
)
-p <PACKAGE_NAME>
:用于指定接收广播的应用程序包名--es <KEY> <VALUE>
:附加字符串类型的额外数据(Intent.putExtra
的等效操作)
# 发送简单广播
adb shell am broadcast -a com.example.ACTION_TEST
# 发送带额外数据的广播
adb shell am broadcast -a com.example.ACTION_TEST --es key1 "value1" --ei key2 100 --ez key3 true
# 指定接收者
adb shell am broadcast -a com.example.ACTION_TEST -n com.example.app/com.example.MyReceiver
--es
:字符串类型数据(value1
)--ei
:整型数据(100
)--ez
:布尔类型数据(true
)--el
:长整型(1000L
)--ef
:浮点型(3.14
)
- adb 获取设备输入事件
adb shell getevent -l
- adb input
# 模拟按下数字0键(按键码参考:frameworks/base/core/java/android/view/KeyEvent.java)
adb shell input keyevent "KEYCODE_0"
# 模拟按下返回键
adb shell input keyevent 4
# 输出文本
adb shell input text bcoder.com
# 模拟点击
adb shell input touchscreen tap 110 66
# 模拟滑动(x1,y1)-(x2,y2)
adb shell input touchscreen swipe 450 66 110 66
- 安装与卸载App
adb install test.apk
# 覆盖安装
adb install -r test.apk
# 安装测试apk
adb install -t test.apk
# 卸载应用
adb shell uninstall <包名>
adb shell uninstall com.android.XXX
- 刷入单编模块
adb root
adb remount
adb push \LA.UM.5.6\out\target\product\msm8937_32\system\priv-app\SystemUI /system/priv-app/
adb reboot
- 刷入系统
adb reboot bootloader
fastboot flash boot boot.img
fastboot reboot
- 刷机命令
# 重新启动到recovery
adb reboot recovery
# 重新启动到bootloader,即进入fastboot
adb reboot bootloader
# 线刷ROM包
adb sideload D:/MIUI12-20200723.zip
- fastboot命令
fastboot devices
fastboot reboot
fastboot reboot-bootloader
fastboot flash recovery recovery.img
fastboot flash boot boot.img
fastboot devices -l 列举设备
fastboot flashing lock 15年以后
fastboot oem lock 15年以前
fastboot flash recovery twrp-3.0.2-0-hammerhead.img 刷入recovery
fastboot reboot 重启手机
fastboot oem get-bootinfo
adb wait-for-device #等待设备
adb reboot-bootloader #这个是重启到bootloader界面 默认是fastboot。
fastboot flash boot boot.img #这个是刷入boot的命令。官解的要手动刷一次。
fastboot flash recovery recovery.img #刷入recovery 已有recovery的可以跳过。
fastboot erase boot #擦除boot分区
fastboot erase system -w #擦除system分区、 userdata分区和cache分区
fastboot update update.zip #将update.zip刷入
fastboot reboot #重启手机
- 其他
# 访问主机
adb shell ping
# 查看你网络连接
adb shell ifconfig
# 查看进程的静态快照片
adb shell ps
# 查看运行进程信息
adb shell top
# 查看时间
adb shell date
# 挂载信息
adb shell mount
# 查看文件
adb shell cat