From eca22f71dc1b472bd005c34519167f8f82c7b719 Mon Sep 17 00:00:00 2001 From: Afzal Najam Date: Mon, 1 Mar 2021 21:17:25 -0500 Subject: [PATCH 1/2] Add option to modify the number of cores --- README.md | 1 + action.yml | 2 ++ lib/emulator-manager.js | 5 ++++- lib/main.js | 5 ++++- src/emulator-manager.ts | 5 +++++ src/main.ts | 6 +++++- 6 files changed, 21 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4158771cb..6fc82ece1 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". | +| `cores` | Optional | N/A | Number of cores to use for the emulator (`hw.cpu.ncore` in config.ini). | | `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`. | diff --git a/action.yml b/action.yml index 94857d07f..32bed0d31 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`' + cores: + description: 'the number of cores to use for the emulator' 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: diff --git a/lib/emulator-manager.js b/lib/emulator-manager.js index 5ee9f5e6d..8582df9bb 100644 --- a/lib/emulator-manager.js +++ b/lib/emulator-manager.js @@ -34,13 +34,16 @@ const EMULATOR_BOOT_TIMEOUT_SECONDS = 600; /** * Creates and launches a new AVD instance with the specified configurations. */ -function launchEmulator(apiLevel, target, arch, profile, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations) { +function launchEmulator(apiLevel, target, arch, profile, cores, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations) { return __awaiter(this, void 0, void 0, function* () { // create a new AVD const profileOption = profile.trim() !== '' ? `--device '${profile}'` : ''; const sdcardPathOrSizeOption = sdcardPathOrSize.trim() !== '' ? `--sdcard '${sdcardPathOrSize}'` : ''; console.log(`Creating AVD.`); yield exec.exec(`sh -c \\"echo no | avdmanager create avd --force -n "${avdName}" --abi '${target}/${arch}' --package 'system-images;android-${apiLevel};${target};${arch}' ${profileOption} ${sdcardPathOrSizeOption}"`); + if (cores) { + yield exec.exec(`sh -c \\"printf 'hw.cpu.ncore=${cores}\n' >> ~/.android/avd/"${avdName}".avd"/config.ini`); + } // start emulator console.log('Starting emulator.'); // turn off hardware acceleration on Linux diff --git a/lib/main.js b/lib/main.js index 16e020fb0..7f1219a3a 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}`); + // Number of cores to use for emulator + const cores = core.getInput('cores'); + console.log(`Cores: ${cores}`); // 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}`); @@ -112,7 +115,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, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations); + yield emulator_manager_1.launchEmulator(apiLevel, target, arch, profile, cores, 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 3065a86f8..48c012f7a 100644 --- a/src/emulator-manager.ts +++ b/src/emulator-manager.ts @@ -10,6 +10,7 @@ export async function launchEmulator( target: string, arch: string, profile: string, + cores: string, sdcardPathOrSize: string, avdName: string, emulatorOptions: string, @@ -23,6 +24,10 @@ export async function launchEmulator( `sh -c \\"echo no | avdmanager create avd --force -n "${avdName}" --abi '${target}/${arch}' --package 'system-images;android-${apiLevel};${target};${arch}' ${profileOption} ${sdcardPathOrSizeOption}"` ); + if (cores) { + await exec.exec(`sh -c \\"printf 'hw.cpu.ncore=${cores}\n' >> ~/.android/avd/"${avdName}".avd"/config.ini`); + } + // start emulator console.log('Starting emulator.'); diff --git a/src/main.ts b/src/main.ts index 4ed0fe6b1..c671dd6cc 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}`); + // Number of cores to use for emulator + const cores = core.getInput('cores'); + console.log(`Cores: ${cores}`); + // 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}`); @@ -98,7 +102,7 @@ async function run() { await installAndroidSdk(apiLevel, target, arch, emulatorBuild, ndkVersion, cmakeVersion); // launch an emulator - await launchEmulator(apiLevel, target, arch, profile, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations); + await launchEmulator(apiLevel, target, arch, profile, cores, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations); // execute the custom script try { From a710c6126c172d8c2579047633fdde7629bf2608 Mon Sep 17 00:00:00 2001 From: Afzal Najam Date: Tue, 2 Mar 2021 12:34:46 -0500 Subject: [PATCH 2/2] Change default cores to 2, add cores in workflow --- .github/workflows/workflow.yml | 1 + README.md | 2 +- action.yml | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index a677231f3..fb9535c18 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -71,6 +71,7 @@ jobs: target: ${{ matrix.target }} arch: x86 profile: Nexus 6 + cores: 2 sdcard-path-or-size: 100M avd-name: test emulator-options: -no-window -gpu swiftshader_indirect -no-snapshot -noaudio -no-boot-anim -camera-back none diff --git a/README.md b/README.md index 6fc82ece1..7273c5a7e 100644 --- a/README.md +++ b/README.md @@ -92,7 +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". | -| `cores` | Optional | N/A | Number of cores to use for the emulator (`hw.cpu.ncore` in config.ini). | +| `cores` | Optional | 2 | Number of cores to use for the emulator (`hw.cpu.ncore` in config.ini). | | `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`. | diff --git a/action.yml b/action.yml index 32bed0d31..f67a96d76 100644 --- a/action.yml +++ b/action.yml @@ -18,6 +18,7 @@ inputs: description: 'hardware profile used for creating the AVD - e.g. `Nexus 6`' cores: description: 'the number of cores to use for the emulator' + default: 2 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: