mirror of
https://github.com/reactivecircus/android-emulator-runner.git
synced 2026-09-01 01:59:32 +00:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 769ff281b3 | |||
| 0d966550cf | |||
| eb9e069f3f | |||
| 0ac3f34493 | |||
| 345ace6178 | |||
| 490f28e17c | |||
| fc08a40bf0 | |||
| 75464a704b | |||
| de92fdaa03 | |||
| 47063ae93b | |||
| 6bb3965c96 | |||
| da846ad45b | |||
| ede514c8c2 | |||
| 4a949058e2 | |||
| e1015a487b | |||
| 883b8114b1 |
@@ -9,11 +9,17 @@ on:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: macOS-latest
|
||||
timeout-minutes: 10
|
||||
runs-on: ${{ matrix.os }}
|
||||
timeout-minutes: 15
|
||||
strategy:
|
||||
matrix:
|
||||
api-level: [21, 23, 29]
|
||||
os: [macos-latest, ubuntu-latest]
|
||||
api-level: [16, 23, 29]
|
||||
exclude:
|
||||
- os: ubuntu-latest
|
||||
api-level: 23
|
||||
- os: ubuntu-latest
|
||||
api-level: 29
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v2
|
||||
@@ -28,16 +34,16 @@ jobs:
|
||||
npm run lint
|
||||
npm test
|
||||
|
||||
- name: Java 13
|
||||
- name: Java 14
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 13
|
||||
java-version: 14
|
||||
|
||||
- name: run action
|
||||
uses: ./
|
||||
with:
|
||||
api-level: ${{ matrix.api-level }}
|
||||
target: google_apis
|
||||
target: default
|
||||
arch: x86
|
||||
profile: Nexus 6
|
||||
emulator-options: -no-window -gpu swiftshader_indirect -no-snapshot -noaudio -no-boot-anim -camera-back none
|
||||
@@ -45,5 +51,6 @@ jobs:
|
||||
working-directory: ./test-fixture/
|
||||
script: |
|
||||
echo $GITHUB_REPOSITORY
|
||||
adb devices
|
||||
./gradlew help
|
||||
./gradlew connectedDebugAndroidTest
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
# Change Log
|
||||
|
||||
## v2.6.0
|
||||
|
||||
* Added support for Linux VMs (no hardware acceleration).
|
||||
|
||||
## v2.5.0
|
||||
|
||||
* Added support for API 15-19 system images.
|
||||
* Switched to the new SDK command-line tools which supports running `sdkmanager` and `avdmanager` with Java 9+.
|
||||
|
||||
## v2.4.0
|
||||
|
||||
* Added support for setting custom `working-directory` - e.g. `./android` if your root Gradle project is under the `./android` sub-directory within your repository.
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<a href="https://github.com/ReactiveCircus/android-emulator-runner"><img alt="GitHub Actions status" src="https://github.com/ReactiveCircus/android-emulator-runner/workflows/Main%20workflow/badge.svg"></a>
|
||||
</p>
|
||||
|
||||
A GitHub Action for installing, configuring and running Android Emulators on macOS virtual machines.
|
||||
A GitHub Action for installing, configuring and running hardware-accelerated Android Emulators on macOS virtual machines (or Linux virtual machines but without hardware accleration).
|
||||
|
||||
The old ARM-based emulators were slow and are no longer supported by Google. The modern Intel Atom (x86 and x86_64) emulators require hardware acceleration (HAXM on Mac & Windows, QEMU on Linux) from the host to run fast. This presents a challenge on CI as to be able to run hardware accelerated emulators within a docker container, **KVM** must be supported by the host VM which isn't the case for cloud-based CI providers due to infrastructural limits. If you want to learn more about this, here's an article I wrote: [Running Android Instrumented Tests on CI](https://dev.to/ychescale9/running-android-emulators-on-ci-from-bitrise-io-to-github-actions-3j76).
|
||||
|
||||
@@ -22,14 +22,16 @@ This action automates the process by doing the following:
|
||||
|
||||
## Usage
|
||||
|
||||
Note that this action must be run on a **macOS** VM, e.g. `macOS-latest` or `macOS-10.14`.
|
||||
It is recommended to run this action on a **macOS** VM, e.g. `macos-latest` or `macos-10.15` to take advantage of hardware accleration support provided by **HAXM**.
|
||||
|
||||
Please note that while Linux VMs (e.g. `ubuntu-latest` or `ubuntu-18.04`) are also supported, hardware acceleration will **not** be available.
|
||||
|
||||
A workflow that uses **android-emulator-runner** to run your instrumented tests on **API 29**:
|
||||
|
||||
```
|
||||
jobs:
|
||||
test:
|
||||
runs-on: macOS-latest
|
||||
runs-on: macos-latest
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v2
|
||||
@@ -46,7 +48,7 @@ We can also leverage GitHub Actions's build matrix to test across multiple confi
|
||||
```
|
||||
jobs:
|
||||
test:
|
||||
runs-on: macOS-latest
|
||||
runs-on: macos-latest
|
||||
strategy:
|
||||
matrix:
|
||||
api-level: [21, 23, 29]
|
||||
@@ -69,9 +71,9 @@ jobs:
|
||||
|
||||
| | **Required** | **Default** | **Description** |
|
||||
|----------------------|--------------|-------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `api-level` | Required | N/A | API level of the platform system image - e.g. 23 for Android Marshmallow, 29 for Android 10. **Minimum API level supported is 21**. |
|
||||
| `api-level` | Required | N/A | API level of the platform system image - e.g. 23 for Android Marshmallow, 29 for Android 10. **Minimum API level supported is 15**. |
|
||||
| `target` | Optional | `default` | Target of the system image - `default` or `google_apis`. |
|
||||
| `arch` | Optional | `x86` | CPU architecture of the system image - `x86` or `x86_64`. |
|
||||
| `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 `$ANDROID_HOME/tools/bin/avdmanager list` and refer to the results under "Available Android Virtual Devices". |
|
||||
| `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`. |
|
||||
@@ -80,3 +82,5 @@ jobs:
|
||||
| `script` | Required | N/A | Custom script to run - e.g. to run Android instrumented tests on the emulator: `./gradlew connectedCheck` |
|
||||
|
||||
Default `emulator-options`: `-no-window -gpu swiftshader_indirect -no-snapshot -noaudio -no-boot-anim`.
|
||||
|
||||
_Please note that if you are running on a Linux VM, `-no-accel` will be added to the `emulator-options` to make sure hardware acceleration is turned off._
|
||||
|
||||
@@ -17,14 +17,14 @@ describe('api-level validator tests', () => {
|
||||
|
||||
it('Throws if api-level is lower than min API supported', () => {
|
||||
const func = () => {
|
||||
validator.checkApiLevel('20');
|
||||
validator.checkApiLevel('14');
|
||||
};
|
||||
expect(func).toThrowError(`Minimum API level supported is ${validator.MIN_API_LEVEL}.`);
|
||||
});
|
||||
|
||||
it('Validates successfully with valid api-level', () => {
|
||||
const func1 = () => {
|
||||
validator.checkApiLevel('21');
|
||||
validator.checkApiLevel('15');
|
||||
};
|
||||
expect(func1).not.toThrow();
|
||||
|
||||
|
||||
+12
-10
@@ -17,8 +17,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const exec = __importStar(require("@actions/exec"));
|
||||
const EMULATOR_BOOT_TIMEOUT_SECONDS = 300;
|
||||
const AVD_MANAGER_PATH = `${process.env.ANDROID_HOME}/tools/bin/avdmanager`;
|
||||
const ADB_PATH = `${process.env.ANDROID_HOME}/platform-tools/adb`;
|
||||
/**
|
||||
* Creates and launches a new AVD instance with the specified configurations.
|
||||
*/
|
||||
@@ -27,14 +25,18 @@ function launchEmulator(apiLevel, target, arch, profile, emulatorOptions, disabl
|
||||
// create a new AVD
|
||||
if (profile.trim() !== '') {
|
||||
console.log(`Creating AVD with custom profile ${profile}`);
|
||||
yield exec.exec(`${AVD_MANAGER_PATH} create avd --force -n test --abi "${target}/${arch}" --package "system-images;android-${apiLevel};${target};${arch}" --device "${profile}"`);
|
||||
yield exec.exec(`avdmanager create avd --force -n test --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 | ${AVD_MANAGER_PATH} create avd --force -n test --abi '${target}/${arch}' --package 'system-images;android-${apiLevel};${target};${arch}'"`);
|
||||
yield exec.exec(`sh -c \\"echo no | avdmanager create avd --force -n test --abi '${target}/${arch}' --package 'system-images;android-${apiLevel};${target};${arch}'"`);
|
||||
}
|
||||
// start emulator
|
||||
console.log('Starting emulator.');
|
||||
// turn off hardware acceleration on Linux
|
||||
if (process.platform === 'linux') {
|
||||
emulatorOptions += ' -accel off';
|
||||
}
|
||||
yield exec.exec(`sh -c \\"${process.env.ANDROID_HOME}/emulator/emulator -avd test ${emulatorOptions} &"`, [], {
|
||||
listeners: {
|
||||
stderr: (data) => {
|
||||
@@ -46,13 +48,13 @@ function launchEmulator(apiLevel, target, arch, profile, emulatorOptions, disabl
|
||||
});
|
||||
// wait for emulator to complete booting
|
||||
yield waitForDevice();
|
||||
yield exec.exec(`${ADB_PATH} shell input keyevent 82`);
|
||||
yield exec.exec(`adb shell input keyevent 82`);
|
||||
// disable animations
|
||||
if (disableAnimations) {
|
||||
console.log('Disabling animations.');
|
||||
yield exec.exec(`${ADB_PATH} shell settings put global window_animation_scale 0.0`);
|
||||
yield exec.exec(`${ADB_PATH} shell settings put global transition_animation_scale 0.0`);
|
||||
yield exec.exec(`${ADB_PATH} shell settings put global animator_duration_scale 0.0`);
|
||||
yield exec.exec(`adb shell settings put global window_animation_scale 0.0`);
|
||||
yield exec.exec(`adb shell settings put global transition_animation_scale 0.0`);
|
||||
yield exec.exec(`adb shell settings put global animator_duration_scale 0.0`);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -63,7 +65,7 @@ exports.launchEmulator = launchEmulator;
|
||||
function killEmulator() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
yield exec.exec(`${ADB_PATH} -s emulator-5554 emu kill`);
|
||||
yield exec.exec(`adb -s emulator-5554 emu kill`);
|
||||
}
|
||||
catch (error) {
|
||||
console.log(error.message);
|
||||
@@ -83,7 +85,7 @@ function waitForDevice() {
|
||||
while (!booted) {
|
||||
try {
|
||||
let result = '';
|
||||
yield exec.exec(`${ADB_PATH} shell getprop sys.boot_completed`, [], {
|
||||
yield exec.exec(`adb shell getprop sys.boot_completed`, [], {
|
||||
listeners: {
|
||||
stdout: (data) => {
|
||||
result += data.toString();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.MIN_API_LEVEL = 21;
|
||||
exports.MIN_API_LEVEL = 15;
|
||||
exports.VALID_TARGETS = ['default', 'google_apis'];
|
||||
exports.VALID_ARCHS = ['x86', 'x86_64'];
|
||||
function checkApiLevel(apiLevel) {
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
||||
result["default"] = mod;
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const exec = __importStar(require("@actions/exec"));
|
||||
/**
|
||||
* Returns the current $JAVA_HOME path.
|
||||
*/
|
||||
function getCurrentJavaHome() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
let defaultJavaHome = '';
|
||||
yield exec.exec(`sh -c \\"echo $JAVA_HOME"`, [], {
|
||||
listeners: {
|
||||
stdout: (data) => {
|
||||
defaultJavaHome += data.toString();
|
||||
}
|
||||
}
|
||||
});
|
||||
return defaultJavaHome.trim();
|
||||
});
|
||||
}
|
||||
exports.getCurrentJavaHome = getCurrentJavaHome;
|
||||
/**
|
||||
* Returns the Java 8 $JAVA_HOME path.
|
||||
*/
|
||||
function getJavaHomeV8() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
let javaHomeV8 = '';
|
||||
yield exec.exec(`/usr/libexec/java_home -v 1.8`, [], {
|
||||
listeners: {
|
||||
stdout: (data) => {
|
||||
javaHomeV8 += data.toString();
|
||||
}
|
||||
}
|
||||
});
|
||||
return javaHomeV8.trim();
|
||||
});
|
||||
}
|
||||
exports.getJavaHomeV8 = getJavaHomeV8;
|
||||
/**
|
||||
* Sets $JAVA_HOME to the specified path.
|
||||
*/
|
||||
function setJavaHome(path) {
|
||||
core.exportVariable('JAVA_HOME', path);
|
||||
}
|
||||
exports.setJavaHome = setJavaHome;
|
||||
+9
-15
@@ -21,13 +21,17 @@ const input_validator_1 = require("./input-validator");
|
||||
const emulator_manager_1 = require("./emulator-manager");
|
||||
const exec = __importStar(require("@actions/exec"));
|
||||
const script_parser_1 = require("./script-parser");
|
||||
const java_version_manager_1 = require("./java-version-manager");
|
||||
function run() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
// only support running on macOS
|
||||
// only support running on macOS or Linux
|
||||
if (process.platform !== 'darwin') {
|
||||
throw new Error('This action is expected to be run within a macOS virtual machine to enable hardware acceleration.');
|
||||
if (process.platform === 'linux') {
|
||||
console.warn(`You're running a Linux VM where hardware acceleration is not available. Please consider using a macOS VM instead to take advantage of native hardware acceleration support provided by HAXM.`);
|
||||
}
|
||||
else {
|
||||
throw new Error('Unsupported virtual machine: please use either macos or ubuntu VM.');
|
||||
}
|
||||
}
|
||||
// API level of the platform and system image
|
||||
const apiLevelInput = core.getInput('api-level', { required: true });
|
||||
@@ -73,20 +77,10 @@ function run() {
|
||||
scripts.forEach((script) => __awaiter(this, void 0, void 0, function* () {
|
||||
console.log(`${script}`);
|
||||
}));
|
||||
// use Java 8 for sdkmanager and avdmanager
|
||||
const defaultJavaHome = yield java_version_manager_1.getCurrentJavaHome();
|
||||
java_version_manager_1.setJavaHome(yield java_version_manager_1.getJavaHomeV8());
|
||||
// install SDK
|
||||
yield sdk_installer_1.installAndroidSdk(apiLevel, target, arch, emulatorBuild);
|
||||
try {
|
||||
// launch an emulator
|
||||
yield emulator_manager_1.launchEmulator(apiLevel, target, arch, profile, emulatorOptions, disableAnimations);
|
||||
}
|
||||
catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
// use default JAVA_HOME for running custom script
|
||||
java_version_manager_1.setJavaHome(defaultJavaHome);
|
||||
// launch an emulator
|
||||
yield emulator_manager_1.launchEmulator(apiLevel, target, arch, profile, emulatorOptions, disableAnimations);
|
||||
// execute the custom script
|
||||
try {
|
||||
// move to custom working directory if set
|
||||
|
||||
+20
-8
@@ -15,30 +15,42 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const exec = __importStar(require("@actions/exec"));
|
||||
const BUILD_TOOLS_VERSION = '29.0.3';
|
||||
const CMDLINE_TOOLS_URL_MAC = 'https://dl.google.com/android/repository/commandlinetools-linux-6200805_latest.zip';
|
||||
const CMDLINE_TOOLS_URL_LINUX = 'https://dl.google.com/android/repository/commandlinetools-linux-6200805_latest.zip';
|
||||
/**
|
||||
* Installs & updates the Android SDK for the macOS platform, including SDK platform for the chosen API level, latest build tools, platform tools, Android Emulator,
|
||||
* and the system image for the chosen API level, CPU arch, and target.
|
||||
*/
|
||||
function installAndroidSdk(apiLevel, target, arch, emulatorBuild) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const sdkmanagerPath = `${process.env.ANDROID_HOME}/tools/bin/sdkmanager`;
|
||||
const isOnMac = process.platform === 'darwin';
|
||||
console.log('Installing new cmdline-tools.');
|
||||
const sdkUrl = isOnMac ? CMDLINE_TOOLS_URL_MAC : CMDLINE_TOOLS_URL_LINUX;
|
||||
yield exec.exec(`sudo mkdir ${process.env.ANDROID_HOME}/cmdline-tools`);
|
||||
yield exec.exec(`curl -fo commandlinetools.zip ${sdkUrl}`);
|
||||
yield exec.exec(`sudo unzip -q commandlinetools.zip -d ${process.env.ANDROID_HOME}/cmdline-tools`);
|
||||
yield exec.exec(`sudo rm -f commandlinetools.zip`);
|
||||
// add paths for commandline-tools and platform-tools
|
||||
core.addPath(`${process.env.ANDROID_HOME}/cmdline-tools/tools:${process.env.ANDROID_HOME}/cmdline-tools/tools/bin:${process.env.ANDROID_HOME}/platform-tools`);
|
||||
yield exec.exec(`sh -c \\"sudo chmod -R 777 ${process.env.ANDROID_HOME}"`);
|
||||
console.log('Installing latest build tools, platform tools, and platform.');
|
||||
yield exec.exec(`sh -c \\"${sdkmanagerPath} --install 'build-tools;${BUILD_TOOLS_VERSION}' platform-tools 'platforms;android-${apiLevel}' > /dev/null"`);
|
||||
yield exec.exec(`sh -c \\"sdkmanager --install 'build-tools;${BUILD_TOOLS_VERSION}' platform-tools 'platforms;android-${apiLevel}' > /dev/null"`);
|
||||
if (emulatorBuild) {
|
||||
console.log(`Installing emulator build ${emulatorBuild}.`);
|
||||
yield exec.exec(`curl -fo emulator.zip https://dl.google.com/android/repository/emulator-darwin-${emulatorBuild}.zip`);
|
||||
yield exec.exec(`rm -rf ${process.env.ANDROID_HOME}/emulator`);
|
||||
yield exec.exec(`unzip -q emulator.zip -d ${process.env.ANDROID_HOME}`);
|
||||
yield exec.exec(`rm -f emulator.zip`);
|
||||
yield exec.exec(`curl -fo emulator.zip https://dl.google.com/android/repository/emulator-${isOnMac ? 'darwin' : 'linux'}-${emulatorBuild}.zip`);
|
||||
yield exec.exec(`sudo rm -rf ${process.env.ANDROID_HOME}/emulator`);
|
||||
yield exec.exec(`sudo unzip -q emulator.zip -d ${process.env.ANDROID_HOME}`);
|
||||
yield exec.exec(`sudo rm -f emulator.zip`);
|
||||
}
|
||||
else {
|
||||
console.log('Installing latest emulator.');
|
||||
yield exec.exec(`sh -c \\"${sdkmanagerPath} --install emulator > /dev/null"`);
|
||||
yield exec.exec(`sh -c \\"sdkmanager --install emulator > /dev/null"`);
|
||||
}
|
||||
console.log('Installing system images.');
|
||||
yield exec.exec(`sh -c \\"${sdkmanagerPath} --install 'system-images;android-${apiLevel};${target};${arch}' > /dev/null"`);
|
||||
yield exec.exec(`sh -c \\"sdkmanager --install 'system-images;android-${apiLevel};${target};${arch}' > /dev/null"`);
|
||||
});
|
||||
}
|
||||
exports.installAndroidSdk = installAndroidSdk;
|
||||
|
||||
Generated
+2742
-2007
File diff suppressed because it is too large
Load Diff
+4
-3
@@ -23,7 +23,8 @@
|
||||
"dependencies": {
|
||||
"@actions/core": "^1.0.0",
|
||||
"@actions/exec": "^1.0.1",
|
||||
"@actions/tool-cache": "^1.1.2"
|
||||
"@actions/tool-cache": "^1.1.2",
|
||||
"minimist": "^1.2.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^24.0.13",
|
||||
@@ -34,8 +35,8 @@
|
||||
"eslint-config-prettier": "^6.5.0",
|
||||
"eslint-plugin-import": "^2.18.2",
|
||||
"eslint-plugin-prettier": "^3.1.1",
|
||||
"jest": "^24.8.0",
|
||||
"jest-circus": "^24.7.1",
|
||||
"jest": "^25.2.3",
|
||||
"jest-circus": "^25.2.3",
|
||||
"prettier": "^1.18.2",
|
||||
"ts-jest": "^24.0.2",
|
||||
"typescript": "^3.5.1"
|
||||
|
||||
+14
-10
@@ -1,8 +1,6 @@
|
||||
import * as exec from '@actions/exec';
|
||||
|
||||
const EMULATOR_BOOT_TIMEOUT_SECONDS = 300;
|
||||
const AVD_MANAGER_PATH = `${process.env.ANDROID_HOME}/tools/bin/avdmanager`;
|
||||
const ADB_PATH = `${process.env.ANDROID_HOME}/platform-tools/adb`;
|
||||
|
||||
/**
|
||||
* Creates and launches a new AVD instance with the specified configurations.
|
||||
@@ -11,14 +9,20 @@ export async function launchEmulator(apiLevel: number, target: string, arch: str
|
||||
// create a new AVD
|
||||
if (profile.trim() !== '') {
|
||||
console.log(`Creating AVD with custom profile ${profile}`);
|
||||
await exec.exec(`${AVD_MANAGER_PATH} create avd --force -n test --abi "${target}/${arch}" --package "system-images;android-${apiLevel};${target};${arch}" --device "${profile}"`);
|
||||
await exec.exec(`avdmanager create avd --force -n test --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 | ${AVD_MANAGER_PATH} create avd --force -n test --abi '${target}/${arch}' --package 'system-images;android-${apiLevel};${target};${arch}'"`);
|
||||
await exec.exec(`sh -c \\"echo no | avdmanager create avd --force -n test --abi '${target}/${arch}' --package 'system-images;android-${apiLevel};${target};${arch}'"`);
|
||||
}
|
||||
|
||||
// start emulator
|
||||
console.log('Starting emulator.');
|
||||
|
||||
// turn off hardware acceleration on Linux
|
||||
if (process.platform === 'linux') {
|
||||
emulatorOptions += ' -accel off';
|
||||
}
|
||||
|
||||
await exec.exec(`sh -c \\"${process.env.ANDROID_HOME}/emulator/emulator -avd test ${emulatorOptions} &"`, [], {
|
||||
listeners: {
|
||||
stderr: (data: Buffer) => {
|
||||
@@ -31,14 +35,14 @@ export async function launchEmulator(apiLevel: number, target: string, arch: str
|
||||
|
||||
// wait for emulator to complete booting
|
||||
await waitForDevice();
|
||||
await exec.exec(`${ADB_PATH} shell input keyevent 82`);
|
||||
await exec.exec(`adb shell input keyevent 82`);
|
||||
|
||||
// disable animations
|
||||
if (disableAnimations) {
|
||||
console.log('Disabling animations.');
|
||||
await exec.exec(`${ADB_PATH} shell settings put global window_animation_scale 0.0`);
|
||||
await exec.exec(`${ADB_PATH} shell settings put global transition_animation_scale 0.0`);
|
||||
await exec.exec(`${ADB_PATH} shell settings put global animator_duration_scale 0.0`);
|
||||
await exec.exec(`adb shell settings put global window_animation_scale 0.0`);
|
||||
await exec.exec(`adb shell settings put global transition_animation_scale 0.0`);
|
||||
await exec.exec(`adb shell settings put global animator_duration_scale 0.0`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +51,7 @@ export async function launchEmulator(apiLevel: number, target: string, arch: str
|
||||
*/
|
||||
export async function killEmulator(): Promise<void> {
|
||||
try {
|
||||
await exec.exec(`${ADB_PATH} -s emulator-5554 emu kill`);
|
||||
await exec.exec(`adb -s emulator-5554 emu kill`);
|
||||
} catch (error) {
|
||||
console.log(error.message);
|
||||
}
|
||||
@@ -64,7 +68,7 @@ async function waitForDevice(): Promise<void> {
|
||||
while (!booted) {
|
||||
try {
|
||||
let result = '';
|
||||
await exec.exec(`${ADB_PATH} shell getprop sys.boot_completed`, [], {
|
||||
await exec.exec(`adb shell getprop sys.boot_completed`, [], {
|
||||
listeners: {
|
||||
stdout: (data: Buffer) => {
|
||||
result += data.toString();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export const MIN_API_LEVEL = 21;
|
||||
export const MIN_API_LEVEL = 15;
|
||||
export const VALID_TARGETS: Array<string> = ['default', 'google_apis'];
|
||||
export const VALID_ARCHS: Array<string> = ['x86', 'x86_64'];
|
||||
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
|
||||
/**
|
||||
* Returns the current $JAVA_HOME path.
|
||||
*/
|
||||
export async function getCurrentJavaHome(): Promise<string> {
|
||||
let defaultJavaHome = '';
|
||||
await exec.exec(`sh -c \\"echo $JAVA_HOME"`, [], {
|
||||
listeners: {
|
||||
stdout: (data: Buffer) => {
|
||||
defaultJavaHome += data.toString();
|
||||
}
|
||||
}
|
||||
});
|
||||
return defaultJavaHome.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Java 8 $JAVA_HOME path.
|
||||
*/
|
||||
export async function getJavaHomeV8(): Promise<string> {
|
||||
let javaHomeV8 = '';
|
||||
await exec.exec(`/usr/libexec/java_home -v 1.8`, [], {
|
||||
listeners: {
|
||||
stdout: (data: Buffer) => {
|
||||
javaHomeV8 += data.toString();
|
||||
}
|
||||
}
|
||||
});
|
||||
return javaHomeV8.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets $JAVA_HOME to the specified path.
|
||||
*/
|
||||
export function setJavaHome(path: string) {
|
||||
core.exportVariable('JAVA_HOME', path);
|
||||
}
|
||||
+10
-16
@@ -4,13 +4,18 @@ import { checkApiLevel, checkTarget, checkArch, checkDisableAnimations, checkEmu
|
||||
import { launchEmulator, killEmulator } from './emulator-manager';
|
||||
import * as exec from '@actions/exec';
|
||||
import { parseScript } from './script-parser';
|
||||
import { getCurrentJavaHome, getJavaHomeV8, setJavaHome } from './java-version-manager';
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
// only support running on macOS
|
||||
// only support running on macOS or Linux
|
||||
if (process.platform !== 'darwin') {
|
||||
throw new Error('This action is expected to be run within a macOS virtual machine to enable hardware acceleration.');
|
||||
if (process.platform === 'linux') {
|
||||
console.warn(
|
||||
`You're running a Linux VM where hardware acceleration is not available. Please consider using a macOS VM instead to take advantage of native hardware acceleration support provided by HAXM.`
|
||||
);
|
||||
} else {
|
||||
throw new Error('Unsupported virtual machine: please use either macos or ubuntu VM.');
|
||||
}
|
||||
}
|
||||
|
||||
// API level of the platform and system image
|
||||
@@ -66,22 +71,11 @@ async function run() {
|
||||
console.log(`${script}`);
|
||||
});
|
||||
|
||||
// use Java 8 for sdkmanager and avdmanager
|
||||
const defaultJavaHome = await getCurrentJavaHome();
|
||||
setJavaHome(await getJavaHomeV8());
|
||||
|
||||
// install SDK
|
||||
await installAndroidSdk(apiLevel, target, arch, emulatorBuild);
|
||||
|
||||
try {
|
||||
// launch an emulator
|
||||
await launchEmulator(apiLevel, target, arch, profile, emulatorOptions, disableAnimations);
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
|
||||
// use default JAVA_HOME for running custom script
|
||||
setJavaHome(defaultJavaHome);
|
||||
// launch an emulator
|
||||
await launchEmulator(apiLevel, target, arch, profile, emulatorOptions, disableAnimations);
|
||||
|
||||
// execute the custom script
|
||||
try {
|
||||
|
||||
+23
-8
@@ -1,25 +1,40 @@
|
||||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
|
||||
const BUILD_TOOLS_VERSION = '29.0.3';
|
||||
const CMDLINE_TOOLS_URL_MAC = 'https://dl.google.com/android/repository/commandlinetools-linux-6200805_latest.zip';
|
||||
const CMDLINE_TOOLS_URL_LINUX = 'https://dl.google.com/android/repository/commandlinetools-linux-6200805_latest.zip';
|
||||
|
||||
/**
|
||||
* Installs & updates the Android SDK for the macOS platform, including SDK platform for the chosen API level, latest build tools, platform tools, Android Emulator,
|
||||
* and the system image for the chosen API level, CPU arch, and target.
|
||||
*/
|
||||
export async function installAndroidSdk(apiLevel: number, target: string, arch: string, emulatorBuild?: string): Promise<void> {
|
||||
const sdkmanagerPath = `${process.env.ANDROID_HOME}/tools/bin/sdkmanager`;
|
||||
const isOnMac = process.platform === 'darwin';
|
||||
console.log('Installing new cmdline-tools.');
|
||||
const sdkUrl = isOnMac ? CMDLINE_TOOLS_URL_MAC : CMDLINE_TOOLS_URL_LINUX;
|
||||
await exec.exec(`sudo mkdir ${process.env.ANDROID_HOME}/cmdline-tools`);
|
||||
await exec.exec(`curl -fo commandlinetools.zip ${sdkUrl}`);
|
||||
await exec.exec(`sudo unzip -q commandlinetools.zip -d ${process.env.ANDROID_HOME}/cmdline-tools`);
|
||||
await exec.exec(`sudo rm -f commandlinetools.zip`);
|
||||
|
||||
// add paths for commandline-tools and platform-tools
|
||||
core.addPath(`${process.env.ANDROID_HOME}/cmdline-tools/tools:${process.env.ANDROID_HOME}/cmdline-tools/tools/bin:${process.env.ANDROID_HOME}/platform-tools`);
|
||||
await exec.exec(`sh -c \\"sudo chmod -R 777 ${process.env.ANDROID_HOME}"`);
|
||||
|
||||
console.log('Installing latest build tools, platform tools, and platform.');
|
||||
await exec.exec(`sh -c \\"${sdkmanagerPath} --install 'build-tools;${BUILD_TOOLS_VERSION}' platform-tools 'platforms;android-${apiLevel}' > /dev/null"`);
|
||||
|
||||
await exec.exec(`sh -c \\"sdkmanager --install 'build-tools;${BUILD_TOOLS_VERSION}' platform-tools 'platforms;android-${apiLevel}' > /dev/null"`);
|
||||
if (emulatorBuild) {
|
||||
console.log(`Installing emulator build ${emulatorBuild}.`);
|
||||
await exec.exec(`curl -fo emulator.zip https://dl.google.com/android/repository/emulator-darwin-${emulatorBuild}.zip`);
|
||||
await exec.exec(`rm -rf ${process.env.ANDROID_HOME}/emulator`);
|
||||
await exec.exec(`unzip -q emulator.zip -d ${process.env.ANDROID_HOME}`);
|
||||
await exec.exec(`rm -f emulator.zip`);
|
||||
await exec.exec(`curl -fo emulator.zip https://dl.google.com/android/repository/emulator-${isOnMac ? 'darwin' : 'linux'}-${emulatorBuild}.zip`);
|
||||
await exec.exec(`sudo rm -rf ${process.env.ANDROID_HOME}/emulator`);
|
||||
await exec.exec(`sudo unzip -q emulator.zip -d ${process.env.ANDROID_HOME}`);
|
||||
await exec.exec(`sudo rm -f emulator.zip`);
|
||||
} else {
|
||||
console.log('Installing latest emulator.');
|
||||
await exec.exec(`sh -c \\"${sdkmanagerPath} --install emulator > /dev/null"`);
|
||||
await exec.exec(`sh -c \\"sdkmanager --install emulator > /dev/null"`);
|
||||
}
|
||||
console.log('Installing system images.');
|
||||
await exec.exec(`sh -c \\"${sdkmanagerPath} --install 'system-images;android-${apiLevel};${target};${arch}' > /dev/null"`);
|
||||
await exec.exec(`sh -c \\"sdkmanager --install 'system-images;android-${apiLevel};${target};${arch}' > /dev/null"`);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ android {
|
||||
|
||||
defaultConfig {
|
||||
applicationId "com.example.testapp"
|
||||
minSdkVersion 21
|
||||
minSdkVersion 15
|
||||
targetSdkVersion 29
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
@@ -28,9 +28,9 @@ dependencies {
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||
implementation 'androidx.appcompat:appcompat:1.1.0'
|
||||
implementation 'androidx.core:core-ktx:1.1.0'
|
||||
implementation 'androidx.core:core-ktx:1.2.0'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
|
||||
testImplementation 'junit:junit:4.12'
|
||||
testImplementation 'junit:junit:4.13'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
|
||||
buildscript {
|
||||
ext.kotlin_version = '1.3.61'
|
||||
ext.kotlin_version = '1.3.70'
|
||||
repositories {
|
||||
google()
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:3.5.3'
|
||||
classpath 'com.android.tools.build:gradle:3.6.0'
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
|
||||
+1
-1
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
|
||||
|
||||
Reference in New Issue
Block a user