Skip to content

Commit fa257dd

Browse files
edusperonirigor789
andauthored
fix(doctor): improve target and sdk compatibility detection on <8.2 (#5648)
* fix: limit android targets on cli <8.2 * chore: cleanup * fix: validate all info not just targetSdk * chore: bump doctor Co-authored-by: Igor Randjelovic <[email protected]>
1 parent 81cb9c3 commit fa257dd

File tree

8 files changed

+141
-32
lines changed

8 files changed

+141
-32
lines changed

lib/services/android-project-service.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,10 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
313313
}
314314
);
315315

316-
this.$androidToolsInfo.validateTargetSdk({
316+
this.$androidToolsInfo.validateInfo({
317317
showWarningsAsErrors: true,
318318
projectDir: projectData.projectDir,
319+
validateTargetSdk: true,
319320
});
320321

321322
return {

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"mobile"
5858
],
5959
"dependencies": {
60-
"@nativescript/doctor": "2.0.7",
60+
"@nativescript/doctor": "2.0.8",
6161
"@nativescript/schematics-executor": "0.0.2",
6262
"@rigor789/resolve-package-path": "^1.0.5",
6363
"axios": "^0.21.1",

packages/doctor/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/doctor",
3-
"version": "2.0.7",
3+
"version": "2.0.8",
44
"description": "Library that helps identifying if the environment can be used for development of {N} apps.",
55
"main": "src/index.js",
66
"types": "./typings/nativescript-doctor.d.ts",

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

+18-7
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,19 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
3131
"android-32",
3232
];
3333

34-
if (runtimeVersion && semver.lt(semver.coerce(runtimeVersion), "6.1.0")) {
35-
baseTargets.sort();
36-
const indexOfSdk29 = baseTargets.indexOf("android-29");
37-
baseTargets = baseTargets.slice(0, indexOfSdk29);
34+
const isRuntimeVersionLessThan = (targetVersion: string) => {
35+
return (
36+
runtimeVersion &&
37+
semver.lt(semver.coerce(runtimeVersion), targetVersion)
38+
);
39+
};
40+
41+
if (isRuntimeVersionLessThan("6.1.0")) {
42+
// limit baseTargets to android-17 - android-28 if the runtime is < 6.1.0
43+
baseTargets = baseTargets.slice(0, baseTargets.indexOf("android-29"));
44+
} else if (isRuntimeVersionLessThan("8.2.0")) {
45+
// limit baseTargets to android-17 - android-30 if the runtime is < 8.2.0
46+
baseTargets = baseTargets.slice(0, baseTargets.indexOf("android-31"));
3847
}
3948

4049
return baseTargets;
@@ -119,15 +128,16 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
119128
message = `You have to install version ${versionRangeMatches[1]}.`;
120129
}
121130

122-
let invalidBuildToolsAdditionalMsg = `Run \`\$ ${this.getPathToSdkManagementTool()}\` from your command-line to install required \`Android Build Tools\`.`;
131+
// let invalidBuildToolsAdditionalMsg = `Run \`\$ ${this.getPathToSdkManagementTool()}\` from your command-line to install required \`Android Build Tools\`.`;
132+
let invalidBuildToolsAdditionalMsg = `Install the required build-tools through Android Studio.`;
123133
if (!isAndroidHomeValid) {
124134
invalidBuildToolsAdditionalMsg +=
125-
" In case you already have them installed, make sure `ANDROID_HOME` environment variable is set correctly.";
135+
" In case you already have them installed, make sure the `ANDROID_HOME` environment variable is set correctly.";
126136
}
127137

128138
errors.push({
129139
warning:
130-
"You need to have the Android SDK Build-tools installed on your system. " +
140+
"No compatible version of the Android SDK Build-tools are installed on your system. " +
131141
message,
132142
additionalInformation: invalidBuildToolsAdditionalMsg,
133143
platforms: [Constants.ANDROID_PLATFORM_NAME],
@@ -504,6 +514,7 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
504514

505515
private getMaxSupportedVersion(projectDir: string): number {
506516
const supportedTargets = this.getSupportedTargets(projectDir);
517+
507518
return this.parseAndroidSdkString(
508519
supportedTargets.sort()[supportedTargets.length - 1]
509520
);

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

+106-10
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,25 @@ describe("androidToolsInfo", () => {
4747
},
4848
readDirectory: (path: string) => {
4949
if (path.indexOf("build-tools") >= 0) {
50-
return ["20.0.0", "27.0.3", "28.0.3", "29.0.1"];
50+
return [
51+
"20.0.0",
52+
"27.0.3",
53+
"28.0.3",
54+
"29.0.1",
55+
"30.0.0",
56+
"31.0.0",
57+
"32.0.0",
58+
];
5159
} else {
52-
return ["android-16", "android-27", "android-28", "android-29"];
60+
return [
61+
"android-16",
62+
"android-27",
63+
"android-28",
64+
"android-29",
65+
"android-30",
66+
"android-31",
67+
"android-32",
68+
];
5369
}
5470
},
5571
};
@@ -58,34 +74,68 @@ describe("androidToolsInfo", () => {
5874
return new AndroidToolsInfo(childProcess, fs, hostInfo, helpers);
5975
};
6076

61-
describe("getToolsInfo", () => {
62-
it("runtime 6.0.0", () => {
77+
describe("getToolsInfo -> compileSdkVersion", () => {
78+
it("runtime 6.0.0 - 28", () => {
6379
const androidToolsInfo = getAndroidToolsInfo("6.0.0");
6480
const toolsInfo = androidToolsInfo.getToolsInfo({ projectDir: "test" });
6581

6682
assert.equal(toolsInfo.compileSdkVersion, 28);
6783
});
6884

69-
it("runtime 6.1.0", () => {
85+
it("runtime 6.1.0 - 30", () => {
7086
const androidToolsInfo = getAndroidToolsInfo("6.1.0");
7187
const toolsInfo = androidToolsInfo.getToolsInfo({ projectDir: "test" });
7288

73-
assert.equal(toolsInfo.compileSdkVersion, 29);
89+
assert.equal(toolsInfo.compileSdkVersion, 30);
90+
});
91+
92+
it("runtime 8.1.1 - 30", () => {
93+
const androidToolsInfo = getAndroidToolsInfo("8.1.1");
94+
const toolsInfo = androidToolsInfo.getToolsInfo({ projectDir: "test" });
95+
96+
assert.equal(toolsInfo.compileSdkVersion, 30);
97+
});
98+
99+
it("runtime 8.2.0 - 32", () => {
100+
const androidToolsInfo = getAndroidToolsInfo("8.2.0");
101+
const toolsInfo = androidToolsInfo.getToolsInfo({ projectDir: "test" });
102+
103+
assert.equal(toolsInfo.compileSdkVersion, 32);
74104
});
75105
});
76106

77107
describe("supportedAndroidSdks", () => {
78-
it("should support android-17 - android-32", () => {
79-
const min = 17;
80-
const max = 32;
108+
const assertSupportedRange = (
109+
runtimeVersion: string,
110+
min: number,
111+
max: number
112+
) => {
81113
let cnt = 0;
82-
const androidToolsInfo = getAndroidToolsInfo("6.5.0");
114+
const androidToolsInfo = getAndroidToolsInfo(runtimeVersion);
83115
const supportedTargets = androidToolsInfo.getSupportedTargets("test");
84116
for (let i = 0; i < supportedTargets.length; i++) {
85117
assert.equal(supportedTargets[i], `android-${min + i}`);
86118
cnt = min + i;
87119
}
88120
assert.equal(cnt, max);
121+
};
122+
123+
it("runtime 6.0.0 should support android-17 - android-28", () => {
124+
const min = 17;
125+
const max = 28;
126+
assertSupportedRange("6.0.0", min, max);
127+
});
128+
129+
it("runtime 8.1.0 should support android-17 - android-30", () => {
130+
const min = 17;
131+
const max = 30;
132+
assertSupportedRange("8.1.0", min, max);
133+
});
134+
135+
it("runtime 8.2.0 should support android-17 - android-32", () => {
136+
const min = 17;
137+
const max = 32;
138+
assertSupportedRange("8.2.0", min, max);
89139
});
90140
});
91141

@@ -295,6 +345,52 @@ describe("androidToolsInfo", () => {
295345
);
296346
});
297347

348+
describe("validataMaxSupportedTargetSdk", () => {
349+
const testCases = [
350+
{
351+
runtimeVersion: "8.1.0",
352+
targetSdk: 30,
353+
expectWarning: false,
354+
},
355+
{
356+
runtimeVersion: "8.1.0",
357+
targetSdk: 31,
358+
expectWarning: true,
359+
},
360+
{
361+
runtimeVersion: "8.1.0",
362+
targetSdk: 32,
363+
expectWarning: true,
364+
},
365+
{
366+
runtimeVersion: "8.2.0",
367+
targetSdk: 32,
368+
expectWarning: false,
369+
},
370+
];
371+
372+
testCases.forEach(({ runtimeVersion, targetSdk, expectWarning }) => {
373+
it(`for runtime ${runtimeVersion} - and targetSdk ${targetSdk}`, () => {
374+
const androidToolsInfo = getAndroidToolsInfo(runtimeVersion);
375+
const actualWarnings = androidToolsInfo.validataMaxSupportedTargetSdk({
376+
projectDir: "test",
377+
targetSdk,
378+
});
379+
let expectedWarnings: NativeScriptDoctor.IWarning[] = [];
380+
381+
if (expectWarning) {
382+
expectedWarnings.push({
383+
additionalInformation: "",
384+
platforms: ["Android"],
385+
warning: `Support for the selected Android target SDK android-${targetSdk} is not verified. Your Android app might not work as expected.`,
386+
});
387+
}
388+
389+
assert.deepEqual(actualWarnings, expectedWarnings);
390+
});
391+
});
392+
});
393+
298394
after(() => {
299395
process.env["ANDROID_HOME"] = originalAndroidHome;
300396
});

packages/doctor/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
},
66
"include": [
77
"src/",
8-
"test/"
8+
"test/",
9+
"typings/"
910
]
1011
}

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,10 @@
340340
"resolved" "https://registry.npmjs.org/@kwsites/promise-deferred/-/promise-deferred-1.1.1.tgz"
341341
"version" "1.1.1"
342342

343-
"@nativescript/[email protected].7":
344-
"integrity" "sha512-Pd3NlFGXN+SC03sBC/pj/9t5v5h7bh2xnbjbXHCo1MTjjm6fpLGFSxlCZRPPFgDIisf4OqGmEeIglNpy7CbyZg=="
345-
"resolved" "https://registry.npmjs.org/@nativescript/doctor/-/doctor-2.0.7.tgz"
346-
"version" "2.0.7"
343+
"@nativescript/[email protected].8":
344+
"integrity" "sha512-/jVGBBBBY2BX1IwriDyXHNi0ZNAkSuzdDQuGY3nUl3BDLu5AM+FFg4qCG3D9IW664WLbA1KbJQd+HUSjRHM/ZQ=="
345+
"resolved" "https://registry.npmjs.org/@nativescript/doctor/-/doctor-2.0.8.tgz"
346+
"version" "2.0.8"
347347
dependencies:
348348
"lodash" "4.17.21"
349349
"osenv" "0.1.5"

0 commit comments

Comments
 (0)