From 1d99f89d5c24e7cd3ab88a496d5c9b5e832049c2 Mon Sep 17 00:00:00 2001 From: vitalyster Date: Tue, 5 Mar 2024 17:03:03 +0300 Subject: [PATCH] Use `xvfb-run` to run emulator on Linux --- lib/emulator-manager.js | 6 +++++- lib/sdk-installer.js | 5 +++++ src/emulator-manager.ts | 8 +++++++- src/sdk-installer.ts | 5 +++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/emulator-manager.js b/lib/emulator-manager.js index 5225a189b..679351eda 100644 --- a/lib/emulator-manager.js +++ b/lib/emulator-manager.js @@ -70,9 +70,13 @@ function launchEmulator(apiLevel, target, arch, profile, cores, ramSize, heapSiz console.log('Disabling Linux hardware acceleration.'); emulatorOptions += ' -accel off'; } + let emulatorCommandPrefix; + if (process.platform === 'linux') { + emulatorCommandPrefix = 'xvfb-run'; + } // start emulator console.log('Starting emulator.'); - yield exec.exec(`sh -c \\"${process.env.ANDROID_HOME}/emulator/emulator -avd "${avdName}" ${emulatorOptions} &"`, [], { + yield exec.exec(`sh -c \\"${emulatorCommandPrefix} ${process.env.ANDROID_HOME}/emulator/emulator -avd "${avdName}" ${emulatorOptions} &"`, [], { listeners: { stderr: (data) => { if (data.toString().includes('invalid command-line parameter')) { diff --git a/lib/sdk-installer.js b/lib/sdk-installer.js index e6fbf75cd..f13093319 100644 --- a/lib/sdk-installer.js +++ b/lib/sdk-installer.js @@ -52,6 +52,7 @@ function installAndroidSdk(apiLevel, target, arch, channelId, emulatorBuild, ndk console.log(`::group::Install Android SDK`); const isOnMac = process.platform === 'darwin'; const isArm = process.arch === 'arm64'; + const isLinux = process.platform === 'linux'; if (!isOnMac) { yield exec.exec(`sh -c \\"sudo chown $USER:$USER ${process.env.ANDROID_HOME} -R`); } @@ -106,6 +107,10 @@ function installAndroidSdk(apiLevel, target, arch, channelId, emulatorBuild, ndk console.log(`Installing CMake ${cmakeVersion}.`); yield exec.exec(`sh -c \\"sdkmanager --install 'cmake;${cmakeVersion}' --channel=${channelId} > /dev/null"`); } + if (isLinux) { + console.log('Installing emulator dependencies.'); + yield exec.exec(`sh -c \\"sudo apt install libpulse0 xvfb"`); + } } finally { console.log(`::endgroup::`); diff --git a/src/emulator-manager.ts b/src/emulator-manager.ts index d1fb364f8..f75257b0c 100644 --- a/src/emulator-manager.ts +++ b/src/emulator-manager.ts @@ -62,10 +62,16 @@ export async function launchEmulator( emulatorOptions += ' -accel off'; } + let emulatorCommandPrefix; + + if (process.platform === 'linux') { + emulatorCommandPrefix = 'xvfb-run' + } + // start emulator console.log('Starting emulator.'); - await exec.exec(`sh -c \\"${process.env.ANDROID_HOME}/emulator/emulator -avd "${avdName}" ${emulatorOptions} &"`, [], { + await exec.exec(`sh -c \\"${emulatorCommandPrefix} ${process.env.ANDROID_HOME}/emulator/emulator -avd "${avdName}" ${emulatorOptions} &"`, [], { listeners: { stderr: (data: Buffer) => { if (data.toString().includes('invalid command-line parameter')) { diff --git a/src/sdk-installer.ts b/src/sdk-installer.ts index 0fd55c8bc..ddb1f2a43 100644 --- a/src/sdk-installer.ts +++ b/src/sdk-installer.ts @@ -18,6 +18,7 @@ export async function installAndroidSdk(apiLevel: string, target: string, arch: console.log(`::group::Install Android SDK`); const isOnMac = process.platform === 'darwin'; const isArm = process.arch === 'arm64'; + const isLinux = process.platform === 'linux'; if (!isOnMac) { await exec.exec(`sh -c \\"sudo chown $USER:$USER ${process.env.ANDROID_HOME} -R`); @@ -79,6 +80,10 @@ export async function installAndroidSdk(apiLevel: string, target: string, arch: console.log(`Installing CMake ${cmakeVersion}.`); await exec.exec(`sh -c \\"sdkmanager --install 'cmake;${cmakeVersion}' --channel=${channelId} > /dev/null"`); } + if (isLinux) { + console.log('Installing emulator dependencies.'); + await exec.exec(`sh -c \\"sudo apt install libpulse0 xvfb"`) + } } finally { console.log(`::endgroup::`); }