Skip to content

Commit 1e90478

Browse files
committed
feat: support ANDROID_SDK_ROOT according to android docs
1 parent e44ca94 commit 1e90478

File tree

3 files changed

+63
-19
lines changed

3 files changed

+63
-19
lines changed

packages/doctor/src/android-tools-info.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,21 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
4747
private static MAX_JAVA_VERSION = null as string;
4848

4949
private toolsInfo: NativeScriptDoctor.IAndroidToolsInfoData;
50+
51+
/**
52+
* Excerpt from: https://developer.android.com/studio/command-line/variables#envar
53+
*
54+
* ANDROID_SDK_ROOT - Sets the path to the SDK installation directory.
55+
* Once set, the value does not typically change, and can be shared by multiple users on the same machine.
56+
* ANDROID_HOME, which also points to the SDK installation directory, is deprecated.
57+
* If you continue to use it, the following rules apply:
58+
*
59+
* - If ANDROID_HOME is defined and contains a valid SDK installation, its value is used instead of the value in ANDROID_SDK_ROOT.
60+
* - If ANDROID_HOME is not defined, the value in ANDROID_SDK_ROOT is used.
61+
* - If ANDROID_HOME is defined but does not exist or does not contain a valid SDK installation, the value in ANDROID_SDK_ROOT is used instead.
62+
*/
5063
public get androidHome(): string {
51-
return process.env["ANDROID_HOME"];
64+
return process.env["ANDROID_SDK_ROOT"] ?? process.env["ANDROID_HOME"];
5265
}
5366
private pathToEmulatorExecutable: string;
5467

@@ -120,7 +133,7 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
120133
let invalidBuildToolsAdditionalMsg = `Run \`\$ ${this.getPathToSdkManagementTool()}\` from your command-line to install required \`Android Build Tools\`.`;
121134
if (!isAndroidHomeValid) {
122135
invalidBuildToolsAdditionalMsg +=
123-
" In case you already have them installed, make sure `ANDROID_HOME` environment variable is set correctly.";
136+
" In case you already have them installed, make sure `ANDROID_SDK_ROOT` environment variable is set correctly.";
124137
}
125138

126139
errors.push({
@@ -250,7 +263,23 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
250263
return null;
251264
}
252265

253-
public validateAndroidHomeEnvVariable(): NativeScriptDoctor.IWarning[] {
266+
public validateAndroidHomeEnvVariable(
267+
androidSdkPath: string = this.androidHome
268+
): NativeScriptDoctor.IWarning[] {
269+
// ANDROID_HOME is deprecated but has priority over ANDROID_SDK_HOME
270+
if (process.env["ANDROID_HOME"] !== this.androidHome) {
271+
const errors = this.validateAndroidHomeEnvVariable(
272+
process.env["ANDROID_HOME"]
273+
);
274+
275+
// return ok if no errors in "ANDROID_HOME"
276+
if (!errors.length) {
277+
return errors;
278+
}
279+
280+
// otherwise continue to check "ANDROID_SDK_ROOT"...
281+
}
282+
254283
const errors: NativeScriptDoctor.IWarning[] = [];
255284
const expectedDirectoriesInAndroidHome = [
256285
"build-tools",
@@ -259,24 +288,24 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
259288
"extras",
260289
];
261290

262-
if (!this.androidHome || !this.fs.exists(this.androidHome)) {
291+
if (!androidSdkPath || !this.fs.exists(androidSdkPath)) {
263292
errors.push({
264293
warning:
265-
"The ANDROID_HOME environment variable is not set or it points to a non-existent directory. You will not be able to perform any build-related operations for Android.",
294+
"The ANDROID_SDK_ROOT environment variable is not set or it points to a non-existent directory. You will not be able to perform any build-related operations for Android.",
266295
additionalInformation:
267-
"To be able to perform Android build-related operations, set the `ANDROID_HOME` variable to point to the root of your Android SDK installation directory.",
296+
"To be able to perform Android build-related operations, set the `ANDROID_SDK_ROOT` variable to point to the root of your Android SDK installation directory.",
268297
platforms: [Constants.ANDROID_PLATFORM_NAME],
269298
});
270299
} else if (
271300
expectedDirectoriesInAndroidHome.map((dir) =>
272-
this.fs.exists(path.join(this.androidHome, dir))
301+
this.fs.exists(path.join(androidSdkPath, dir))
273302
).length === 0
274303
) {
275304
errors.push({
276305
warning:
277-
"The ANDROID_HOME environment variable points to incorrect directory. You will not be able to perform any build-related operations for Android.",
306+
"The ANDROID_SDK_ROOT environment variable points to incorrect directory. You will not be able to perform any build-related operations for Android.",
278307
additionalInformation:
279-
"To be able to perform Android build-related operations, set the `ANDROID_HOME` variable to point to the root of your Android SDK installation directory, " +
308+
"To be able to perform Android build-related operations, set the `ANDROID_SDK_ROOT` variable to point to the root of your Android SDK installation directory, " +
280309
"where you will find `tools` and `platform-tools` directories.",
281310
platforms: [Constants.ANDROID_PLATFORM_NAME],
282311
});
@@ -378,7 +407,7 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
378407
const isAndroidHomeValid = this.isAndroidHomeValid();
379408

380409
if (isAndroidHomeValid) {
381-
// In case ANDROID_HOME is correct, check if sdkmanager exists and if not it means the SDK has not been updated.
410+
// In case ANDROID_SDK_ROOT is correct, check if sdkmanager exists and if not it means the SDK has not been updated.
382411
// In this case user shoud use `android` from the command-line instead of sdkmanager.
383412
const pathToSdkmanager = path.join(
384413
this.androidHome,
@@ -397,7 +426,7 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
397426

398427
sdkManagementToolPath = pathToExecutable.replace(
399428
this.androidHome,
400-
this.hostInfo.isWindows ? "%ANDROID_HOME%" : "$ANDROID_HOME"
429+
this.hostInfo.isWindows ? "%ANDROID_SDK_ROOT%" : "$ANDROID_SDK_ROOT"
401430
);
402431
}
403432

packages/doctor/src/doctor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class Doctor implements NativeScriptDoctor.IDoctor {
104104
this.processValidationErrors({
105105
warnings: this.androidToolsInfo.validateAndroidHomeEnvVariable(),
106106
infoMessage:
107-
"Your ANDROID_HOME environment variable is set and points to correct directory.",
107+
"Your ANDROID_SDK_ROOT environment variable is set and points to correct directory.",
108108
platforms: [Constants.ANDROID_PLATFORM_NAME],
109109
}),
110110
this.processSysInfoItem({

packages/doctor/typings/interfaces.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,11 @@ declare module NativeScriptDoctor {
209209
* @param {string} runtimeVersion @optional The runtime version against which the validation is executed. In case this parameter is passed, it takes precedence over the projectDir argument.
210210
* @return {Promise<boolean>} true if local build can be executed for the provided platform.
211211
*/
212-
canExecuteLocalBuild(platform: string, projectDir?: string, runtimeVersion?: string): Promise<boolean>;
212+
canExecuteLocalBuild(
213+
platform: string,
214+
projectDir?: string,
215+
runtimeVersion?: string
216+
): Promise<boolean>;
213217

214218
/**
215219
* Executes all checks for the current environment and returns the warnings from each check.
@@ -363,7 +367,10 @@ declare module NativeScriptDoctor {
363367
dotNetVer?: string;
364368
}
365369

366-
interface ISysInfoData extends ICommonSysInfoData, IiOSSysInfoData, IAndroidSysInfoData { }
370+
interface ISysInfoData
371+
extends ICommonSysInfoData,
372+
IiOSSysInfoData,
373+
IAndroidSysInfoData {}
367374

368375
/**
369376
* Describes warning returned from @nativescript/doctor check.
@@ -487,11 +494,15 @@ declare module NativeScriptDoctor {
487494
* @param {string} runtimeVersion @optional The runtime version against which the validation is executed. In case this parameter is passed, it takes precedence over the projectDir argument.
488495
* @return {NativeScriptDoctor.IWarning[]} An array of errors from the validation checks. If there are no errors will return [].
489496
*/
490-
validateJavacVersion(installedJavaVersion: string, projectDir?: string, runtimeVersion?: string): NativeScriptDoctor.IWarning[];
497+
validateJavacVersion(
498+
installedJavaVersion: string,
499+
projectDir?: string,
500+
runtimeVersion?: string
501+
): NativeScriptDoctor.IWarning[];
491502

492503
/**
493-
* Returns the path to the adb which is located in ANDROID_HOME.
494-
* @return {Promise<string>} Path to the adb which is located in ANDROID_HOME.
504+
* Returns the path to the adb which is located in ANDROID_HOME or ANDROID_SDK_ROOT.
505+
* @return {Promise<string>} Path to the adb which is located in ANDROID_HOME or ANDROID_SDK_ROOT.
495506
*/
496507
getPathToAdbFromAndroidHome(): Promise<string>;
497508

@@ -506,14 +517,18 @@ declare module NativeScriptDoctor {
506517
* @param {ITargetValidationOptions} options The targetSdk to be validated and the project directory - used to determine the Android Runtime version.
507518
* @return {NativeScriptDoctor.IWarning[]} An array of errors from the validation checks. If there are no errors will return [].
508519
*/
509-
validateMinSupportedTargetSdk(options: ITargetValidationOptions): NativeScriptDoctor.IWarning[];
520+
validateMinSupportedTargetSdk(
521+
options: ITargetValidationOptions
522+
): NativeScriptDoctor.IWarning[];
510523

511524
/**
512525
* Validates if the provided targetSdk is lower that the maximum supported target SDK.
513526
* @param {ITargetValidationOptions} options The targetSdk to be validated and the project directory - used to determine the Android Runtime version.
514527
* @return {NativeScriptDoctor.IWarning[]} An array of errors from the validation checks. If there are no errors will return [].
515528
*/
516-
validataMaxSupportedTargetSdk(options: ITargetValidationOptions): NativeScriptDoctor.IWarning[];
529+
validataMaxSupportedTargetSdk(
530+
options: ITargetValidationOptions
531+
): NativeScriptDoctor.IWarning[];
517532

518533
/**
519534
* Returns the path to the emulator executable.

0 commit comments

Comments
 (0)