You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何程序化映射Flutter模拟器ID与对应设备ID?

问题描述

我定义了多个Android模拟器配置文件,每个配置对应唯一的emulator id,可通过命令 ~/Library/Android/sdk/emulator/emulator -list-avds 查看。
使用命令 ~/Library/Android/sdk/emulator/emulator -avd EMULATOR_ID 可启动任意模拟器,启动时会分配一个device id(首个通常为 emulator-5554),后续操作(如向设备安装、启动应用)需用到该ID。

当并行启动多个模拟器时,问题出现:

  • adb devices 仅返回device id列表,无法对应到具体的模拟器;
  • flutter devices 虽信息更详细,但所有模拟器标识相同,仍无法区分。

我的需求是:程序化地在不同模拟器上启动集成测试,因此需要明确特定emulator id对应的device id,该如何实现?

典型操作会话

以下是操作示例,可见最终无法区分哪个device id对应Pixel 2模拟器:

➜  ~/StudioProjects/ft (integration_test) ✗ adb devices                                                   
List of devices attached

➜  ~/StudioProjects/ft (integration_test) ✗ flutter devices                                              
2 connected devices:

macOS (desktop) • macos  • darwin-arm64   • macOS 13.3.1 22E261 darwin-arm64
Chrome (web)    • chrome • web-javascript • Google Chrome 112.0.5615.137

➜  ~/StudioProjects/ft (integration_test) ✗ ~/Library/Android/sdk/emulator/emulator -list-avds          
Nexus_10_API_33
Nexus_7_API_33
Pixel_2_API_33
Pixel_6_API_33

➜  ~/StudioProjects/ft (integration_test) ✗ ~/Library/Android/sdk/emulator/emulator -avd Nexus_10_API_33 & 
[1] 23358
INFO    | Android emulator version 31.3.10.0 (build_id 8807927) (CL:N/A)                                                                                                                
emulator: INFO: Found systemPath /Users/sylvia/Library/Android/sdk/system-images/android-33/google_apis/arm64-v8a/
[...]

➜  ~/StudioProjects/ft (integration_test) ✗ ~/Library/Android/sdk/emulator/emulator -avd Nexus_7_API_33 & 
[2] 23579
INFO    | Android emulator version 31.3.10.0 (build_id 8807927) (CL:N/A)                                                                                                                
emulator: INFO: Found systemPath /Users/sylvia/Library/Android/sdk/system-images/android-33/google_apis/arm64-v8a/
[...]

➜  ~/StudioProjects/ft (integration_test) ✗ ~/Library/Android/sdk/emulator/emulator -avd Pixel_2_API_33 & 
[3] 24138
INFO    | Android emulator version 31.3.10.0 (build_id 8807927) (CL:N/A)                                                                                                                
emulator: INFO: Found systemPath /Users/sylvia/Library/Android/sdk/system-images/android-33/google_apis_playstore/arm64-v8a/
[...]

➜  ~/StudioProjects/ft (integration_test) ✗ adb devices                                                    
List of devices attached
emulator-5554   device
emulator-5556   device
emulator-5558   device

➜  ~/StudioProjects/ft (integration_test) ✗ flutter devices                                                
5 connected devices:

sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64  • Android 13 (API 33) (emulator)
sdk gphone64 arm64 (mobile) • emulator-5556 • android-arm64  • Android 13 (API 33) (emulator)
sdk gphone64 arm64 (mobile) • emulator-5558 • android-arm64  • Android 13 (API 33) (emulator)
macOS (desktop)             • macos         • darwin-arm64   • macOS 13.3.1 22E261 darwin-arm64
Chrome (web)                • chrome        • web-javascript • Google Chrome 112.0.5615.137
解决方案

方法一:启动时指定固定端口(最适合程序化操作)

启动模拟器时通过 -port 参数指定固定端口,device id会直接对应为 emulator-端口号,完全可控:

# 启动Pixel_2_API_33,指定端口5554
~/Library/Android/sdk/emulator/emulator -avd Pixel_2_API_33 -port 5554 &
# 启动Nexus_7_API_33,指定端口5556
~/Library/Android/sdk/emulator/emulator -avd Nexus_7_API_33 -port 5556 &

这样emulator-5554就对应Pixel_2_API_33,直接关联,无需后续匹配。

方法二:通过进程PID关联device id

  1. 启动模拟器时记录命令返回的进程PID(如启动命令后的[1] 23358)
  2. 使用lsof命令查找该进程占用的adb端口:
lsof -p 23358 | grep TCP | grep 555[0-9]

输出中的本地端口即为模拟器的adb端口,对应的device id就是emulator-端口号。

方法三:读取模拟器系统属性获取avd名称

针对已启动的模拟器,通过adb连接后读取系统属性直接获取对应的emulator id:

# 替换emulator-5554为目标device id
adb -s emulator-5554 shell getprop ro.boot.qemu.avd_name

该命令会返回对应的模拟器配置名称(如Pixel_2_API_33),可批量遍历所有device id完成匹配。


内容的提问来源于stack exchange,提问作者duffy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 09:08:07