云计算百科
云计算领域专业知识百科平台

Android 日常开发Adb常用命令附档

介绍

        在做Android开发的时候,我们经常会用需要管理设备,模拟设备,最好的伴侣就是ADB命令

常见的Adb命令

一、ADB 基础命令

1. 设备连接管理

# 查看已连接的设备
adb devices

# 查看设备序列号
adb get-serialno

# 查看设备状态
adb get-state

# 查看设备 IP 地址
adb shell ifconfig 或 adb shell ip addr show

# 无线连接设备(需先通过USB连接,需要再手机开发者开启无线调试)
adb tcpip 5555
adb connect 192.168.1.100:5555

# 断开无线连接
adb disconnect 192.168.1.100:5555

# 重新连接到 USB
adb usb

2. 应用管理

2.1安装应用

# 安装 APK
adb install app.apk

# 安装并替换现有应用
adb install -r app.apk

# 允许测试 APK
adb install -t app.apk

# 安装到指定位置
adb install -s app.apk # 安装到 SD 卡

# 多设备时指定设备安装
adb -s emulator-5554 install app.apk

2.2卸载应用

# 卸载应用
adb uninstall com.example.app

# 卸载但保留数据
adb uninstall -k com.example.app

2.3应用信息

# 列出所有应用
adb shell pm list packages

# 列出系统应用
adb shell pm list packages -s

# 列出第三方应用
adb shell pm list packages -3

# 查看应用安装路径
adb shell pm path com.example.app

# 清除应用数据
adb shell pm clear com.example.app

# 查看应用详细信息
adb shell dumpsys package com.example.app

3. 文件操作

3.1复制文件

# 从设备复制到电脑
adb pull /sdcard/file.txt ./local/path/

# 从电脑复制到设备
adb push ./local/file.txt /sdcard/

# 复制整个目录
adb pull /sdcard/folder/ ./
adb push ./folder/ /sdcard/

3.2文件管理

# 查看文件列表
adb shell ls /sdcard/
adb shell ls -la /sdcard/

# 创建目录
adb shell mkdir /sdcard/new_folder

# 删除文件
adb shell rm /sdcard/file.txt

# 删除目录
adb shell rm -r /sdcard/folder/

# 重命名/移动文件
adb shell mv /sdcard/old.txt /sdcard/new.txt

4. 日志操作

4.1基本日志查看

# 查看实时日志
adb logcat

# 清空日志
adb logcat -c

# 查看带时间的日志
adb logcat -v time

# 查看特定标签的日志
adb logcat -s TAG_NAME

# 将日志保存到文件
adb logcat > log.txt

4.2高级日志过滤

# 按优先级过滤
adb logcat *:W # 只显示 Warning 及以上级别

# 多条件过滤
adb logcat -s TAG1:TAG2 *:S

# 显示特定进程日志
adb logcat –pid=PID

# 按时间查看日志
adb logcat -t '01-26 14:00:00.000'

4.3Bug报告

# 生成完整 bugreport
adb bugreport

# 保存到文件
adb bugreport > bugreport.zip

5. 设备信息

5.1系统信息

# 查看设备型号
adb shell getprop ro.product.model

# 查看 Android 版本
adb shell getprop ro.build.version.release

# 查看系统构建信息
adb shell getprop ro.build.fingerprint

# 查看所有属性
adb shell getprop

# 查看 CPU 信息
adb shell cat /proc/cpuinfo

# 查看内存信息
adb shell cat /proc/meminfo
adb shell dumpsys meminfo

5.2电池信息

# 查看电池状态
adb shell dumpsys battery

# 设置电池状态(模拟)
adb shell dumpsys battery set level 50
adb shell dumpsys battery set status 2 # 2=充电中

# 重置电池状态
adb shell dumpsys battery reset

5.3屏幕信息

# 获取屏幕分辨率
adb shell wm size

# 获取屏幕密度
adb shell wm density

# 修改屏幕分辨率(临时)
adb shell wm size 1080×1920

# 修改屏幕密度
adb shell wm density 320

# 恢复默认设置
adb shell wm size reset
adb shell wm density reset

6. 输入操作

6.1模拟按键事件

# 常用按键码
adb shell input keyevent 3 # HOME
adb shell input keyevent 4 # BACK
adb shell input keyevent 24 # 音量+
adb shell input keyevent 25 # 音量-
adb shell input keyevent 26 # 电源键
adb shell input keyevent 82 # 菜单键
adb shell input keyevent 220 # 屏幕亮度+
adb shell input keyevent 221 # 屏幕亮度-

# 媒体控制
adb shell input keyevent 85 # 播放/暂停
adb shell input keyevent 86 # 停止
adb shell input keyevent 87 # 下一曲
adb shell input keyevent 88 # 上一曲

6.2触摸和滑动

# 点击屏幕坐标
adb shell input tap 500 1000

# 滑动操作
adb shell input swipe 300 1000 300 500 # 向上滑动
adb shell input swipe 500 500 100 500 # 向左滑动

# 长按
adb shell input swipe 500 500 500 500 1000

# 文本输入
adb shell input text "Hello World"

7. 截图和录屏

7.1截图

# 截图保存到设备
adb shell screencap /sdcard/screenshot.png

# 截图并直接保存到电脑
adb exec-out screencap -p > screenshot.png

# 自动化截图
adb shell screencap /sdcard/screen.png && adb pull /sdcard/screen.png

7.2录屏

# 开始录屏
adb shell screenrecord /sdcard/demo.mp4

# 设置录制时间(秒)
adb shell screenrecord –time-limit 30 /sdcard/demo.mp4

# 设置分辨率
adb shell screenrecord –size 1280×720 /sdcard/demo.mp4

# 设置比特率
adb shell screenrecord –bit-rate 4000000 /sdcard/demo.mp4

8. 性能监控

8.1CPU 使用情况

# 查看 CPU 使用率
adb shell top -n 1

# 查看指定进程
adb shell top -p PID

# 持续监控
adb shell top -d 1

8.2内存监控

# 查看内存使用情况
adb shell dumpsys meminfo

# 查看指定应用内存
adb shell dumpsys meminfo com.example.app

8.3网络状态

# 查看网络连接
adb shell netstat

# 查看网络速度
adb shell dumpsys netstats

8.4电池性能

# 查看电池状态
adb shell dumpsys batterystats

# 生成电池报告
adb shell dumpsys batterystats –reset
adb shell dumpsys batterystats –checkin

9. 网络相关

9.1网络调试

# 查看网络状态
adb shell ping www.google.com

# 查看网络信息
adb shell ifconfig
adb shell ip addr show

# 查看路由表
adb shell netstat -rn
adb shell ip route show

9.2端口转发

# 端口转发
adb forward tcp:8080 tcp:8080

# 反向端口转发
adb reverse tcp:8080 tcp:8080

# 查看所有转发
adb forward –list

9.3网络限速(模拟弱网)

# 启用网络限速
adb shell svc data disable # 禁用移动数据
adb shell svc wifi disable # 禁用 WiFi

# 设置网络延迟
adb shell tc qdisc add dev wlan0 root netem delay 100ms

# 设置丢包率
adb shell tc qdisc add dev wlan0 root netem loss 10%

10. 进程管理

10.1查看进程

# 查看所有进程
adb shell ps

# 查看特定应用进程
adb shell ps | grep com.example

# 查看进程树
adb shell ps -A

10.2进程控制

# 杀死进程
adb shell am kill com.example.app
adb shell kill PID

# 强制停止应用
adb shell am force-stop com.example.app

11. Activity 和 Service 管理

11.1Activity 操作

# 启动 Activity
adb shell am start -n com.example/.MainActivity

# 启动带参数的 Activity
adb shell am start -n com.example/.MainActivity -e key value

# 启动浏览器打开网址
adb shell am start -a android.intent.action.VIEW -d "https://www.example.com"

# 查看当前 Activity
adb shell dumpsys activity activities | grep mResumedActivity

11.2Service 操作

# 启动 Service
adb shell am startservice com.example/.MyService

# 停止 Service
adb shell am stopservice com.example/.MyService

12. 广播发送

# 发送广播
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

# 发送带参数的广播
adb shell am broadcast -a com.example.MY_ACTION -e key value

# 发送给特定应用
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -n com.example/.MyReceiver

13. 权限管理

# 查看应用权限
adb shell pm list permissions -d -g

# 授予权限
adb shell pm grant com.example.app android.permission.CAMERA

# 撤销权限
adb shell pm revoke com.example.app android.permission.CAMERA

14. 输入法管理

# 列出所有输入法
adb shell ime list -a

# 设置默认输入法
adb shell settings put secure default_input_method com.example.ime/.ImeService

# 启用/禁用输入法
adb shell ime enable com.example.ime/.ImeService
adb shell ime disable com.example.ime/.ImeService

15. 模拟器控制

# 模拟来电
adb emu gsm call 1234567890

# 挂断电话
adb emu gsm cancel 1234567890

# 模拟短信
adb emu sms send 1234567890 "Hello"

# 模拟 GPS 位置
adb emu geo fix 121.5 25.0

# 模拟电量变化
adb emu power capacity 50
adb emu power ac off # 断开充电

16. 高级调试功能

16.1无线调试(Android 11+)

# 配对设备
adb pair 192.168.1.100:12345
# 输入配对码

# 连接已配对设备
adb connect 192.168.1.100:12345

16.2分屏模式

# 进入分屏模式
adb shell am start -n com.example/.MainActivity –windowingMode 2

16.3屏幕旋转

# 锁定旋转
adb shell settings put system accelerometer_rotation 0

# 设置方向
adb shell settings put system user_rotation 0 # 0=竖屏,1=横屏

# 解锁自动旋转
adb shell settings put system accelerometer_rotation 1

17. 实用脚本示例

17.1批量安装 APK

#!/bin/bash
for apk in *.apk; do
echo "Installing $apk…"
adb install "$apk"
done

17.2自动化测试脚本

#!/bin/bash
# 清理日志
adb logcat -c

# 启动应用
adb shell am start -n com.example/.MainActivity

# 等待加载
sleep 2

# 执行操作
adb shell input tap 100 200
adb shell input text "test"
adb shell input keyevent 66 # Enter

# 截图
adb exec-out screencap -p > test_result.png

# 获取日志
adb logcat -d > test_log.txt

17.3设备监控脚本

#!/bin/bash
while true; do
echo "=== $(date) ==="
echo "CPU Usage:"
adb shell top -n 1 | head -20
echo ""
echo "Memory Usage:"
adb shell dumpsys meminfo | head -30
echo ""
echo "Battery:"
adb shell dumpsys battery
echo "========================="
sleep 5
done

三、使用技巧和提示

  • 多设备操作

    # 指定设备执行命令
    adb -s device_serial_number command

    # 同时向所有设备发送命令
    adb devices | tail -n +2 | cut -sf 1 | xargs -I {} adb -s {} command

  • 命令组合:
     

    # 连续执行多个命令
    adb shell "cmd1 && cmd2 && cmd3"

    # 管道操作
    adb shell ps | grep com.example

  • 提高效率:
     

    # 创建别名
    alias adbs='adb shell'
    alias adbll='adb logcat'

    # 使用脚本自动化重复任务

    四、常见问题解决

    设备未识别:

    # 重启 adb 服务
    adb kill-server
    adb start-server

    # 查看 USB 连接
    lsusb # Linux/Mac

    权限不足:

    # 获取 root 权限(需要已 root 的设备)
    adb root

    # 重新挂载系统分区为可写
    adb remount

    命令无响应:

    # 重启 adbd
    adb usb
    adb tcpip 5555
    #重启
    adb reboot

  • 赞(0)
    未经允许不得转载:网硕互联帮助中心 » Android 日常开发Adb常用命令附档
    分享到: 更多 (0)

    评论 抢沙发

    评论前必须登录!