Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: support ANDROID_SDK_ROOT according to android docs #5632

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions packages/doctor/src/android-tools-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,21 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
private static MAX_JAVA_VERSION = null as string;

private toolsInfo: NativeScriptDoctor.IAndroidToolsInfoData;

/**
* Excerpt from: https://developer.android.com/studio/command-line/variables#envar
*
* ANDROID_SDK_ROOT - Sets the path to the SDK installation directory.
* Once set, the value does not typically change, and can be shared by multiple users on the same machine.
* ANDROID_HOME, which also points to the SDK installation directory, is deprecated.
* If you continue to use it, the following rules apply:
*
* - If ANDROID_HOME is defined and contains a valid SDK installation, its value is used instead of the value in ANDROID_SDK_ROOT.
* - If ANDROID_HOME is not defined, the value in ANDROID_SDK_ROOT is used.
* - 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.
*/
public get androidHome(): string {
return process.env["ANDROID_HOME"];
return process.env["ANDROID_SDK_ROOT"] ?? process.env["ANDROID_HOME"];
}
private pathToEmulatorExecutable: string;

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

errors.push({
Expand Down Expand Up @@ -250,7 +263,23 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
return null;
}

public validateAndroidHomeEnvVariable(): NativeScriptDoctor.IWarning[] {
public validateAndroidHomeEnvVariable(
androidSdkPath: string = this.androidHome
): NativeScriptDoctor.IWarning[] {
// ANDROID_HOME is deprecated but has priority over ANDROID_SDK_HOME
if (process.env["ANDROID_HOME"] !== this.androidHome) {
const errors = this.validateAndroidHomeEnvVariable(
process.env["ANDROID_HOME"]
);

// return ok if no errors in "ANDROID_HOME"
if (!errors.length) {
return errors;
}

// otherwise continue to check "ANDROID_SDK_ROOT"...
}

const errors: NativeScriptDoctor.IWarning[] = [];
const expectedDirectoriesInAndroidHome = [
"build-tools",
Expand All @@ -259,24 +288,24 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
"extras",
];

if (!this.androidHome || !this.fs.exists(this.androidHome)) {
if (!androidSdkPath || !this.fs.exists(androidSdkPath)) {
errors.push({
warning:
"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.",
"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.",
additionalInformation:
"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.",
"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.",
platforms: [Constants.ANDROID_PLATFORM_NAME],
});
} else if (
expectedDirectoriesInAndroidHome.map((dir) =>
this.fs.exists(path.join(this.androidHome, dir))
this.fs.exists(path.join(androidSdkPath, dir))
).length === 0
) {
errors.push({
warning:
"The ANDROID_HOME environment variable points to incorrect directory. You will not be able to perform any build-related operations for Android.",
"The ANDROID_SDK_ROOT environment variable points to incorrect directory. You will not be able to perform any build-related operations for Android.",
additionalInformation:
"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, " +
"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, " +
"where you will find `tools` and `platform-tools` directories.",
platforms: [Constants.ANDROID_PLATFORM_NAME],
});
Expand Down Expand Up @@ -378,7 +407,7 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
const isAndroidHomeValid = this.isAndroidHomeValid();

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

sdkManagementToolPath = pathToExecutable.replace(
this.androidHome,
this.hostInfo.isWindows ? "%ANDROID_HOME%" : "$ANDROID_HOME"
this.hostInfo.isWindows ? "%ANDROID_SDK_ROOT%" : "$ANDROID_SDK_ROOT"
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/doctor/src/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class Doctor implements NativeScriptDoctor.IDoctor {
this.processValidationErrors({
warnings: this.androidToolsInfo.validateAndroidHomeEnvVariable(),
infoMessage:
"Your ANDROID_HOME environment variable is set and points to correct directory.",
"Your ANDROID_SDK_ROOT environment variable is set and points to correct directory.",
platforms: [Constants.ANDROID_PLATFORM_NAME],
}),
this.processSysInfoItem({
Expand Down
29 changes: 22 additions & 7 deletions packages/doctor/typings/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ declare module NativeScriptDoctor {
* @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.
* @return {Promise<boolean>} true if local build can be executed for the provided platform.
*/
canExecuteLocalBuild(platform: string, projectDir?: string, runtimeVersion?: string): Promise<boolean>;
canExecuteLocalBuild(
platform: string,
projectDir?: string,
runtimeVersion?: string
): Promise<boolean>;

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

interface ISysInfoData extends ICommonSysInfoData, IiOSSysInfoData, IAndroidSysInfoData { }
interface ISysInfoData
extends ICommonSysInfoData,
IiOSSysInfoData,
IAndroidSysInfoData {}

/**
* Describes warning returned from @nativescript/doctor check.
Expand Down Expand Up @@ -487,11 +494,15 @@ declare module NativeScriptDoctor {
* @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.
* @return {NativeScriptDoctor.IWarning[]} An array of errors from the validation checks. If there are no errors will return [].
*/
validateJavacVersion(installedJavaVersion: string, projectDir?: string, runtimeVersion?: string): NativeScriptDoctor.IWarning[];
validateJavacVersion(
installedJavaVersion: string,
projectDir?: string,
runtimeVersion?: string
): NativeScriptDoctor.IWarning[];

/**
* Returns the path to the adb which is located in ANDROID_HOME.
* @return {Promise<string>} Path to the adb which is located in ANDROID_HOME.
* Returns the path to the adb which is located in ANDROID_HOME or ANDROID_SDK_ROOT.
* @return {Promise<string>} Path to the adb which is located in ANDROID_HOME or ANDROID_SDK_ROOT.
*/
getPathToAdbFromAndroidHome(): Promise<string>;

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

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

/**
* Returns the path to the emulator executable.
Expand Down