Skip to content

Commit 7c6dc40

Browse files
committed
feat(android): allow to force gradle version throught nativescript-config (android.gradleVersion).
Also we now replace `{{pluginNamespace}}` for plugins as requirement for gradle 8 Same `__PACKAGE__` is replaced in app build.gradle (for namespace or app id)
1 parent b5fef05 commit 7c6dc40

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

lib/definitions/project.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ interface INsConfigAndroid extends INsConfigPlaform {
139139
enableLineBreakpoints?: boolean;
140140

141141
enableMultithreadedJavascript?: boolean;
142+
143+
gradleVersion?: string;
142144
}
143145

144146
interface INsConfigHooks {

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,18 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
361361
this.$fs.copyFile(path.join(dir, "*"), destination);
362362
}
363363
}
364-
364+
private extractNamespaceFromManifest(manifestPath:string): string {
365+
const fileContent = this.$fs.readText(manifestPath);
366+
const contentRegex = new RegExp('package="(.*)"');
367+
const match = fileContent.match(contentRegex);
368+
let namespace: string;
369+
if (match) {
370+
namespace = match[1];
371+
const replacedFileContent = fileContent.replace(contentRegex, "");
372+
this.$fs.writeFile(manifestPath, replacedFileContent);
373+
}
374+
return namespace;
375+
}
365376
private async setupGradle(
366377
pluginTempDir: string,
367378
platformsAndroidDirPath: string,
@@ -379,10 +390,25 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
379390
const runtimeGradleVersions = await this.getRuntimeGradleVersions(
380391
projectDir
381392
);
393+
let gradleVersion = runtimeGradleVersions.gradleVersion;
394+
if (this.$projectData.nsConfig.android.gradleVersion) {
395+
gradleVersion = this.$projectData.nsConfig.android.gradleVersion;
396+
}
382397
this.replaceGradleVersion(
383398
pluginTempDir,
384-
runtimeGradleVersions.gradleVersion
399+
gradleVersion
385400
);
401+
402+
// In gradle 8 every android project must have a namespace in "android"
403+
// and the package property in manifest is now forbidden
404+
// let s replace it
405+
const manifestFilePath = this.getManifest(path.join(pluginTempDir, 'src', 'main'));
406+
let pluginNamespace = this.extractNamespaceFromManifest(manifestFilePath);
407+
if (!pluginNamespace) {
408+
pluginNamespace = pluginName.replace(/@/g, '').replace(/[/-]/g, '.')
409+
}
410+
411+
this.replaceFileContent(buildGradlePath, "{{pluginNamespace}}", pluginNamespace);
386412
this.replaceFileContent(buildGradlePath, "{{pluginName}}", pluginName);
387413
this.replaceFileContent(settingsGradlePath, "{{pluginName}}", pluginName);
388414
}

lib/services/android-project-service.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,24 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
459459
gradleSettingsFilePath
460460
);
461461

462+
463+
const gradleVersion = projectData.nsConfig.android.gradleVersion;
464+
if (gradleVersion) {
465+
// user defined a custom gradle version, let's apply it
466+
const gradleWrapperFilePath = path.join(
467+
this.getPlatformData(projectData).projectRoot,
468+
"gradle",
469+
"wrapper",
470+
"gradle-wrapper.properties"
471+
);
472+
shell.sed(
473+
"-i",
474+
/gradle-([0-9.]+)-bin.zip/,
475+
`gradle-${gradleVersion}-bin.zip`,
476+
gradleWrapperFilePath
477+
);
478+
}
479+
462480
try {
463481
// will replace applicationId in app/App_Resources/Android/app.gradle if it has not been edited by the user
464482
const appGradleContent = this.$fs.readText(projectData.appGradlePath);
@@ -487,6 +505,17 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
487505
projectData.projectIdentifiers.android,
488506
manifestPath
489507
);
508+
const buildGradlePath = path.join(
509+
this.getPlatformData(projectData).projectRoot,
510+
"app",
511+
"build.gradle"
512+
);
513+
shell.sed(
514+
"-i",
515+
/__PACKAGE__/,
516+
projectData.projectIdentifiers.android,
517+
buildGradlePath
518+
);
490519
}
491520

492521
private getProjectNameFromId(projectData: IProjectData): string {

0 commit comments

Comments
 (0)