如何配置Detox以使用已运行的Android模拟器?
关于Detox复用Android模拟器及资源异常恢复的问题
核心问题1:配置Detox复用已运行的Android模拟器
默认情况下,Detox会尝试启动新的Android模拟器,若已有同AVD的模拟器运行,偶尔会触发Running multiple emulators with the same AVD错误。需求如下:
- 是否可以配置Detox直接复用已运行的模拟器,而非启动新实例?
- 已知react-native-firebase团队已实现该用法,希望明确具体配置方式,并建议将该方法纳入Detox官方文档。
相关错误日志
root@df3aaa6b6563:/pwd# nohup $ANDROID_HOME/emulator/emulator -avd "NEXUS_6_API_30" -noaudio -gpu swiftshader_indirect -camera-back none -no-snapshot -no-window -no-boot-anim -nojni -memory 4048 -timezone 'Europe/Helsinki' -cores 4 & root@df3aaa6b6563:/pwd# yarn e2e-test-android --tags @freepath yarn run v1.22.19 $ NODE_ENV=ci DETOX_CONFIGURATION=android.emu.debug DETOX_RECORD_LOGS=failing DETOX_HEADLESS=true DETOX_GPU=swiftshader_indirect DETOX_TAKE_SCREENSHOTS=failing DETOX_RECORD_VIDEOS=failing cucumber-js ./e2e/features -f json:e2e/report/cucumber_report.json --require-module ts-node/register --require 'e2e/features/step_definitions/*.ts' --tags @freepath *** In Cucumber BeforeAll hook for Detox *** nohup sh -c 'yarn e2e-start' > ./metrobundler.log 0</dev/null & Running detox.init ... stdout: stdout: stdout: stdout: stdout: stdout: 15:16:14.313 detox[15538] ERROR: [SPAWN_FAIL] `/opt/android/emulator/emulator -verbose -no-audio -no-boot-anim -no-window -gpu swiftshader_indirect -port 11062 @NEXUS_6_API_30` failed with code 1 err: ChildProcessError: `/opt/android/emulator/emulator -verbose -no-audio -no-boot-anim -no-window -gpu swiftshader_indirect -port 11062 @NEXUS_6_API_30` failed with code 1 at ChildProcess.<anonymous> (/pwd/node_modules/child-process-promise/lib/index.js:132:23) at ChildProcess.emit (events.js:400:28) at ChildProcess.emit (domain.js:475:12) at maybeClose (internal/child_process.js:1088:16) at Process.ChildProcess._handle.onexit (internal/child_process.js:296:5) 15:16:14.315 detox[15538] ERROR: [SPAWN_FAIL] ERROR | Running multiple emulators with the same AVD ERROR | is an experimental feature. ERROR | Please use -read-only flag to enable this feature.
核心问题2:从Detox资源占用异常状态恢复
2022年9月27日更新:经排查,上述问题源于强制中断Detox运行(如按CTRL+C)导致清理流程未执行,产生残留资源占用。正常未中断时Detox可自动复用已运行模拟器,但异常状态下日志显示Device emulator-13196 is already taken, skipping...,此时Detox尝试启动新模拟器仍触发同AVD运行错误。需要解决:
- 如何从这种资源异常状态恢复?
- 目前仅偶尔更换随机端口可临时解决,但无法通过ps命令找到残留进程;CI环境中每次使用新云机器则不会出现该问题。
相关错误日志
14:48:10.516 detox[73180] DEBUG: [EMU_BIN_VERSION_DETECT] Detected emulator binary version { major: 31, minor: 3, patch: 10, toString: [Function: toString] } 14:48:10.516 detox[73180] DEBUG: [ALLOCATE_DEVICE] Trying to allocate a device based on "NEXUS_6_API_31" 14:48:10.519 detox[73180] DEBUG: [EXEC_CMD, #2] "/Users/jari/Library/Android/sdk/platform-tools/adb" devices 14:48:10.559 detox[73180] DEBUG: [EXEC_SUCCESS, #2] List of devices attached emulator-13196 device 14:48:10.561 detox[73180] DEBUG: [DEVICE_LOOKUP] Device emulator-13196 is already taken, skipping... 14:48:10.563 detox[73180] DEBUG: [ALLOCATE_DEVICE] Settled on emulator-15874 14:48:10.566 detox[73180] DEBUG: [SPAWN_CMD] /Users/jari/Library/Android/sdk/emulator/emulator -verbose -no-audio -no-boot-anim -gpu swiftshader_indirect -port 15874 @NEXUS_6_API_31 14:48:13.053 detox[73180] ERROR: [SPAWN_FAIL] `/Users/jari/Library/Android/sdk/emulator/emulator -verbose -no-audio -no-boot-anim -gpu swiftshader_indirect -port 15874 @NEXUS_6_API_31` failed with code 1 err: ChildProcessError: `/Users/jari/Library/Android/sdk/emulator/emulator -verbose -no-audio -no-boot-anim -gpu swiftshader_indirect -port 15874 @NEXUS_6_API_31` failed with code 1 at ChildProcess.<anonymous> (/Users/jari/git/omapolkumobile/node_modules/child-process-promise/lib/index.js:132:23) at ChildProcess.emit (events.js:400:28) at ChildProcess.emit (domain.js:475:12) at maybeClose (internal/child_process.js:1088:16) at Process.ChildProcess._handle.onexit (internal/child_process.js:296:5) 14:48:13.055 detox[73180] ERROR: [SPAWN_FAIL] INFO | Android emulator version 31.3.10.0 (build_id 8807927) (CL:N/A) INFO | Found AVD name 'NEXUS_6_API_31' INFO | Found AVD target architecture: x86_64 INFO | argv[0]: '/Users/jari/Library/Android/sdk/emulator/emulator'; program directory: '/Users/jari/Library/Android/sdk/emulator' VERBOSE | Found directory: /Users/jari/Library/Android/sdk/system-images/android-31/google_apis/x86_64/ emulator: INFO: Found systemPath /Users/jari/Library/Android/sdk/system-images/android-31/google_apis/x86_64/ WARNING | unexpected system image feature string, emulator might not function correctly, please try updating the emulator. ERROR | Running multiple emulators with the same AVD
内容的提问来源于stack exchange,提问作者jaheikki
相关产品推荐
相关产品推荐

