From b0f3c521a52eeba97926f0bf809efae03490404b Mon Sep 17 00:00:00 2001 From: Yang Date: Sun, 27 Dec 2020 19:41:13 +1100 Subject: [PATCH] Support specifying SD card path or size. --- .github/workflows/workflow.yml | 1 + README.md | 1 + action.yml | 2 ++ lib/emulator-manager.js | 14 +++++--------- lib/main.js | 5 ++++- src/emulator-manager.ts | 22 ++++++++++++++-------- src/main.ts | 6 +++++- test-fixture/app/build.gradle | 1 - 8 files changed, 32 insertions(+), 20 deletions(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index e6316e9cf..12fac2924 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -68,6 +68,7 @@ jobs: target: ${{ matrix.target }} arch: x86 profile: Nexus 6 + sdcard-path-or-size: 100M avd-name: test emulator-options: -no-window -gpu swiftshader_indirect -no-snapshot -noaudio -no-boot-anim -camera-back none disable-animations: true diff --git a/README.md b/README.md index cd88e7964..830c348f7 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ jobs: | `target` | Optional | `default` | Target of the system image - `default`, `google_apis` or `playstore`. | | `arch` | Optional | `x86` | CPU architecture of the system image - `x86` or `x86_64`. Note that `x86_64` image is only available for API 21+. | | `profile` | Optional | N/A | Hardware profile used for creating the AVD - e.g. `Nexus 6`. For a list of all profiles available, run `avdmanager list` and refer to the results under "Available Android Virtual Devices". | +| `sdcard-path-or-size` | Optional | N/A | Path to the SD card image for this AVD or the size of a new SD card image to create for this AVD, in KB or MB, denoted with K or M. - e.g. `path/to/sdcard`, or `1000M`. | | `avd-name` | Optional | `test` | Custom AVD name used for creating the Android Virtual Device. | | `emulator-options` | Optional | See below | Command-line options used when launching the emulator (replacing all default options) - e.g. `-no-window -no-snapshot -camera-back emulated`. | | `disable-animations` | Optional | `true` | Whether to disable animations - `true` or `false`. | diff --git a/action.yml b/action.yml index 77b3947be..94857d07f 100644 --- a/action.yml +++ b/action.yml @@ -16,6 +16,8 @@ inputs: default: 'x86' profile: description: 'hardware profile used for creating the AVD - e.g. `Nexus 6`' + sdcard-path-or-size: + description: 'path to the SD card image for this AVD or the size of a new SD card image to create for this AVD, in KB or MB, denoted with K or M. - e.g. `path/to/sdcard`, or `1000M`' avd-name: description: 'custom AVD name used for creating the Android Virtual Device' default: 'test' diff --git a/lib/emulator-manager.js b/lib/emulator-manager.js index 023161411..d7b99d9b1 100644 --- a/lib/emulator-manager.js +++ b/lib/emulator-manager.js @@ -34,17 +34,13 @@ const EMULATOR_BOOT_TIMEOUT_SECONDS = 600; /** * Creates and launches a new AVD instance with the specified configurations. */ -function launchEmulator(apiLevel, target, arch, profile, avdName, emulatorOptions, disableAnimations) { +function launchEmulator(apiLevel, target, arch, profile, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations) { return __awaiter(this, void 0, void 0, function* () { // create a new AVD - if (profile.trim() !== '') { - console.log(`Creating AVD with custom profile ${profile}`); - yield exec.exec(`avdmanager create avd --force -n "${avdName}" --abi "${target}/${arch}" --package "system-images;android-${apiLevel};${target};${arch}" --device "${profile}"`); - } - else { - console.log(`Creating AVD without custom profile.`); - yield exec.exec(`sh -c \\"echo no | avdmanager create avd --force -n "${avdName}" --abi '${target}/${arch}' --package 'system-images;android-${apiLevel};${target};${arch}'"`); - } + const profileOption = profile.trim() !== '' ? `--device "${profile}"` : ''; + const sdcardPathOrSizeOption = sdcardPathOrSize.trim() !== '' ? `--sdcard "${sdcardPathOrSize}"` : ''; + console.log(`Creating AVD.`); + yield exec.exec(`avdmanager create avd --force -n "${avdName}" --abi "${target}/${arch}" --package "system-images;android-${apiLevel};${target};${arch}" ${profileOption} ${sdcardPathOrSizeOption}`); // start emulator console.log('Starting emulator.'); // turn off hardware acceleration on Linux diff --git a/lib/main.js b/lib/main.js index 478426a27..16e020fb0 100644 --- a/lib/main.js +++ b/lib/main.js @@ -63,6 +63,9 @@ function run() { // Hardware profile used for creating the AVD const profile = core.getInput('profile'); console.log(`Hardware profile: ${profile}`); + // SD card path or size used for creating the AVD + const sdcardPathOrSize = core.getInput('sdcard-path-or-size'); + console.log(`SD card path or size: ${sdcardPathOrSize}`); // custom name used for creating the AVD const avdName = core.getInput('avd-name'); console.log(`AVD name: ${avdName}`); @@ -109,7 +112,7 @@ function run() { // install SDK yield sdk_installer_1.installAndroidSdk(apiLevel, target, arch, emulatorBuild, ndkVersion, cmakeVersion); // launch an emulator - yield emulator_manager_1.launchEmulator(apiLevel, target, arch, profile, avdName, emulatorOptions, disableAnimations); + yield emulator_manager_1.launchEmulator(apiLevel, target, arch, profile, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations); // execute the custom script try { // move to custom working directory if set diff --git a/src/emulator-manager.ts b/src/emulator-manager.ts index 0daceab15..35cfc6c57 100644 --- a/src/emulator-manager.ts +++ b/src/emulator-manager.ts @@ -5,15 +5,21 @@ const EMULATOR_BOOT_TIMEOUT_SECONDS = 600; /** * Creates and launches a new AVD instance with the specified configurations. */ -export async function launchEmulator(apiLevel: number, target: string, arch: string, profile: string, avdName: string, emulatorOptions: string, disableAnimations: boolean): Promise { +export async function launchEmulator( + apiLevel: number, + target: string, + arch: string, + profile: string, + sdcardPathOrSize: string, + avdName: string, + emulatorOptions: string, + disableAnimations: boolean +): Promise { // create a new AVD - if (profile.trim() !== '') { - console.log(`Creating AVD with custom profile ${profile}`); - await exec.exec(`avdmanager create avd --force -n "${avdName}" --abi "${target}/${arch}" --package "system-images;android-${apiLevel};${target};${arch}" --device "${profile}"`); - } else { - console.log(`Creating AVD without custom profile.`); - await exec.exec(`sh -c \\"echo no | avdmanager create avd --force -n "${avdName}" --abi '${target}/${arch}' --package 'system-images;android-${apiLevel};${target};${arch}'"`); - } + const profileOption = profile.trim() !== '' ? `--device "${profile}"` : ''; + const sdcardPathOrSizeOption = sdcardPathOrSize.trim() !== '' ? `--sdcard "${sdcardPathOrSize}"` : ''; + console.log(`Creating AVD.`); + await exec.exec(`avdmanager create avd --force -n "${avdName}" --abi "${target}/${arch}" --package "system-images;android-${apiLevel};${target};${arch}" ${profileOption} ${sdcardPathOrSizeOption}`); // start emulator console.log('Starting emulator.'); diff --git a/src/main.ts b/src/main.ts index 5407d95c0..4ed0fe6b1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -39,6 +39,10 @@ async function run() { const profile = core.getInput('profile'); console.log(`Hardware profile: ${profile}`); + // SD card path or size used for creating the AVD + const sdcardPathOrSize = core.getInput('sdcard-path-or-size'); + console.log(`SD card path or size: ${sdcardPathOrSize}`); + // custom name used for creating the AVD const avdName = core.getInput('avd-name'); console.log(`AVD name: ${avdName}`); @@ -94,7 +98,7 @@ async function run() { await installAndroidSdk(apiLevel, target, arch, emulatorBuild, ndkVersion, cmakeVersion); // launch an emulator - await launchEmulator(apiLevel, target, arch, profile, avdName, emulatorOptions, disableAnimations); + await launchEmulator(apiLevel, target, arch, profile, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations); // execute the custom script try { diff --git a/test-fixture/app/build.gradle b/test-fixture/app/build.gradle index bcd260249..abb3c49fd 100644 --- a/test-fixture/app/build.gradle +++ b/test-fixture/app/build.gradle @@ -1,6 +1,5 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' -apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 30