ADB(Android Debug Bridge,安卓调试桥)是连接电脑与安卓设备的通用命令行工具。雷电模拟器每个实例都通过 ADB 调试端口开放,开发者可以直接用 adb 完成连接、安装、模拟输入、截图、文件传输等全部调试与自动化工作,无需依赖任何专有命令行封装。
雷电模拟器每个实例都通过一组 ADB 调试端口开放。常规命名规则为:
| 实例序号 | 默认 adb 端口 |
|---|---|
| 0(主实例) | 5554 |
| 1 | 5556 |
| 2 | 5558 |
| … | 依次 +2 |
实例 0 启动后,在 adb devices 中通常显示为一个串号(如 emulator-5554)
多开时实例 1 约对应 emulator-5556、实例 2 约对应 emulator-5558。具体串号以本机 adb devices 实际输出为准。
adb devices # 查看本机所有实例串号
📌 同机双雷电(雷电9 + 雷电14)端口冲突提醒:若同一台电脑同时运行雷电9 与 雷电14 的实例 0,两者都会抢占
5555。先启动的实例正常注册,后启动的实例 adb 端口会被压制、在adb devices中看不到✅ 解决办法:避免同机双开实例 0,或只保留一个模拟器版本运行。
E:\leidian\LDPlayer14),在地址栏输入 CMD 回车,呼出命令提示符。adb devices 回车,确认实例已列出(如 emulator-5554)。adb shell 回车,进入安卓 shell。E:\leidian\LDPlayer14> adb devices
List of devices attached
emulator-5554 device
E:\leidian\LDPlayer14> adb shell
xxx:/ #
当多个实例同时运行时,每个实例都会注册各自的串号。先用 adb devices 查看全部,再用 -s <串号> 指定目标:
adb devices # 先看全部串号
adb -s emulator-5554 shell # 连接实例 0
adb -s emulator-5554 shell pm list packages -3
若串号未出现,先确认对应模拟器实例已启动,再执行 adb devices 刷新。
当电脑同时连接多台模拟器(或多开实例)时,任何不带 -s 的 adb 命令都会报错:
adb.exe: more than one device/emulator
解决办法只有一种:始终用 -s <串号> 显式指定目标设备。把串号作为 adb 的第一个参数即可:
adb -s emulator-5554 shell input tap 500 500
📌 关于串号的稳定性:运行中实例的串号(如
emulator-5554)由雷电 adb server 持续维护,adb disconnect后会在数秒内被自动拉回;要彻底断开只能关闭实例或adb kill-server(但再次运行任意 adb 命令会重新注册)。因此脚本里直接用-s锁定adb devices里的真实串号最省心。
部分指令涉及权限,可先开启 ROOT(见 3.1)后再操作。下例默认已通过 adb shell 进入安卓,或在前缀带上 adb -s <串号>。
打开模拟器「设置中心 → 其他 → 开启 ROOT 权限」。连接 adb 后输入 adb shell,若提示符为 $ 则权限不足:
adb shell # 进入后看到 $
exit # 退回上一层
adb root # 以 root 重启 adb 守护进程
adb shell # 再次进入,提示符应为 #
adb -s <串号> <其他命令>
# 例:
adb -s emulator-5554 shell pm list packages -3
若本机只有一台模拟器,也可省略 -s,直接 adb <命令>。
adb install C:\xx.apk # 安装
adb install -r C:\xx.apk # 覆盖安装(保留数据)
adb uninstall <包名> # 卸载
adb shell pm list packages # 所有应用
adb shell pm list packages -3 # 第三方应用
adb shell pm list packages -s # 系统应用
adb shell dumpsys window | findstr mCurrentFocus # 正在运行的应用
adb shell cmd package resolve-activity --brief <包名> | tail -n 1
adb shell dumpsys package <包名> | findstr version
adb shell pm clear <包名>
| 动作 | 命令 | 说明 |
|---|---|---|
| 按键 | adb shell input keyevent <键值> | 如 3 为 HOME 键,4 为返回键 |
| 字符 | adb shell input text <字符> | 如 hello;不支持中文 |
| 点击 | adb shell input tap <X> <Y> | X Y 为屏幕坐标(像素) |
| 滑动 | adb shell input swipe <X1> <Y1> <X2> <Y2> [TIME] | TIME 为毫秒,默认 100 |
| 长按 | adb shell input swipe <X> <Y> <X> <Y> <TIME> | 起止同点即长按 |
| 启动 App | adb shell am start -n <包名>/<Activity> | 见3.5获取的 Activity 名 |
adb push C:\test.apk /sdcard/ # 电脑 → 模拟器
adb pull /sdcard/test.apk C:\ # 模拟器 → 电脑(用 Windows 盘符,勿用 /c/)
adb shell screencap /sdcard/screen.png # 截当前屏,存到设备
adb pull /sdcard/screen.png C:\ # 下载到电脑
adb shell screenrecord /sdcard/test.mp4 # 开始录制
# 按 CTRL+C 结束
adb pull /sdcard/test.mp4 C:\ # 导出
adb shell getprop ro.product.model # 型号
adb shell getprop ro.product.brand # 品牌
adb shell getprop ro.product.board # 处理器
adb shell getprop ro.build.version.release # 安卓版本
adb shell wm size # 分辨率
adb shell dumpsys SurfaceFlinger | findstr "GLES" # 渲染模式
adb shell settings put global development_settings_enabled 0
把上述指令组合,即可对雷电模拟器做完整自动化。常见场景:
| 目的 | 命令 |
|---|---|
| 单实例点击坐标 | adb shell input tap 500 500 |
| 指定实例点击坐标 | adb -s emulator-5554 shell input tap 500 500 |
| 输入英文文本 | adb shell input text hello |
| 滑动操作 | adb shell input swipe 300 800 300 300 500 |
| 返回主屏 | adb shell input keyevent 3 |
| 查安卓版本 | adb shell getprop ro.build.version.release |
| 启动系统设置 | adb shell am start -n com.android.settings/.Settings |
| 截图并导出 | adb shell screencap /sdcard/s.png 后接 adb pull /sdcard/s.png C:\ |
| 向设备推文件 | adb push C:\conf.txt /sdcard/ |