Skip to content

Commit 543ded6

Browse files
Dimitar TachevDimitar Tachev
Dimitar Tachev
authored and
Dimitar Tachev
committed
feat: use the Gradle and Android plugin versions specified in the package.json of the runtime instead of hardcoded ones
1 parent 983c3b7 commit 543ded6

File tree

7 files changed

+74
-22
lines changed

7 files changed

+74
-22
lines changed

lib/declarations.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -850,3 +850,11 @@ interface IAssetsGenerationService {
850850
*/
851851
generateSplashScreens(splashesGenerationData: ISplashesGenerationData): Promise<void>;
852852
}
853+
854+
/**
855+
* Describes the Gradle versions specified in the package.json of the Android runtime
856+
*/
857+
interface IRuntimeGradleVersions {
858+
gradleVersion?: string;
859+
gradleAndroidPluginVersion?: string;
860+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
interface IBuildOptions extends IAndroidBuildOptions{
3+
projectDir?: string
34
}
45

56
interface IAndroidBuildOptions {

lib/node-package-manager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export class NodePackageManager implements INodePackageManager {
123123
const url = `https://registry.npmjs.org/${packageName}`;
124124
this.$logger.trace(`Trying to get data from npm registry for package ${packageName}, url is: ${url}`);
125125
const responseData = (await this.$httpClient.httpRequest(url)).body;
126-
this.$logger.trace(`Successfully received data from npm registry for package ${packageName}. Response data is: ${responseData}`);
126+
this.$logger.trace(`Successfully received data from npm registry for package ${packageName}.`);
127127
const jsonData = JSON.parse(responseData);
128128
this.$logger.trace(`Successfully parsed data from npm registry for package ${packageName}.`);
129129
return jsonData;

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

+60-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as path from "path";
2-
import { MANIFEST_FILE_NAME, INCLUDE_GRADLE_NAME, ASSETS_DIR, RESOURCES_DIR } from "../constants";
2+
import { MANIFEST_FILE_NAME, INCLUDE_GRADLE_NAME, ASSETS_DIR, RESOURCES_DIR, TNS_ANDROID_RUNTIME_NAME } from "../constants";
33
import { getShortPluginName, hook } from "../common/helpers";
44
import { Builder, parseString } from "xml2js";
55
import { ILogger } from "log4js";
@@ -12,19 +12,25 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
1212
return this.$injector.resolve("hooksService");
1313
}
1414

15+
private get $platformService(): IPlatformService {
16+
return this.$injector.resolve("platformService");
17+
}
18+
1519
constructor(private $injector: IInjector,
1620
private $fs: IFileSystem,
1721
private $childProcess: IChildProcess,
1822
private $hostInfo: IHostInfo,
1923
private $androidToolsInfo: IAndroidToolsInfo,
20-
private $logger: ILogger) { }
24+
private $logger: ILogger,
25+
private $npm: INodePackageManager,
26+
private $projectDataService: IProjectDataService,
27+
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants) { }
2128

2229
private static MANIFEST_ROOT = {
2330
$: {
2431
"xmlns:android": "http://schemas.android.com/apk/res/android"
2532
}
2633
};
27-
private static ANDROID_PLUGIN_GRADLE_TEMPLATE = "../../vendor/gradle-plugin";
2834

2935
private getAndroidSourceDirectories(source: string): Array<string> {
3036
const directories = [RESOURCES_DIR, "java", ASSETS_DIR, "jniLibs"];
@@ -174,7 +180,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
174180

175181
await this.updateManifest(manifestFilePath, pluginTempMainSrcDir, shortPluginName);
176182
this.copySourceSetDirectories(androidSourceSetDirectories, pluginTempMainSrcDir);
177-
this.setupGradle(pluginTempDir, options.platformsAndroidDirPath);
183+
await this.setupGradle(pluginTempDir, options.platformsAndroidDirPath, options.projectDir);
178184
await this.buildPlugin({ pluginDir: pluginTempDir, pluginName: options.pluginName });
179185
this.copyAar(shortPluginName, pluginTempDir, options.aarOutputDir);
180186
}
@@ -221,23 +227,67 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
221227
}
222228
}
223229

224-
private setupGradle(pluginTempDir: string, platformsAndroidDirPath: string): void {
225-
const gradleTemplatePath = path.resolve(path.join(__dirname, AndroidPluginBuildService.ANDROID_PLUGIN_GRADLE_TEMPLATE));
230+
private async setupGradle(pluginTempDir: string, platformsAndroidDirPath: string, projectDir: string): Promise<void> {
231+
const gradleTemplatePath = path.resolve(path.join(__dirname, "../../vendor/gradle-plugin"));
226232
const allGradleTemplateFiles = path.join(gradleTemplatePath, "*");
233+
const buildGradlePath = path.join(pluginTempDir, "build.gradle");
227234

228235
this.$fs.copyFile(allGradleTemplateFiles, pluginTempDir);
229-
this.addCompileDependencies(platformsAndroidDirPath, pluginTempDir);
230-
// TODO: replace placeholders in target destination
236+
this.addCompileDependencies(platformsAndroidDirPath, buildGradlePath);
237+
const runtimeGradleVersions = await this.getRuntimeGradleVersions(projectDir);
238+
this.replaceGradleVersion(pluginTempDir, runtimeGradleVersions.gradleVersion);
239+
this.replaceGradleAndroidPluginVersion(buildGradlePath, runtimeGradleVersions.gradleAndroidPluginVersion);
240+
}
241+
242+
private async getRuntimeGradleVersions(projectDir: string): Promise<IRuntimeGradleVersions> {
243+
const runtimeVersions: IRuntimeGradleVersions = {};
244+
const registryData = await this.$npm.getRegistryPackageData(TNS_ANDROID_RUNTIME_NAME);
245+
let runtimeVersion: string = registryData["dist-tags"].latest;
246+
if (projectDir) {
247+
runtimeVersion = this.$platformService.getCurrentPlatformVersion(
248+
this.$devicePlatformsConstants.Android,
249+
this.$projectDataService.getProjectData(projectDir));
250+
}
251+
252+
const latestPackageData = registryData.versions[runtimeVersion];
253+
const packageJsonGradle = latestPackageData && latestPackageData.gradle;
254+
if (packageJsonGradle) {
255+
runtimeVersions.gradleVersion = packageJsonGradle.version;
256+
runtimeVersions.gradleAndroidPluginVersion = packageJsonGradle.android;
257+
}
258+
259+
return runtimeVersions;
260+
}
261+
262+
private replaceGradleVersion(pluginTempDir: string, version: string): void {
263+
const gradleVersion = version || "4.4";
264+
const gradleVersionPlaceholder = "{{runtimeGradleVersion}}";
265+
const gradleWrapperPropertiesPath = path.join(pluginTempDir, "gradle", "wrapper", "gradle-wrapper.properties");
266+
267+
this.replaceFileContent(gradleWrapperPropertiesPath, gradleVersionPlaceholder, gradleVersion);
268+
}
269+
270+
private replaceGradleAndroidPluginVersion(buildGradlePath: string, version: string): void {
271+
const gradleAndroidPluginVersionPlaceholder = "{{runtimeAndroidPluginVersion}}";
272+
const gradleAndroidPluginVersion = version || "3.1.2";
273+
274+
this.replaceFileContent(buildGradlePath, gradleAndroidPluginVersionPlaceholder,gradleAndroidPluginVersion);
275+
}
276+
277+
private replaceFileContent(filePath: string, content: string, replacement: string) {
278+
const fileContent = this.$fs.readText(filePath);
279+
const contentRegex = new RegExp(content, "g");
280+
const replacedFileContent = fileContent.replace(contentRegex, replacement);
281+
this.$fs.writeFile(filePath, replacedFileContent);
231282
}
232283

233-
private addCompileDependencies(platformsAndroidDirPath: string, pluginTempDir: string): void {
284+
private addCompileDependencies(platformsAndroidDirPath: string, buildGradlePath: string): void {
234285
const includeGradlePath = path.join(platformsAndroidDirPath, INCLUDE_GRADLE_NAME);
235286
if (this.$fs.exists(includeGradlePath)) {
236287
const includeGradleContent = this.$fs.readText(includeGradlePath);
237288
const repositoriesAndDependenciesScopes = this.getIncludeGradleCompileDependenciesScope(includeGradleContent);
238289

239290
if (repositoriesAndDependenciesScopes.length > 0) {
240-
const buildGradlePath = path.join(pluginTempDir, "build.gradle");
241291
this.$fs.appendFile(buildGradlePath, "\n" + repositoriesAndDependenciesScopes.join("\n"));
242292
}
243293
}

lib/services/android-project-service.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
100100
return this._platformData;
101101
}
102102

103-
// TODO: Remove prior to the 4.0 CLI release @Pip3r4o @PanayotCankov
104-
// Similar to the private method of the same name in platform-service.
105-
private getCurrentPlatformVersion(platformData: IPlatformData, projectData: IProjectData): string {
103+
public getCurrentPlatformVersion(platformData: IPlatformData, projectData: IProjectData): string {
106104
const currentPlatformData: IDictionary<any> = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName);
107105

108106
return currentPlatformData && currentPlatformData[constants.VERSION_STRING];
@@ -422,6 +420,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
422420
const pluginPlatformsFolderPath = this.getPluginPlatformsFolderPath(pluginData, AndroidProjectService.ANDROID_PLATFORM_NAME);
423421
if (this.$fs.exists(pluginPlatformsFolderPath)) {
424422
const options: IBuildOptions = {
423+
projectDir: projectData.projectDir,
425424
pluginName: pluginData.name,
426425
platformsAndroidDirPath: pluginPlatformsFolderPath,
427426
aarOutputDir: pluginPlatformsFolderPath,
@@ -533,9 +532,6 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
533532

534533
public async removePluginNativeCode(pluginData: IPluginData, projectData: IProjectData): Promise<void> {
535534
try {
536-
// check whether the dependency that's being removed has native code
537-
// TODO: Remove prior to the 4.1 CLI release @Pip3r4o @PanayotCankov
538-
// the updated gradle script will take care of cleaning the prepared android plugins
539535
if (!this.runtimeVersionIsGreaterThanOrEquals(projectData, "3.3.0")) {
540536
const pluginConfigDir = path.join(this.getPlatformData(projectData).projectRoot, "configurations", pluginData.name);
541537
if (this.$fs.exists(pluginConfigDir)) {
@@ -563,8 +559,6 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
563559
if (shouldUseNewRoutine) {
564560
this.provideDependenciesJson(projectData, dependencies);
565561
} else {
566-
// TODO: Remove prior to the 4.1 CLI release @Pip3r4o @PanayotCankov
567-
568562
const platformDir = path.join(projectData.platformsDir, AndroidProjectService.ANDROID_PLATFORM_NAME);
569563
const buildDir = path.join(platformDir, "build-tools");
570564
const checkV8dependants = path.join(buildDir, "check-v8-dependants.js");
@@ -580,7 +574,6 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
580574
}
581575

582576
if (!shouldUseNewRoutine) {
583-
// TODO: Remove prior to the 4.0 CLI release @Pip3r4o @PanayotCankov
584577
const projectRoot = this.getPlatformData(projectData).projectRoot;
585578
await this.cleanProject(projectRoot, projectData);
586579
}

vendor/gradle-plugin/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:3.1.2'
8+
classpath 'com.android.tools.build:gradle:{{runtimeAndroidPluginVersion}}'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files

vendor/gradle-plugin/gradle/wrapper/gradle-wrapper.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-{{runtimeGradleVersion}}-bin.zip

0 commit comments

Comments
 (0)