Skip to content

Commit e994454

Browse files
committed
Avoid sudo chown -R on ${ANDROID_HOME} on Linux
This seems to take several minutes, a decent fraction of the overall time in my simple use. Instead of changing the ownership of `${ANDROID_HOME}` instead arrange to run individual commands under `sudo` when needed. Note that `$PATH` is not preserved by `sudo` so we must use the full path to the `sdkmanager`. Another wrinkle is the cmdline-tools installation, since `tc.extractZip` and `io.mv` do not include sudo-ish functionality. Instead precreate the target directory with the ownership to allow the unpack as the current user.
1 parent 2cfd145 commit e994454

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/sdk-installer.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,42 @@ export async function installAndroidSdk(apiLevel: string, target: string, arch:
1919
const isOnMac = process.platform === 'darwin';
2020
const isArm = process.arch === 'arm64';
2121

22+
var sudoCmd: string = '';
2223
if (!isOnMac) {
23-
await exec.exec(`sh -c \\"sudo chown $USER:$USER ${process.env.ANDROID_HOME} -R`);
24+
sudoCmd = 'sudo';
2425
}
2526

2627
const cmdlineToolsPath = `${process.env.ANDROID_HOME}/cmdline-tools`;
2728
if (!fs.existsSync(cmdlineToolsPath)) {
2829
console.log('Installing new cmdline-tools.');
2930
const sdkUrl = isOnMac ? CMDLINE_TOOLS_URL_MAC : CMDLINE_TOOLS_URL_LINUX;
3031
const downloadPath = await tc.downloadTool(sdkUrl);
32+
if (sudoCmd != '') {
33+
await exec.exec(`sh -c \\"${sudoCmd} mkdir ${cmdlineToolsPath}"`);
34+
await exec.exec(`sh -c \\"${sudoCmd} chown $USER:$USER ${cmdlineToolsPath}"`);
35+
}
3136
await tc.extractZip(downloadPath, cmdlineToolsPath);
3237
await io.mv(`${cmdlineToolsPath}/cmdline-tools`, `${cmdlineToolsPath}/latest`);
3338
}
3439

3540
// add paths for commandline-tools and platform-tools
3641
core.addPath(`${cmdlineToolsPath}/latest:${cmdlineToolsPath}/latest/bin:${process.env.ANDROID_HOME}/platform-tools`);
3742

43+
// find sdkmanager
44+
const sdkManager: string = await io.which('sdkmanager', true);
45+
3846
// set standard AVD path
3947
core.exportVariable('ANDROID_AVD_HOME', `${process.env.HOME}/.android/avd`);
4048

4149
// accept all Android SDK licenses
42-
await exec.exec(`sh -c \\"yes | sdkmanager --licenses > /dev/null"`);
50+
await exec.exec(`sh -c \\"yes | ${sdkManager} --licenses > /dev/null"`);
4351

4452
console.log('Installing latest build tools, platform tools, and platform.');
4553

46-
await exec.exec(`sh -c \\"sdkmanager --install 'build-tools;${BUILD_TOOLS_VERSION}' platform-tools > /dev/null"`);
54+
await exec.exec(`sh -c \\"${sudoCmd} ${sdkManager} --install 'build-tools;${BUILD_TOOLS_VERSION}' platform-tools > /dev/null"`);
4755

4856
console.log('Installing latest emulator.');
49-
await exec.exec(`sh -c \\"sdkmanager --install emulator --channel=${channelId} > /dev/null"`);
57+
await exec.exec(`sh -c \\"${sudoCmd} ${sdkManager} --install emulator --channel=${channelId} > /dev/null"`);
5058

5159
if (emulatorBuild) {
5260
console.log(`Installing emulator build ${emulatorBuild}.`);
@@ -65,19 +73,19 @@ export async function installAndroidSdk(apiLevel: string, target: string, arch:
6573
downloadUrlSuffix = `-${emulatorBuild}`;
6674
}
6775
await exec.exec(`curl -fo emulator.zip https://dl.google.com/android/repository/emulator-${isOnMac ? 'darwin' : 'linux'}${downloadUrlSuffix}.zip`);
68-
await exec.exec(`unzip -o -q emulator.zip -d ${process.env.ANDROID_HOME}`);
76+
await exec.exec(`${sudoCmd} unzip -o -q emulator.zip -d ${process.env.ANDROID_HOME}`);
6977
await io.rmRF('emulator.zip');
7078
}
7179
console.log('Installing system images.');
72-
await exec.exec(`sh -c \\"sdkmanager --install 'system-images;android-${apiLevel};${target};${arch}' --channel=${channelId} > /dev/null"`);
80+
await exec.exec(`sh -c \\"${sudoCmd} ${sdkManager} --install 'system-images;android-${apiLevel};${target};${arch}' --channel=${channelId} > /dev/null"`);
7381

7482
if (ndkVersion) {
7583
console.log(`Installing NDK ${ndkVersion}.`);
76-
await exec.exec(`sh -c \\"sdkmanager --install 'ndk;${ndkVersion}' --channel=${channelId} > /dev/null"`);
84+
await exec.exec(`sh -c \\"${sudoCmd} ${sdkManager} --install 'ndk;${ndkVersion}' --channel=${channelId} > /dev/null"`);
7785
}
7886
if (cmakeVersion) {
7987
console.log(`Installing CMake ${cmakeVersion}.`);
80-
await exec.exec(`sh -c \\"sdkmanager --install 'cmake;${cmakeVersion}' --channel=${channelId} > /dev/null"`);
88+
await exec.exec(`sh -c \\"${sudoCmd} ${sdkManager} --install 'cmake;${cmakeVersion}' --channel=${channelId} > /dev/null"`);
8189
}
8290
} finally {
8391
console.log(`::endgroup::`);

0 commit comments

Comments
 (0)