8.7 KiB
8.7 KiB
ADB高级命令
概述
ADB (Android Debug Bridge) 是Android开发中最常用的命令行工具,用于与Android设备通信。本文档详细介绍ADB的高级用法和实用技巧。
ADB基础
安装与配置
# 检查ADB版本
adb version
# 查看连接的设备
adb devices
# 查看设备详细信息
adb devices -l
设备连接
# USB连接
adb devices
# 网络连接(需要先USB连接一次)
adb tcpip 5555
adb connect <设备IP>:5555
# 断开网络连接
adb disconnect <设备IP>:5555
# 切换回USB模式
adb usb
日志相关命令
1. logcat基础
# 查看所有日志
adb logcat
# 清空日志缓冲区
adb logcat -c
# 查看指定tag的日志
adb logcat -s TAG_NAME
# 查看多个tag
adb logcat -s TAG1 TAG2
# 过滤日志级别
adb logcat *:E # 只显示Error级别
adb logcat *:W # 只显示Warning及以上
2. logcat高级用法
# 按时间格式显示
adb logcat -v time
# 显示进程ID和线程ID
adb logcat -v threadtime
# 显示颜色(需要终端支持)
adb logcat -v color
# 显示所有信息(最详细)
adb logcat -v long
# 保存日志到文件
adb logcat > log.txt
# 实时查看并保存
adb logcat | tee log.txt
# 按包名过滤
adb logcat | grep com.example.app
3. 日志过滤技巧
# 使用grep过滤
adb logcat | grep "关键词"
# 排除某些tag
adb logcat | grep -v "TAG_NAME"
# 组合过滤
adb logcat | grep -E "关键词1|关键词2"
# 按时间范围过滤(需要先保存日志)
adb logcat -t '01-01 12:00:00.000'
4. 日志级别
# 设置日志级别
adb shell setprop log.tag.TAG_NAME VERBOSE
adb shell setprop log.tag.TAG_NAME DEBUG
adb shell setprop log.tag.TAG_NAME INFO
adb shell setprop log.tag.TAG_NAME WARN
adb shell setprop log.tag.TAG_NAME ERROR
# 查看当前日志级别
adb shell getprop log.tag.TAG_NAME
系统服务调试
1. dumpsys命令
# 查看所有可用的服务
adb shell dumpsys -l
# 查看Activity信息
adb shell dumpsys activity
# 查看Activity栈
adb shell dumpsys activity activities
# 查看当前Activity
adb shell dumpsys activity top
# 查看Service信息
adb shell dumpsys activity services
# 查看内存信息
adb shell dumpsys meminfo
# 查看指定应用的内存
adb shell dumpsys meminfo com.example.app
# 查看电池信息
adb shell dumpsys batterystats
# 查看网络信息
adb shell dumpsys netstats
# 查看包信息
adb shell dumpsys package com.example.app
# 查看窗口信息
adb shell dumpsys window
# 查看输入法信息
adb shell dumpsys input_method
2. Activity Manager命令
# 启动Activity
adb shell am start -n com.example.app/.MainActivity
# 启动Activity并传递数据
adb shell am start -n com.example.app/.MainActivity \
-e key1 value1 -e key2 value2
# 启动Service
adb shell am startservice -n com.example.app/.MyService
# 发送广播
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED
# 强制停止应用
adb shell am force-stop com.example.app
# 杀死进程
adb shell am kill com.example.app
# 启动Activity并测量启动时间
adb shell am start -W -n com.example.app/.MainActivity
3. Package Manager命令
# 安装APK
adb install app.apk
# 安装并替换已存在的应用
adb install -r app.apk
# 安装到SD卡
adb install -s app.apk
# 卸载应用
adb uninstall com.example.app
# 保留数据卸载
adb uninstall -k com.example.app
# 查看已安装的包
adb shell pm list packages
# 查看指定应用的包信息
adb shell pm list packages | grep com.example
# 查看包路径
adb shell pm path com.example.app
# 清除应用数据
adb shell pm clear com.example.app
# 启用/禁用组件
adb shell pm enable com.example.app/.MainActivity
adb shell pm disable com.example.app/.MainActivity
4. 输入事件模拟
# 点击屏幕坐标
adb shell input tap 500 1000
# 滑动
adb shell input swipe 300 1000 300 500
# 输入文本
adb shell input text "Hello World"
# 按键事件
adb shell input keyevent KEYCODE_HOME
adb shell input keyevent KEYCODE_BACK
adb shell input keyevent KEYCODE_MENU
# 长按
adb shell input swipe 500 1000 500 1000 2000
文件操作
1. 文件传输
# 推送文件到设备
adb push local_file.txt /sdcard/
# 从设备拉取文件
adb pull /sdcard/file.txt ./
# 查看设备文件
adb shell ls /sdcard/
# 查看文件内容
adb shell cat /sdcard/file.txt
2. 文件权限
# 修改文件权限
adb shell chmod 755 /data/local/tmp/script.sh
# 修改文件所有者
adb shell chown system:system /data/local/tmp/file
# 查看文件权限
adb shell ls -l /data/local/tmp/
性能数据采集
1. CPU信息
# 查看CPU使用率
adb shell top
# 查看指定进程的CPU使用率
adb shell top -p <PID>
# 查看CPU信息
adb shell cat /proc/cpuinfo
# 查看CPU频率
adb shell cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
2. 内存信息
# 查看内存使用情况
adb shell dumpsys meminfo
# 查看指定应用的内存
adb shell dumpsys meminfo com.example.app
# 查看系统内存
adb shell cat /proc/meminfo
# 查看进程内存映射
adb shell cat /proc/<PID>/maps
3. 网络信息
# 查看网络接口
adb shell ifconfig
# 查看网络连接
adb shell netstat
# 查看路由表
adb shell ip route
# 查看网络统计
adb shell cat /proc/net/sockstat
4. 电池信息
# 查看电池状态
adb shell dumpsys battery
# 重置电池统计
adb shell dumpsys batterystats --reset
# 导出电池报告(需要配合bugreport)
adb bugreport
系统属性操作
1. 查看系统属性
# 查看所有属性
adb shell getprop
# 查看指定属性
adb shell getprop ro.build.version.sdk
# 查看属性列表(部分)
adb shell getprop | grep "ro\."
2. 设置系统属性
# 设置属性(需要root)
adb shell setprop property_name property_value
# 设置日志级别
adb shell setprop log.tag.TAG_NAME DEBUG
调试技巧
1. 查看进程信息
# 查看所有进程
adb shell ps
# 查看指定应用的进程
adb shell ps | grep com.example.app
# 查看进程树
adb shell ps -A
# 查看进程详细信息
adb shell ps -ef
2. 查看线程信息
# 查看进程的所有线程
adb shell ps -T -p <PID>
# 查看线程堆栈
adb shell kill -3 <PID> # 发送SIGQUIT信号,生成堆栈
3. 网络调试
# 设置代理
adb shell settings put global http_proxy <proxy_host>:<proxy_port>
# 清除代理
adb shell settings delete global http_proxy
adb shell settings delete global global_http_proxy_host
adb shell settings delete global global_http_proxy_port
# 查看代理设置
adb shell settings get global http_proxy
4. 权限调试
# 授予运行时权限
adb shell pm grant com.example.app android.permission.CAMERA
# 撤销权限
adb shell pm revoke com.example.app android.permission.CAMERA
# 查看应用权限
adb shell dumpsys package com.example.app | grep permission
高级用法
1. 执行Shell命令
# 执行单条命令
adb shell "ls -l /sdcard/"
# 执行多条命令
adb shell "cd /sdcard && ls -l"
# 执行脚本
adb shell sh /sdcard/script.sh
2. 端口转发
# 转发端口
adb forward tcp:8080 tcp:8080
# 转发到Unix域套接字
adb forward tcp:8080 local:/tmp/socket
# 查看所有转发
adb forward --list
# 删除转发
adb forward --remove tcp:8080
3. 无线调试(Android 11+)
# 配对设备(首次)
adb pair <IP>:<PORT>
# 输入配对码
# 连接设备
adb connect <IP>:<PORT>
# 查看已连接的设备
adb devices
4. 多设备管理
# 指定设备执行命令
adb -s <device_serial> shell ...
# 查看设备序列号
adb devices -l
实用脚本
1. 清理日志并重启
#!/bin/bash
adb logcat -c
adb shell reboot
2. 批量安装APK
#!/bin/bash
for apk in *.apk; do
adb install -r "$apk"
done
3. 监控应用崩溃
#!/bin/bash
adb logcat | grep -i "fatal\|exception\|crash"
4. 性能数据采集
#!/bin/bash
# 采集CPU、内存、网络数据
adb shell top -n 1 > cpu.txt
adb shell dumpsys meminfo > meminfo.txt
adb shell dumpsys netstats > netstats.txt
常见问题
1. 设备未识别
# 检查USB连接
adb kill-server
adb start-server
adb devices
2. 权限不足
# 某些命令需要root权限
adb root
adb remount
3. 日志过多
# 使用过滤器减少日志
adb logcat -c # 清空缓冲区
adb logcat *:E # 只显示错误