1import assert from 'assert'; 2import fs from 'fs'; 3import os from 'os'; 4import path from 'path'; 5 6/** 7 * The default Android SDK locations per platform. 8 * @see https://developer.android.com/studio/run/emulator-commandline#filedir 9 * @see https://developer.android.com/studio/intro/studio-config#optimize-studio-windows 10 */ 11const ANDROID_DEFAULT_LOCATION: Readonly<Partial<Record<NodeJS.Platform, string>>> = { 12 darwin: path.join(os.homedir(), 'Library', 'Android', 'sdk'), 13 linux: path.join(os.homedir(), 'Android', 'sdk'), 14 win32: path.join(os.homedir(), 'AppData', 'Local', 'Android', 'Sdk'), 15}; 16 17/** 18 * Resolve and validate the root folder where the Android SDK has been installed. 19 * This checks both `ANDROID_HOME`, `ANDROID_SDK_ROOT`, and the default path for the current platform. 20 * @see https://developer.android.com/studio/command-line/variables 21 */ 22export function assertSdkRoot() { 23 if (process.env.ANDROID_HOME) { 24 assert( 25 fs.existsSync(process.env.ANDROID_HOME), 26 `Failed to resolve the Android SDK path. ANDROID_HOME is set to a non-existing path: ${process.env.ANDROID_HOME}` 27 ); 28 return process.env.ANDROID_HOME; 29 } 30 31 if (process.env.ANDROID_SDK_ROOT) { 32 assert( 33 fs.existsSync(process.env.ANDROID_SDK_ROOT), 34 `Failed to resolve the Android SDK path. Deprecated ANDROID_SDK_ROOT is set to a non-existing path: ${process.env.ANDROID_SDK_ROOT}. Use ANDROID_HOME instead.` 35 ); 36 return process.env.ANDROID_SDK_ROOT; 37 } 38 39 const defaultLocation = ANDROID_DEFAULT_LOCATION[process.platform]; 40 if (defaultLocation) { 41 assert( 42 fs.existsSync(defaultLocation), 43 `Failed to resolve the Android SDK path. Default install location not found: ${defaultLocation}. Use ANDROID_HOME to set the Android SDK location.` 44 ); 45 return defaultLocation; 46 } 47 48 return null; 49} 50