Skip to content

Windows 上的安卓应用运行器:双击 APK 即装,应用以独立窗口与桌面图标运行,自持 QEMU 运行时

fix(emulator): 全新 AVD 启动被三个问题连环卡死,逐个修掉

RoutineMiku9f50dd1bd37f
diff --git a/engine/_tools/cyg/usr/sbin/e2fsck.exe b/engine/_tools/cyg/usr/sbin/e2fsck.exe
new file mode 100644
index 0000000..7dd2ca1
Binary files /dev/null and b/engine/_tools/cyg/usr/sbin/e2fsck.exe differ
diff --git a/engine/_tools/cyg/usr/sbin/mke2fs.exe b/engine/_tools/cyg/usr/sbin/mke2fs.exe
new file mode 100644
index 0000000..2172e62
Binary files /dev/null and b/engine/_tools/cyg/usr/sbin/mke2fs.exe differ
diff --git a/engine/_tools/cyg/usr/sbin/resize2fs.exe b/engine/_tools/cyg/usr/sbin/resize2fs.exe
new file mode 100644
index 0000000..30319e5
Binary files /dev/null and b/engine/_tools/cyg/usr/sbin/resize2fs.exe differ
diff --git a/engine/_tools/engine_dll/cygwin1.dll b/engine/_tools/engine_dll/cygwin1.dll
new file mode 100644
index 0000000..814d569
Binary files /dev/null and b/engine/_tools/engine_dll/cygwin1.dll differ
diff --git a/engine/_tools/libs/cygblkid-1.dll b/engine/_tools/libs/cygblkid-1.dll
new file mode 100644
index 0000000..d7e3092
Binary files /dev/null and b/engine/_tools/libs/cygblkid-1.dll differ
diff --git a/engine/_tools/libs/cygcom_err-2.dll b/engine/_tools/libs/cygcom_err-2.dll
new file mode 100644
index 0000000..40158c1
Binary files /dev/null and b/engine/_tools/libs/cygcom_err-2.dll differ
diff --git a/engine/_tools/libs/cyge2p-2.dll b/engine/_tools/libs/cyge2p-2.dll
new file mode 100644
index 0000000..a0a2464
Binary files /dev/null and b/engine/_tools/libs/cyge2p-2.dll differ
diff --git a/engine/_tools/libs/cygext2fs-2.dll b/engine/_tools/libs/cygext2fs-2.dll
new file mode 100644
index 0000000..253f4e9
Binary files /dev/null and b/engine/_tools/libs/cygext2fs-2.dll differ
diff --git a/engine/_tools/libs/cygiconv-2.dll b/engine/_tools/libs/cygiconv-2.dll
new file mode 100644
index 0000000..1105f2b
Binary files /dev/null and b/engine/_tools/libs/cygiconv-2.dll differ
diff --git a/engine/_tools/libs/cygintl-8.dll b/engine/_tools/libs/cygintl-8.dll
new file mode 100644
index 0000000..1b55d87
Binary files /dev/null and b/engine/_tools/libs/cygintl-8.dll differ
diff --git a/engine/_tools/libs/cyguuid-1.dll b/engine/_tools/libs/cyguuid-1.dll
new file mode 100644
index 0000000..1070ee2
Binary files /dev/null and b/engine/_tools/libs/cyguuid-1.dll differ
diff --git a/engine/_tools/mkfs-test/cygblkid-1.dll b/engine/_tools/mkfs-test/cygblkid-1.dll
new file mode 100644
index 0000000..d7e3092
Binary files /dev/null and b/engine/_tools/mkfs-test/cygblkid-1.dll differ
diff --git a/engine/_tools/mkfs-test/cygcom_err-2.dll b/engine/_tools/mkfs-test/cygcom_err-2.dll
new file mode 100644
index 0000000..40158c1
Binary files /dev/null and b/engine/_tools/mkfs-test/cygcom_err-2.dll differ
diff --git a/engine/_tools/mkfs-test/cyge2p-2.dll b/engine/_tools/mkfs-test/cyge2p-2.dll
new file mode 100644
index 0000000..a0a2464
Binary files /dev/null and b/engine/_tools/mkfs-test/cyge2p-2.dll differ
diff --git a/engine/_tools/mkfs-test/cygext2fs-2.dll b/engine/_tools/mkfs-test/cygext2fs-2.dll
new file mode 100644
index 0000000..253f4e9
Binary files /dev/null and b/engine/_tools/mkfs-test/cygext2fs-2.dll differ
diff --git a/engine/_tools/mkfs-test/cyggccpp-1.dll b/engine/_tools/mkfs-test/cyggccpp-1.dll
new file mode 100644
index 0000000..c5af48b
Binary files /dev/null and b/engine/_tools/mkfs-test/cyggccpp-1.dll differ
diff --git a/engine/_tools/mkfs-test/cyggctba-1.dll b/engine/_tools/mkfs-test/cyggctba-1.dll
new file mode 100644
index 0000000..8cb59e4
Binary files /dev/null and b/engine/_tools/mkfs-test/cyggctba-1.dll differ
diff --git a/engine/_tools/mkfs-test/cygiconv-2.dll b/engine/_tools/mkfs-test/cygiconv-2.dll
new file mode 100644
index 0000000..1105f2b
Binary files /dev/null and b/engine/_tools/mkfs-test/cygiconv-2.dll differ
diff --git a/engine/_tools/mkfs-test/cygintl-8.dll b/engine/_tools/mkfs-test/cygintl-8.dll
new file mode 100644
index 0000000..1b55d87
Binary files /dev/null and b/engine/_tools/mkfs-test/cygintl-8.dll differ
diff --git a/engine/_tools/mkfs-test/cygssp-0.dll b/engine/_tools/mkfs-test/cygssp-0.dll
new file mode 100644
index 0000000..8c00287
Binary files /dev/null and b/engine/_tools/mkfs-test/cygssp-0.dll differ
diff --git a/engine/_tools/mkfs-test/cyguuid-1.dll b/engine/_tools/mkfs-test/cyguuid-1.dll
new file mode 100644
index 0000000..1070ee2
Binary files /dev/null and b/engine/_tools/mkfs-test/cyguuid-1.dll differ
diff --git a/engine/_tools/mkfs-test/cygwin1.dll b/engine/_tools/mkfs-test/cygwin1.dll
new file mode 100644
index 0000000..814d569
Binary files /dev/null and b/engine/_tools/mkfs-test/cygwin1.dll differ
diff --git a/engine/_tools/mkfs-test/mke2fs.exe b/engine/_tools/mkfs-test/mke2fs.exe
new file mode 100644
index 0000000..2172e62
Binary files /dev/null and b/engine/_tools/mkfs-test/mke2fs.exe differ
diff --git a/engine/_tools/mkfs42/cygblkid-1.dll b/engine/_tools/mkfs42/cygblkid-1.dll
new file mode 100644
index 0000000..bd713a1
Binary files /dev/null and b/engine/_tools/mkfs42/cygblkid-1.dll differ
diff --git a/engine/_tools/mkfs42/cygcom_err-2.dll b/engine/_tools/mkfs42/cygcom_err-2.dll
new file mode 100644
index 0000000..79682d2
Binary files /dev/null and b/engine/_tools/mkfs42/cygcom_err-2.dll differ
diff --git a/engine/_tools/mkfs42/cyge2p-2.dll b/engine/_tools/mkfs42/cyge2p-2.dll
new file mode 100644
index 0000000..8db97fb
Binary files /dev/null and b/engine/_tools/mkfs42/cyge2p-2.dll differ
diff --git a/engine/_tools/mkfs42/cygext2fs-2.dll b/engine/_tools/mkfs42/cygext2fs-2.dll
new file mode 100644
index 0000000..d88d6a7
Binary files /dev/null and b/engine/_tools/mkfs42/cygext2fs-2.dll differ
diff --git a/engine/_tools/mkfs42/cyggcc_s-1.dll b/engine/_tools/mkfs42/cyggcc_s-1.dll
new file mode 100644
index 0000000..4b7fa0c
Binary files /dev/null and b/engine/_tools/mkfs42/cyggcc_s-1.dll differ
diff --git a/engine/_tools/mkfs42/cygiconv-2.dll b/engine/_tools/mkfs42/cygiconv-2.dll
new file mode 100644
index 0000000..e223e4b
Binary files /dev/null and b/engine/_tools/mkfs42/cygiconv-2.dll differ
diff --git a/engine/_tools/mkfs42/cygintl-8.dll b/engine/_tools/mkfs42/cygintl-8.dll
new file mode 100644
index 0000000..47a3169
Binary files /dev/null and b/engine/_tools/mkfs42/cygintl-8.dll differ
diff --git a/engine/_tools/mkfs42/cyguuid-1.dll b/engine/_tools/mkfs42/cyguuid-1.dll
new file mode 100644
index 0000000..c870826
Binary files /dev/null and b/engine/_tools/mkfs42/cyguuid-1.dll differ
diff --git a/engine/_tools/mkfs42/cygwin1.dll b/engine/_tools/mkfs42/cygwin1.dll
new file mode 100644
index 0000000..fbda26a
Binary files /dev/null and b/engine/_tools/mkfs42/cygwin1.dll differ
diff --git a/engine/_tools/mkfs42/mke2fs.exe b/engine/_tools/mkfs42/mke2fs.exe
new file mode 100644
index 0000000..3993e4c
Binary files /dev/null and b/engine/_tools/mkfs42/mke2fs.exe differ
diff --git a/src/core/emulator.js b/src/core/emulator.js
index 7ca6e7a..f671131 100644
--- a/src/core/emulator.js
+++ b/src/core/emulator.js
@@ -98,6 +98,65 @@ class EmulatorEngine {
} catch (_) { /* ignore */ }
}
+ /** AVD 目录里是否真的有一份 quickboot 快照 */
+ hasSnapshot() {
+ try {
+ const dir = path.join(this.provisioner.paths().avdHome, `${AVD_NAME}.avd`, 'snapshots');
+ return fs.readdirSync(dir).length > 0;
+ } catch (_) {
+ return false;
+ }
+ }
+
+ /**
+ * 清掉占着 AVD 锁的孤儿模拟器。
+ * emulator.exe 只是个启动器,真正的 qemu 被 spawn 成独立进程(detached),
+ * 所以只 kill 自己手上的 proc 句柄经常留个孤儿在那占着 multiinstance.lock,
+ * 下一次启动就会 FATAL "Running multiple emulators with the same AVD"。
+ * 按「AVD 锁里的 pid」+「命令行带本 AVD 名的 qemu/emulator」两个来源清。
+ */
+ async killOrphanProcesses({ onLog = () => {} } = {}) {
+ if (process.platform !== 'win32') return 0;
+ const { execFile } = require('child_process');
+ const avdDir = path.join(this.provisioner.paths().avdHome, `${AVD_NAME}.avd`);
+ const pids = new Set();
+ // 1) 锁文件里 emulator 自己写的 pid(最可靠)
+ try {
+ const f = path.join(avdDir, 'hardware-qemu.ini.lock', 'pid');
+ if (fs.existsSync(f) && fs.statSync(f).isFile()) {
+ const pid = parseInt(fs.readFileSync(f, 'utf8').trim(), 10);
+ if (pid) pids.add(pid);
+ }
+ } catch (_) { /* ignore */ }
+ // 2) 兜底:命令行里引用本 AVD 的 qemu / emulator
+ try {
+ const out = await new Promise((res) => execFile(
+ 'wmic',
+ ['process', 'where', "name='qemu-system-x86_64.exe' or name='emulator.exe'", 'get', 'processid,commandline', '/format:csv'],
+ { windowsHide: true, maxBuffer: 32 * 1024 * 1024 },
+ (e, o) => res(e ? '' : o || ''),
+ ));
+ for (const line of String(out).split('\n')) {
+ if (line.includes(AVD_NAME) && /qemu|emulator/i.test(line)) {
+ const m = /,(\d+)\s*$/.exec(line.trim());
+ if (m) pids.add(Number(m[1]));
+ }
+ }
+ } catch (_) { /* wmic 不可用就只靠锁文件 */ }
+ pids.delete(process.pid);
+ if (!pids.size) {
+ this.clearLocks(avdDir);
+ return 0;
+ }
+ onLog(`清理 ${pids.size} 个残留模拟器进程`);
+ await Promise.all([...pids].map((pid) => new Promise((r) => {
+ execFile('taskkill', ['/PID', String(pid), '/T', '/F'], { windowsHide: true }, () => r());
+ })));
+ await sleep(1200);
+ this.clearLocks(avdDir);
+ return pids.size;
+ }
+
/**
* 准备 AVD:写 config.ini。每次启动按当前自动配置重写,保证降级重试即时生效。
*/
@@ -235,6 +294,9 @@ class EmulatorEngine {
'-no-audio',
'-no-boot-anim',
'-no-metrics',
+ // 关掉崩溃上报:一旦历史崩溃报告存在,emulator 会弹「同意上传」模态框,
+ // 主线程被卡住、永远不开机(日志表现为 Showing crashdialog to get consent)。
+ '-crash-report-mode', 'disabled',
'-gpu', gpu,
'-memory', String(memoryMB),
'-cores', String(cores),
@@ -252,7 +314,11 @@ class EmulatorEngine {
// 不要模拟器自带的手机外壳;侧边浮动工具栏由窗口代理隐藏。
args.push('-no-skin');
}
- if (snapshot && !wipe) args.push('-snapshot', SNAPSHOT_NAME);
+ // 只有快照真的存在才传 -snapshot:全新 AVD(或快照刚被作废)时传了它,
+ // qemu 会一直等一份不存在的内存状态,进程活着但永远不开机——
+ // 上层只看到「开机超时」,接着换参数重试,反而被 AVD 锁挡成
+ // "Running multiple emulators with the same AVD"。
+ if (snapshot && !wipe && this.hasSnapshot()) args.push('-snapshot', SNAPSHOT_NAME);
else args.push('-no-snapshot');
if (wipe) args.push('-wipe-data');
if (accel === 'off') args.push('-accel', 'off');
@@ -318,12 +384,16 @@ class EmulatorEngine {
async killAttempt() {
const proc = this.proc;
this.proc = null;
- if (!proc) return;
- try { proc.kill(); } catch (_) { /* ignore */ }
- if (process.platform === 'win32' && proc.pid) {
- try { spawn('taskkill', ['/PID', String(proc.pid), '/T', '/F'], { windowsHide: true, stdio: 'ignore' }); } catch (_) { /* ignore */ }
+ if (proc) {
+ try { proc.kill(); } catch (_) { /* ignore */ }
+ if (process.platform === 'win32' && proc.pid) {
+ try { spawn('taskkill', ['/PID', String(proc.pid), '/T', '/F'], { windowsHide: true, stdio: 'ignore' }); } catch (_) { /* ignore */ }
+ }
}
- await sleep(1200);
+ await sleep(800);
+ // proc 句柄常常已经没了(emulator.exe 启动完就退出,qemu 是 detached 的独立进程),
+ // 不按进程名兜底清一遍,下一轮就会被 AVD 锁挡成 FATAL。
+ await this.killOrphanProcesses().catch(() => {});
}
/**
@@ -382,6 +452,11 @@ class EmulatorEngine {
this.lastDiagnosis = null;
let lastError = '';
+ // 冷启前先清孤儿:上一次运行留下的 qemu 仍占着 AVD 锁时,
+ // 所有尝试都会立刻 FATAL "Running multiple emulators with the same AVD"。
+ // eslint-disable-next-line no-await-in-loop
+ await this.killOrphanProcesses({ onLog }).catch(() => {});
+
for (let i = 0; i < variants.length; i += 1) {
const base = variants[i];
// 端口占用也会导致失败,每次重试重新探测一次
diff --git a/src/core/engine.js b/src/core/engine.js
index b2cfaa6..b0264b8 100644
--- a/src/core/engine.js
+++ b/src/core/engine.js
@@ -651,45 +651,10 @@ class Runtime {
* 清理上一次运行遗留、却没被接管的孤儿模拟器。
* 关掉 ApkStart 时 emulator 是 detached 的,qemu 会存活;下次启动时它仍占着 AVD 锁,
* 表现为冷启直接 FATAL "Running multiple emulators with the same AVD"。
- * 优先用 AVD 锁文件里 emulator 自己写的 PID(最可靠),再兜底按命令行扫一遍本 AVD 的 qemu/emulator。
+ * 实现收敛到 EmulatorEngine,重试路径(killAttempt)也用同一份逻辑。
*/
async killOrphanEmulators({ onLog = () => {} } = {}) {
- const avdDir = path.join(this.provisioner.paths().avdHome, `${AVD_NAME}.avd`);
- const killPid = (pid) => new Promise((resolve) => {
- if (!pid) return resolve();
- const { execFile } = require('child_process');
- execFile('taskkill', ['/PID', String(pid), '/T', '/F'], { windowsHide: true }, () => resolve());
- });
- const pids = new Set();
- // 1) 锁文件记录的 PID(emulator 启动时写入,指向 emulator.exe,/T 会连带杀掉 qemu)
- try {
- const f = path.join(avdDir, 'hardware-qemu.ini.lock', 'pid');
- if (fs.existsSync(f) && fs.statSync(f).isFile()) {
- const pid = parseInt(fs.readFileSync(f, 'utf8').trim(), 10);
- if (pid) pids.add(pid);
- }
- } catch (_) { /* ignore */ }
- // 2) 兜底:命令行里引用本 AVD 的 qemu / emulator 全部强杀(wmic 不可用时跳过)
- try {
- const { execFile } = require('child_process');
- const out = await new Promise((res) => execFile(
- 'wmic',
- ['process', 'where', "name='qemu-system-x86_64.exe' or name='emulator.exe'", 'get', 'processid,commandline', '/format:csv'],
- { windowsHide: true, maxBuffer: 32 * 1024 * 1024 },
- (e, o) => res(e ? '' : o || ''),
- ));
- for (const line of String(out).split('\n')) {
- if (line.includes(AVD_NAME) && /qemu|emulator/i.test(line)) {
- const m = /,(\d+)\s*$/.exec(line.trim());
- if (m) pids.add(Number(m[1]));
- }
- }
- } catch (_) { /* wmic 不可用就跳过,锁文件 PID 已覆盖绝大多数情况 */ }
- if (pids.size) {
- onLog(`清理 ${pids.size} 个残留模拟器进程`);
- await Promise.all([...pids].map(killPid));
- await new Promise((r) => setTimeout(r, 1500));
- }
+ return this.emulator.killOrphanProcesses({ onLog });
}
async ensure({ onLog = () => {} } = {}) {