Skip to content

Commit d597ec0

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 d597ec0

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/sdk-installer.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,41 @@ 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

40+
const sdkManager = `${cmdlineToolsPath}/latest/bin/sdkmanager`;
41+
3542
// add paths for commandline-tools and platform-tools
3643
core.addPath(`${cmdlineToolsPath}/latest:${cmdlineToolsPath}/latest/bin:${process.env.ANDROID_HOME}/platform-tools`);
3744

3845
// set standard AVD path
3946
core.exportVariable('ANDROID_AVD_HOME', `${process.env.HOME}/.android/avd`);
4047

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

4451
console.log('Installing latest build tools, platform tools, and platform.');
4552

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

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

5158
if (emulatorBuild) {
5259
console.log(`Installing emulator build ${emulatorBuild}.`);
@@ -65,19 +72,19 @@ export async function installAndroidSdk(apiLevel: string, target: string, arch:
6572
downloadUrlSuffix = `-${emulatorBuild}`;
6673
}
6774
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}`);
75+
await exec.exec(`${sudoCmd} unzip -o -q emulator.zip -d ${process.env.ANDROID_HOME}`);
6976
await io.rmRF('emulator.zip');
7077
}
7178
console.log('Installing system images.');
72-
await exec.exec(`sh -c \\"sdkmanager --install 'system-images;android-${apiLevel};${target};${arch}' --channel=${channelId} > /dev/null"`);
79+
await exec.exec(`sh -c \\"${sudoCmd} ${sdkManager} --install 'system-images;android-${apiLevel};${target};${arch}' --channel=${channelId} > /dev/null"`);
7380

7481
if (ndkVersion) {
7582
console.log(`Installing NDK ${ndkVersion}.`);
76-
await exec.exec(`sh -c \\"sdkmanager --install 'ndk;${ndkVersion}' --channel=${channelId} > /dev/null"`);
83+
await exec.exec(`sh -c \\"${sudoCmd} ${sdkManager} --install 'ndk;${ndkVersion}' --channel=${channelId} > /dev/null"`);
7784
}
7885
if (cmakeVersion) {
7986
console.log(`Installing CMake ${cmakeVersion}.`);
80-
await exec.exec(`sh -c \\"sdkmanager --install 'cmake;${cmakeVersion}' --channel=${channelId} > /dev/null"`);
87+
await exec.exec(`sh -c \\"${sudoCmd} ${sdkManager} --install 'cmake;${cmakeVersion}' --channel=${channelId} > /dev/null"`);
8188
}
8289
} finally {
8390
console.log(`::endgroup::`);

0 commit comments

Comments
 (0)