Use new cmdline-tools with support for running sdkmanager and avdmanager with Java 8+.

This commit is contained in:
Yang Chen
2020-03-05 14:05:05 +11:00
committed by Yang
parent e1015a487b
commit 4a949058e2
9 changed files with 43 additions and 141 deletions
+1
View File
@@ -45,5 +45,6 @@ jobs:
working-directory: ./test-fixture/ working-directory: ./test-fixture/
script: | script: |
echo $GITHUB_REPOSITORY echo $GITHUB_REPOSITORY
adb devices
./gradlew help ./gradlew help
./gradlew connectedDebugAndroidTest ./gradlew connectedDebugAndroidTest
+8 -10
View File
@@ -17,8 +17,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const exec = __importStar(require("@actions/exec")); const exec = __importStar(require("@actions/exec"));
const EMULATOR_BOOT_TIMEOUT_SECONDS = 300; 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. * Creates and launches a new AVD instance with the specified configurations.
*/ */
@@ -27,11 +25,11 @@ function launchEmulator(apiLevel, target, arch, profile, emulatorOptions, disabl
// create a new AVD // create a new AVD
if (profile.trim() !== '') { if (profile.trim() !== '') {
console.log(`Creating AVD with custom profile ${profile}`); 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 { else {
console.log(`Creating AVD without custom profile.`); 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 // start emulator
console.log('Starting emulator.'); console.log('Starting emulator.');
@@ -46,13 +44,13 @@ function launchEmulator(apiLevel, target, arch, profile, emulatorOptions, disabl
}); });
// wait for emulator to complete booting // wait for emulator to complete booting
yield waitForDevice(); yield waitForDevice();
yield exec.exec(`${ADB_PATH} shell input keyevent 82`); yield exec.exec(`adb shell input keyevent 82`);
// disable animations // disable animations
if (disableAnimations) { if (disableAnimations) {
console.log('Disabling animations.'); console.log('Disabling animations.');
yield exec.exec(`${ADB_PATH} shell settings put global window_animation_scale 0.0`); yield exec.exec(`adb 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 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 animator_duration_scale 0.0`);
} }
}); });
} }
@@ -63,7 +61,7 @@ exports.launchEmulator = launchEmulator;
function killEmulator() { function killEmulator() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
yield exec.exec(`${ADB_PATH} -s emulator-5554 emu kill`); yield exec.exec(`adb -s emulator-5554 emu kill`);
} }
catch (error) { catch (error) {
console.log(error.message); console.log(error.message);
@@ -83,7 +81,7 @@ function waitForDevice() {
while (!booted) { while (!booted) {
try { try {
let result = ''; let result = '';
yield exec.exec(`${ADB_PATH} shell getprop sys.boot_completed`, [], { yield exec.exec(`adb shell getprop sys.boot_completed`, [], {
listeners: { listeners: {
stdout: (data) => { stdout: (data) => {
result += data.toString(); result += data.toString();
-60
View File
@@ -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;
-6
View File
@@ -21,7 +21,6 @@ const input_validator_1 = require("./input-validator");
const emulator_manager_1 = require("./emulator-manager"); const emulator_manager_1 = require("./emulator-manager");
const exec = __importStar(require("@actions/exec")); const exec = __importStar(require("@actions/exec"));
const script_parser_1 = require("./script-parser"); const script_parser_1 = require("./script-parser");
const java_version_manager_1 = require("./java-version-manager");
function run() { function run() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
@@ -73,9 +72,6 @@ function run() {
scripts.forEach((script) => __awaiter(this, void 0, void 0, function* () { scripts.forEach((script) => __awaiter(this, void 0, void 0, function* () {
console.log(`${script}`); 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 // install SDK
yield sdk_installer_1.installAndroidSdk(apiLevel, target, arch, emulatorBuild); yield sdk_installer_1.installAndroidSdk(apiLevel, target, arch, emulatorBuild);
try { try {
@@ -85,8 +81,6 @@ function run() {
catch (error) { catch (error) {
core.setFailed(error.message); core.setFailed(error.message);
} }
// use default JAVA_HOME for running custom script
java_version_manager_1.setJavaHome(defaultJavaHome);
// execute the custom script // execute the custom script
try { try {
// move to custom working directory if set // move to custom working directory if set
+12 -4
View File
@@ -15,17 +15,25 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result; return result;
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const exec = __importStar(require("@actions/exec")); const exec = __importStar(require("@actions/exec"));
const BUILD_TOOLS_VERSION = '29.0.3'; const BUILD_TOOLS_VERSION = '29.0.3';
const CMDLINE_TOOLS_URL = '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, * 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. * and the system image for the chosen API level, CPU arch, and target.
*/ */
function installAndroidSdk(apiLevel, target, arch, emulatorBuild) { function installAndroidSdk(apiLevel, target, arch, emulatorBuild) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const sdkmanagerPath = `${process.env.ANDROID_HOME}/tools/bin/sdkmanager`; console.log('Installing new cmdline-tools.');
yield exec.exec(`mkdir ${process.env.ANDROID_HOME}/cmdline-tools`);
yield exec.exec(`curl -fo commandlinetools.zip ${CMDLINE_TOOLS_URL}`);
yield exec.exec(`unzip -q commandlinetools.zip -d ${process.env.ANDROID_HOME}/cmdline-tools`);
yield exec.exec(`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`);
console.log('Installing latest build tools, platform tools, and platform.'); 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) { if (emulatorBuild) {
console.log(`Installing emulator build ${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(`curl -fo emulator.zip https://dl.google.com/android/repository/emulator-darwin-${emulatorBuild}.zip`);
@@ -35,10 +43,10 @@ function installAndroidSdk(apiLevel, target, arch, emulatorBuild) {
} }
else { else {
console.log('Installing latest emulator.'); 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.'); 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; exports.installAndroidSdk = installAndroidSdk;
+8 -10
View File
@@ -1,8 +1,6 @@
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
const EMULATOR_BOOT_TIMEOUT_SECONDS = 300; 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. * Creates and launches a new AVD instance with the specified configurations.
@@ -11,10 +9,10 @@ export async function launchEmulator(apiLevel: number, target: string, arch: str
// create a new AVD // create a new AVD
if (profile.trim() !== '') { if (profile.trim() !== '') {
console.log(`Creating AVD with custom profile ${profile}`); 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 { } else {
console.log(`Creating AVD without custom profile.`); 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 // start emulator
@@ -31,14 +29,14 @@ export async function launchEmulator(apiLevel: number, target: string, arch: str
// wait for emulator to complete booting // wait for emulator to complete booting
await waitForDevice(); await waitForDevice();
await exec.exec(`${ADB_PATH} shell input keyevent 82`); await exec.exec(`adb shell input keyevent 82`);
// disable animations // disable animations
if (disableAnimations) { if (disableAnimations) {
console.log('Disabling animations.'); console.log('Disabling animations.');
await exec.exec(`${ADB_PATH} shell settings put global window_animation_scale 0.0`); await exec.exec(`adb 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 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 animator_duration_scale 0.0`);
} }
} }
@@ -47,7 +45,7 @@ export async function launchEmulator(apiLevel: number, target: string, arch: str
*/ */
export async function killEmulator(): Promise<void> { export async function killEmulator(): Promise<void> {
try { try {
await exec.exec(`${ADB_PATH} -s emulator-5554 emu kill`); await exec.exec(`adb -s emulator-5554 emu kill`);
} catch (error) { } catch (error) {
console.log(error.message); console.log(error.message);
} }
@@ -64,7 +62,7 @@ async function waitForDevice(): Promise<void> {
while (!booted) { while (!booted) {
try { try {
let result = ''; let result = '';
await exec.exec(`${ADB_PATH} shell getprop sys.boot_completed`, [], { await exec.exec(`adb shell getprop sys.boot_completed`, [], {
listeners: { listeners: {
stdout: (data: Buffer) => { stdout: (data: Buffer) => {
result += data.toString(); result += data.toString();
-39
View File
@@ -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);
}
-8
View File
@@ -4,7 +4,6 @@ import { checkApiLevel, checkTarget, checkArch, checkDisableAnimations, checkEmu
import { launchEmulator, killEmulator } from './emulator-manager'; import { launchEmulator, killEmulator } from './emulator-manager';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
import { parseScript } from './script-parser'; import { parseScript } from './script-parser';
import { getCurrentJavaHome, getJavaHomeV8, setJavaHome } from './java-version-manager';
async function run() { async function run() {
try { try {
@@ -66,10 +65,6 @@ async function run() {
console.log(`${script}`); console.log(`${script}`);
}); });
// use Java 8 for sdkmanager and avdmanager
const defaultJavaHome = await getCurrentJavaHome();
setJavaHome(await getJavaHomeV8());
// install SDK // install SDK
await installAndroidSdk(apiLevel, target, arch, emulatorBuild); await installAndroidSdk(apiLevel, target, arch, emulatorBuild);
@@ -80,9 +75,6 @@ async function run() {
core.setFailed(error.message); core.setFailed(error.message);
} }
// use default JAVA_HOME for running custom script
setJavaHome(defaultJavaHome);
// execute the custom script // execute the custom script
try { try {
// move to custom working directory if set // move to custom working directory if set
+14 -4
View File
@@ -1,15 +1,25 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
const BUILD_TOOLS_VERSION = '29.0.3'; const BUILD_TOOLS_VERSION = '29.0.3';
const CMDLINE_TOOLS_URL = '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, * 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. * 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> { export async function installAndroidSdk(apiLevel: number, target: string, arch: string, emulatorBuild?: string): Promise<void> {
const sdkmanagerPath = `${process.env.ANDROID_HOME}/tools/bin/sdkmanager`; console.log('Installing new cmdline-tools.');
await exec.exec(`mkdir ${process.env.ANDROID_HOME}/cmdline-tools`);
await exec.exec(`curl -fo commandlinetools.zip ${CMDLINE_TOOLS_URL}`);
await exec.exec(`unzip -q commandlinetools.zip -d ${process.env.ANDROID_HOME}/cmdline-tools`);
await exec.exec(`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`);
console.log('Installing latest build tools, platform tools, and platform.'); 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) { if (emulatorBuild) {
console.log(`Installing emulator build ${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(`curl -fo emulator.zip https://dl.google.com/android/repository/emulator-darwin-${emulatorBuild}.zip`);
@@ -18,8 +28,8 @@ export async function installAndroidSdk(apiLevel: number, target: string, arch:
await exec.exec(`rm -f emulator.zip`); await exec.exec(`rm -f emulator.zip`);
} else { } else {
console.log('Installing latest emulator.'); 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.'); 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"`);
} }