Skip to content

Commit c6a117e

Browse files
committed
fix(android): only name aars for packages the CLI builds
Aar names were allocated among every package with a `nativescript` key and a platforms/android directory, including packages that only ship prebuilt aars. Those never run the aar build or use tempPlugin, yet could make prepare fail: `@foo/plugin-x` (with sources) next to `@foo/plugin_x` (only platforms/android/vendor.aar) both mapped to `foo_plugin_x`. Names are now allocated only among packages buildAar builds: a `nativescript` package whose platforms/android has an AndroidManifest.xml or Android source directories. Plugins that declare Android but only ship prebuilt aars are left out too and get no aar name. AndroidPluginBuildService exposes that condition as hasAarSources, which buildAar and hasOutdatedAar use as well. Two packages that both build still clash and get renamed or rejected as before.
1 parent b0af0fc commit c6a117e

6 files changed

Lines changed: 96 additions & 31 deletions

File tree

‎lib/definitions/android-plugin-migrator.d.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ interface IAndroidBuildOptions {
2222
interface IAndroidPluginBuildService {
2323
buildAar(options: IPluginBuildOptions): Promise<boolean>;
2424
migrateIncludeGradle(options: IPluginBuildOptions): boolean;
25+
/**
26+
* Whether `buildAar` builds an aar from this platforms/android directory,
27+
* i.e. it has an AndroidManifest.xml or Android source directories.
28+
*/
29+
hasAarSources(platformsAndroidDirPath: string): boolean;
2530
/**
2631
* Whether the plugin has sources to build but `aarOutputDir` lacks its aar
2732
* or still holds one the CLI built under another of its candidate names.

‎lib/services/android-plugin-build-service.ts‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,11 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
240240
const androidSourceDirectories = this.getAndroidSourceDirectories(
241241
options.platformsAndroidDirPath,
242242
);
243+
const hasAarSources = this.hasAarSources(options.platformsAndroidDirPath);
243244
const aarName = this.getAarName(options);
244245
const pluginTempDir = path.join(options.tempPluginDirPath, aarName);
245246
const staleAars =
246-
options.aarName && (manifestFilePath || androidSourceDirectories.length)
247+
options.aarName && hasAarSources
247248
? this.getStaleAars(options, aarName)
248249
: [];
249250
const pluginSourceFileHashesInfo = await this.getSourceFilesHashes(
@@ -256,8 +257,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
256257
this.removeStaleAars(options.pluginName, staleAars);
257258

258259
const shouldBuildAar = await this.shouldBuildAar({
259-
manifestFilePath,
260-
androidSourceDirectories,
260+
hasAarSources,
261261
pluginTempDir,
262262
aarPath: path.join(this.getAarOutputDir(options), `${aarName}.aar`),
263263
fileHashesInfo: pluginSourceFileHashesInfo,
@@ -300,13 +300,16 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
300300
return shouldBuildAar;
301301
}
302302

303+
public hasAarSources(platformsAndroidDirPath: string): boolean {
304+
return (
305+
this.$fs.exists(platformsAndroidDirPath) &&
306+
(!!this.getManifest(platformsAndroidDirPath) ||
307+
this.getAndroidSourceDirectories(platformsAndroidDirPath).length > 0)
308+
);
309+
}
310+
303311
public hasOutdatedAar(options: IPluginBuildOptions): boolean {
304-
if (
305-
!this.$fs.exists(options.platformsAndroidDirPath) ||
306-
(!this.getManifest(options.platformsAndroidDirPath) &&
307-
!this.getAndroidSourceDirectories(options.platformsAndroidDirPath)
308-
.length)
309-
) {
312+
if (!this.hasAarSources(options.platformsAndroidDirPath)) {
310313
return false;
311314
}
312315

@@ -442,14 +445,12 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
442445
}
443446

444447
private async shouldBuildAar(opts: {
445-
manifestFilePath: string;
446-
androidSourceDirectories: string[];
448+
hasAarSources: boolean;
447449
pluginTempDir: string;
448450
aarPath: string;
449451
fileHashesInfo: IStringDictionary;
450452
}): Promise<boolean> {
451-
let shouldBuildAar =
452-
!!opts.manifestFilePath || !!opts.androidSourceDirectories.length;
453+
let shouldBuildAar = opts.hasAarSources;
453454

454455
if (
455456
shouldBuildAar &&

‎lib/services/android-project-service.ts‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,9 @@ export class AndroidProjectService
780780
}
781781

782782
/**
783-
* Names must be unique among every package preparePluginNativeCode builds,
784-
* not only those in dependencies.json, since they share platforms/tempPlugin.
783+
* Names must be unique among every package preparePluginNativeCode builds
784+
* an aar for, not only those in dependencies.json, since they share
785+
* platforms/tempPlugin. Packages that only ship prebuilt aars take no name.
785786
*/
786787
private setPluginAarNames(
787788
projectData: IProjectData,
@@ -790,15 +791,14 @@ export class AndroidProjectService
790791
const aarPluginNames = dependencies
791792
.filter(
792793
(dep) =>
793-
AndroidProjectService.isNativeAndroidDependency(dep) ||
794-
(dep.nativescript &&
795-
this.$fs.exists(
796-
path.join(
797-
dep.directory,
798-
constants.PLATFORMS_DIR_NAME,
799-
AndroidProjectService.ANDROID_PLATFORM_NAME,
800-
),
801-
)),
794+
dep.nativescript &&
795+
this.$androidPluginBuildService.hasAarSources(
796+
path.join(
797+
dep.directory,
798+
constants.PLATFORMS_DIR_NAME,
799+
AndroidProjectService.ANDROID_PLATFORM_NAME,
800+
),
801+
),
802802
)
803803
.map((dep) => dep.name);
804804

‎test/services/android-plugin-build-service.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,38 @@ dependencies {
690690
);
691691
});
692692

693+
describe("hasAarSources", () => {
694+
it("is true for a manifest or Android source directories", () => {
695+
const withManifest = setup({ addManifest: true });
696+
assert.isTrue(
697+
androidBuildPluginService.hasAarSources(
698+
withManifest.platformsAndroidDirPath,
699+
),
700+
);
701+
const withResources = setup({ addResFolder: true });
702+
assert.isTrue(
703+
androidBuildPluginService.hasAarSources(
704+
withResources.platformsAndroidDirPath,
705+
),
706+
);
707+
});
708+
709+
it("is false for prebuilt aars only or a missing directory", () => {
710+
const config = setup({ addIncludeGradle: true });
711+
fs.writeFile(path.join(pluginFolder, "vendor.aar"), "");
712+
assert.isFalse(
713+
androidBuildPluginService.hasAarSources(
714+
config.platformsAndroidDirPath,
715+
),
716+
);
717+
assert.isFalse(
718+
androidBuildPluginService.hasAarSources(
719+
path.join(pluginFolder, "missing"),
720+
),
721+
);
722+
});
723+
});
724+
693725
describe("hasOutdatedAar", () => {
694726
it("is false for a plugin without sources to build", () => {
695727
const config: IPluginBuildOptions = {

‎test/services/android-project-service.ts‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -433,16 +433,16 @@ describe("androidProjectService plugin aar names", () => {
433433
name: string;
434434
dependencies?: string[];
435435
native?: boolean;
436-
androidDir?: boolean;
436+
android?: "sources" | "prebuilt" | "none";
437437
}): void {
438438
const directory = path.join(projectDir, "node_modules", plugin.name);
439439
const nativescript =
440440
plugin.native === false ? {} : { platforms: { android: "8.0.0" } };
441-
if (plugin.androidDir !== false) {
442-
fs.writeFile(
443-
path.join(directory, "platforms", "android", "AndroidManifest.xml"),
444-
"<manifest/>",
445-
);
441+
const androidDir = path.join(directory, "platforms", "android");
442+
if (plugin.android === "prebuilt") {
443+
fs.writeFile(path.join(androidDir, "vendor.aar"), "");
444+
} else if (plugin.android !== "none") {
445+
fs.writeFile(path.join(androidDir, "AndroidManifest.xml"), "<manifest/>");
446446
}
447447
dependencies.push(<IDependencyData>{
448448
name: plugin.name,
@@ -496,6 +496,8 @@ describe("androidProjectService plugin aar names", () => {
496496
},
497497
migrateIncludeGradle: () => false,
498498
hasOutdatedAar: () => outdatedAar,
499+
hasAarSources: (platformsAndroidDirPath: string) =>
500+
fs.exists(path.join(platformsAndroidDirPath, "AndroidManifest.xml")),
499501
});
500502
for (const unused of [
501503
"androidToolsInfo",
@@ -642,7 +644,7 @@ describe("androidProjectService plugin aar names", () => {
642644

643645
it("ignores packages without platforms/android when naming aars", async () => {
644646
addPlugin({ name: "@foo/plugin-x" });
645-
addPlugin({ name: "@bar/plugin-x", native: false, androidDir: false });
647+
addPlugin({ name: "@bar/plugin-x", native: false, android: "none" });
646648
const projectData = createProjectData();
647649
await service.beforePrepareAllPlugins(projectData, dependencies);
648650

@@ -654,6 +656,27 @@ describe("androidProjectService plugin aar names", () => {
654656
assert.equal(buildAarCalls[0].aarName, "plugin_x");
655657
});
656658

659+
it("ignores packages that only ship prebuilt aars when naming aars", async () => {
660+
addPlugin({ name: "@foo/plugin-x" });
661+
addPlugin({ name: "@foo/plugin_x", native: false, android: "prebuilt" });
662+
addPlugin({ name: "@bar/plugin-x", android: "prebuilt" });
663+
const projectData = createProjectData();
664+
await service.beforePrepareAllPlugins(projectData, dependencies);
665+
666+
for (const name of ["@foo/plugin-x", "@foo/plugin_x", "@bar/plugin-x"]) {
667+
await service.preparePluginNativeCode(pluginData(name), projectData);
668+
}
669+
670+
assert.deepStrictEqual(
671+
buildAarCalls.map((options) => [options.pluginName, options.aarName]),
672+
[
673+
["@foo/plugin-x", "plugin_x"],
674+
["@foo/plugin_x", undefined],
675+
["@bar/plugin-x", undefined],
676+
],
677+
);
678+
});
679+
657680
it("works out the aar names when plugins are prepared on their own", async () => {
658681
addClashingPlugins();
659682

‎test/stubs.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,10 @@ export class AndroidPluginBuildServiceStub implements IAndroidPluginBuildService
754754
hasOutdatedAar(options: IPluginBuildOptions): boolean {
755755
return false;
756756
}
757+
758+
hasAarSources(platformsAndroidDirPath: string): boolean {
759+
return false;
760+
}
757761
}
758762

759763
export class PlatformProjectServiceStub

0 commit comments

Comments
 (0)