Skip to content

feat: use runtime Gradle version during plugin AAR build #3731

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

Merged
merged 7 commits into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
8 changes: 8 additions & 0 deletions lib/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,3 +850,11 @@ interface IAssetsGenerationService {
*/
generateSplashScreens(splashesGenerationData: ISplashesGenerationData): Promise<void>;
}

/**
* Describes the Gradle versions specified in the package.json of the Android runtime
*/
interface IRuntimeGradleVersions {
gradleVersion?: string;
gradleAndroidPluginVersion?: string;
}
3 changes: 2 additions & 1 deletion lib/definitions/android-plugin-migrator.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

interface IBuildOptions extends IAndroidBuildOptions{
projectDir?: string
}

interface IAndroidBuildOptions {
Expand Down Expand Up @@ -33,5 +34,5 @@ interface IBuildAndroidPluginData {
/**
* Information about tools that will be used to build the plugin, for example compile SDK version, build tools version, etc.
*/
androidToolsInfo: IAndroidToolsInfoData;
androidToolsInfo?: IAndroidToolsInfoData;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this is not mandatory anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using it in a single place and now it defaults to the old logic inside (this.$androidToolsInfo.getToolsInfo();)

}
2 changes: 1 addition & 1 deletion lib/node-package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class NodePackageManager implements INodePackageManager {
const url = `https://registry.npmjs.org/${packageName}`;
this.$logger.trace(`Trying to get data from npm registry for package ${packageName}, url is: ${url}`);
const responseData = (await this.$httpClient.httpRequest(url)).body;
this.$logger.trace(`Successfully received data from npm registry for package ${packageName}. Response data is: ${responseData}`);
this.$logger.trace(`Successfully received data from npm registry for package ${packageName}.`);
const jsonData = JSON.parse(responseData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you delete response data from the log message? It is added here with debug purposes?

this.$logger.trace(`Successfully parsed data from npm registry for package ${packageName}.`);
return jsonData;
Expand Down
230 changes: 142 additions & 88 deletions lib/services/android-plugin-build-service.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
import * as path from "path";
import { MANIFEST_FILE_NAME, INCLUDE_GRADLE_NAME, ASSETS_DIR, RESOURCES_DIR } from "../constants";
import { MANIFEST_FILE_NAME, INCLUDE_GRADLE_NAME, ASSETS_DIR, RESOURCES_DIR, TNS_ANDROID_RUNTIME_NAME } from "../constants";
import { getShortPluginName, hook } from "../common/helpers";
import { Builder, parseString } from "xml2js";
import { ILogger } from "log4js";

export class AndroidPluginBuildService implements IAndroidPluginBuildService {

/**
* Required for hooks execution to work.
*/
private get $hooksService(): IHooksService {
return this.$injector.resolve("hooksService");
}

private get $platformService(): IPlatformService {
return this.$injector.resolve("platformService");
}

constructor(private $injector: IInjector,
private $fs: IFileSystem,
private $childProcess: IChildProcess,
private $hostInfo: IHostInfo,
private $androidToolsInfo: IAndroidToolsInfo,
private $logger: ILogger) { }
private $logger: ILogger,
private $npm: INodePackageManager,
private $projectDataService: IProjectDataService,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants) { }

private static MANIFEST_ROOT = {
$: {
"xmlns:android": "http://schemas.android.com/apk/res/android"
}
};
private static ANDROID_PLUGIN_GRADLE_TEMPLATE = "../../vendor/gradle-plugin";

private getAndroidSourceDirectories(source: string): Array<string> {
const directories = [RESOURCES_DIR, "java", ASSETS_DIR, "jniLibs"];
Expand Down Expand Up @@ -164,113 +169,157 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
*/
public async buildAar(options: IBuildOptions): Promise<boolean> {
this.validateOptions(options);
const manifestFilePath = this.getManifest(options.platformsAndroidDirPath);
const androidSourceSetDirectories = this.getAndroidSourceDirectories(options.platformsAndroidDirPath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe androidSourceDirectories is a better name.

const shouldBuildAar = !!manifestFilePath || androidSourceSetDirectories.length > 0;

if (shouldBuildAar) {
const shortPluginName = getShortPluginName(options.pluginName);
const pluginTempDir = path.join(options.tempPluginDirPath, shortPluginName);
const pluginTempMainSrcDir = path.join(pluginTempDir, "src", "main");

await this.updateManifest(manifestFilePath, pluginTempMainSrcDir, shortPluginName);
this.copySourceSetDirectories(androidSourceSetDirectories, pluginTempMainSrcDir);
await this.setupGradle(pluginTempDir, options.platformsAndroidDirPath, options.projectDir);
await this.buildPlugin({ pluginDir: pluginTempDir, pluginName: options.pluginName });
this.copyAar(shortPluginName, pluginTempDir, options.aarOutputDir);
}

// IDEA: Accept as an additional parameter the Android Support Library version from CLI, pass it on as -PsupportVersion later to the build
// IDEA: apply app.gradle here in order to get any and all user-defined variables
// IDEA: apply the entire include.gradle here instead of copying over the repositories {} and dependencies {} scopes
return shouldBuildAar;
}

const shortPluginName = getShortPluginName(options.pluginName);
const newPluginDir = path.join(options.tempPluginDirPath, shortPluginName);
const newPluginMainSrcDir = path.join(newPluginDir, "src", "main");
private async updateManifest(manifestFilePath: string, pluginTempMainSrcDir: string, shortPluginName: string): Promise<void> {
let updatedManifestContent;
this.$fs.ensureDirectoryExists(pluginTempMainSrcDir);
const defaultPackageName = "org.nativescript." + shortPluginName;
if (manifestFilePath) {
let androidManifestContent;
try {
androidManifestContent = this.$fs.readText(manifestFilePath);
} catch (err) {
throw new Error(
`Failed to fs.readFileSync the manifest file located at ${manifestFilePath}`
Copy link
Contributor

@Fatme Fatme Jul 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be nice to add the message from real error here.

`Failed to fs.readFileSync the manifest file located at ${manifestFilePath}`

Also use $errors.failWithoutHelp

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use this.$errors.fail or this.$erros.failWithoutHelp instead throw new Error

);
}

// find manifest file
//// prepare manifest file content
const manifestFilePath = this.getManifest(options.platformsAndroidDirPath);
let shouldBuildAar = false;
updatedManifestContent = await this.updateManifestContent(androidManifestContent, defaultPackageName);
} else {
updatedManifestContent = this.createManifestContent(defaultPackageName);
}

// look for AndroidManifest.xml
if (manifestFilePath) {
shouldBuildAar = true;
const pathToTempAndroidManifest = path.join(pluginTempMainSrcDir, MANIFEST_FILE_NAME);
try {
this.$fs.writeFile(pathToTempAndroidManifest, updatedManifestContent);
} catch (e) {
throw new Error(`Failed to write the updated AndroidManifest in the new location - ${pathToTempAndroidManifest}`);
Copy link
Contributor

@Fatme Fatme Jul 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we can add the real error message to the output.
Use this.$errors.fail

}
}

// look for android resources
const androidSourceSetDirectories = this.getAndroidSourceDirectories(options.platformsAndroidDirPath);
private copySourceSetDirectories(androidSourceSetDirectories: string[], pluginTempMainSrcDir: string): void {
for (const dir of androidSourceSetDirectories) {
const dirNameParts = dir.split(path.sep);
// get only the last subdirectory of the entire path string. e.g. 'res', 'java', etc.
const dirName = dirNameParts[dirNameParts.length - 1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use path.dirname(path.dirname(dir))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after a further looking into that, it could be just path.basename(dir)

const destination = path.join(pluginTempMainSrcDir, dirName);

if (androidSourceSetDirectories.length > 0) {
shouldBuildAar = true;
this.$fs.ensureDirectoryExists(destination);
this.$fs.copyFile(path.join(dir, "*"), destination);
}
}

// if a manifest OR/AND resource files are present - write files, build plugin
if (shouldBuildAar) {
let updatedManifestContent;
this.$fs.ensureDirectoryExists(newPluginMainSrcDir);

if (manifestFilePath) {
let androidManifestContent;
try {
androidManifestContent = this.$fs.readText(manifestFilePath);
} catch (err) {
throw new Error(
`Failed to fs.readFileSync the manifest file located at ${manifestFilePath}`
);
}
private async setupGradle(pluginTempDir: string, platformsAndroidDirPath: string, projectDir: string): Promise<void> {
const gradleTemplatePath = path.resolve(path.join(__dirname, "../../vendor/gradle-plugin"));
const allGradleTemplateFiles = path.join(gradleTemplatePath, "*");
const buildGradlePath = path.join(pluginTempDir, "build.gradle");

updatedManifestContent = await this.updateManifestContent(androidManifestContent, defaultPackageName);
} else {
updatedManifestContent = this.createManifestContent(defaultPackageName);
}
this.$fs.copyFile(allGradleTemplateFiles, pluginTempDir);
this.addCompileDependencies(platformsAndroidDirPath, buildGradlePath);
const runtimeGradleVersions = await this.getRuntimeGradleVersions(projectDir);
this.replaceGradleVersion(pluginTempDir, runtimeGradleVersions.gradleVersion);
this.replaceGradleAndroidPluginVersion(buildGradlePath, runtimeGradleVersions.gradleAndroidPluginVersion);
}

// write the AndroidManifest in the temp-dir/plugin-name/src/main
const pathToNewAndroidManifest = path.join(newPluginMainSrcDir, MANIFEST_FILE_NAME);
try {
this.$fs.writeFile(pathToNewAndroidManifest, updatedManifestContent);
} catch (e) {
throw new Error(`Failed to write the updated AndroidManifest in the new location - ${pathToNewAndroidManifest}`);
}
private async getRuntimeGradleVersions(projectDir: string): Promise<IRuntimeGradleVersions> {
const registryData = await this.$npm.getRegistryPackageData(TNS_ANDROID_RUNTIME_NAME);
let runtimeGradleVersions: IRuntimeGradleVersions = null;
if (projectDir) {
const projectRuntimeVersion = this.$platformService.getCurrentPlatformVersion(
this.$devicePlatformsConstants.Android,
this.$projectDataService.getProjectData(projectDir));
runtimeGradleVersions = this.getGradleVersions(registryData.versions[projectRuntimeVersion]);
this.$logger.trace(`Got gradle versions ${JSON.stringify(runtimeGradleVersions)} from runtime v${projectRuntimeVersion}`);
}

// copy all android sourceset directories to the new temporary plugin dir
for (const dir of androidSourceSetDirectories) {
// get only the last subdirectory of the entire path string. e.g. 'res', 'java', etc.
const dirNameParts = dir.split(path.sep);
const dirName = dirNameParts[dirNameParts.length - 1];
if (!runtimeGradleVersions) {
const latestRuntimeVersion = registryData["dist-tags"].latest;
runtimeGradleVersions = this.getGradleVersions(registryData.versions[latestRuntimeVersion]);
this.$logger.trace(`Got gradle versions ${JSON.stringify(runtimeGradleVersions)} from the latest runtime v${latestRuntimeVersion}`);
}

const destination = path.join(newPluginMainSrcDir, dirName);
this.$fs.ensureDirectoryExists(destination);
return runtimeGradleVersions || {};
}

this.$fs.copyFile(path.join(dir, "*"), destination);
}
private getGradleVersions(packageData: { gradle: { version: string, android: string }}): IRuntimeGradleVersions {
const packageJsonGradle = packageData && packageData.gradle;
let runtimeVersions: IRuntimeGradleVersions = null;
if (packageJsonGradle && (packageJsonGradle.version || packageJsonGradle.android)) {
runtimeVersions = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this line and initialize the variable when declaring it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its here as we want to return null in such cases

runtimeVersions.gradleVersion = packageJsonGradle.version;
runtimeVersions.gradleAndroidPluginVersion = packageJsonGradle.android;
}

// copy the preconfigured gradle android library project template to the temporary android library
this.$fs.copyFile(path.join(path.resolve(path.join(__dirname, AndroidPluginBuildService.ANDROID_PLUGIN_GRADLE_TEMPLATE), "*")), newPluginDir);
return runtimeVersions;
}

// sometimes the AndroidManifest.xml or certain resources in /res may have a compile dependency to a library referenced in include.gradle. Make sure to compile the plugin with a compile dependency to those libraries
const includeGradlePath = path.join(options.platformsAndroidDirPath, INCLUDE_GRADLE_NAME);
if (this.$fs.exists(includeGradlePath)) {
const includeGradleContent = this.$fs.readText(includeGradlePath);
const repositoriesAndDependenciesScopes = this.getIncludeGradleCompileDependenciesScope(includeGradleContent);
private replaceGradleVersion(pluginTempDir: string, version: string): void {
const gradleVersion = version || "4.4";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4.4 can be extracted to constant

const gradleVersionPlaceholder = "{{runtimeGradleVersion}}";
const gradleWrapperPropertiesPath = path.join(pluginTempDir, "gradle", "wrapper", "gradle-wrapper.properties");

// dependencies { } object was found - append dependencies scope
if (repositoriesAndDependenciesScopes.length > 0) {
const buildGradlePath = path.join(newPluginDir, "build.gradle");
this.$fs.appendFile(buildGradlePath, "\n" + repositoriesAndDependenciesScopes.join("\n"));
}
}
this.replaceFileContent(gradleWrapperPropertiesPath, gradleVersionPlaceholder, gradleVersion);
}

// finally build the plugin
this.$androidToolsInfo.validateInfo({ showWarningsAsErrors: true, validateTargetSdk: true });
const androidToolsInfo = this.$androidToolsInfo.getToolsInfo();
await this.buildPlugin( { pluginDir: newPluginDir, pluginName: options.pluginName, androidToolsInfo });

const finalAarName = `${shortPluginName}-release.aar`;
const pathToBuiltAar = path.join(newPluginDir, "build", "outputs", "aar", finalAarName);

if (this.$fs.exists(pathToBuiltAar)) {
try {
if (options.aarOutputDir) {
this.$fs.copyFile(pathToBuiltAar, path.join(options.aarOutputDir, `${shortPluginName}.aar`));
}
} catch (e) {
throw new Error(`Failed to copy built aar to destination. ${e.message}`);
}
private replaceGradleAndroidPluginVersion(buildGradlePath: string, version: string): void {
const gradleAndroidPluginVersionPlaceholder = "{{runtimeAndroidPluginVersion}}";
const gradleAndroidPluginVersion = version || "3.1.2";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can be extracted to constant - 3.1.2


return true;
} else {
throw new Error(`No built aar found at ${pathToBuiltAar}`);
this.replaceFileContent(buildGradlePath, gradleAndroidPluginVersionPlaceholder, gradleAndroidPluginVersion);
}

private replaceFileContent(filePath: string, content: string, replacement: string) {
const fileContent = this.$fs.readText(filePath);
const contentRegex = new RegExp(content, "g");
const replacedFileContent = fileContent.replace(contentRegex, replacement);
this.$fs.writeFile(filePath, replacedFileContent);
}

private addCompileDependencies(platformsAndroidDirPath: string, buildGradlePath: string): void {
const includeGradlePath = path.join(platformsAndroidDirPath, INCLUDE_GRADLE_NAME);
if (this.$fs.exists(includeGradlePath)) {
const includeGradleContent = this.$fs.readText(includeGradlePath);
const repositoriesAndDependenciesScopes = this.getIncludeGradleCompileDependenciesScope(includeGradleContent);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe compileDependencies is a better name. It is not a good practice to have and in the name.


if (repositoriesAndDependenciesScopes.length > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repositoriesAndDependenciesScopes.length is enough

this.$fs.appendFile(buildGradlePath, "\n" + repositoriesAndDependenciesScopes.join("\n"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe EOL is better than \n

}
}
}

return false;
private copyAar(shortPluginName: string, pluginTempDir: string, aarOutputDir: string): void {
const finalAarName = `${shortPluginName}-release.aar`;
const pathToBuiltAar = path.join(pluginTempDir, "build", "outputs", "aar", finalAarName);

if (this.$fs.exists(pathToBuiltAar)) {
try {
if (aarOutputDir) {
this.$fs.copyFile(pathToBuiltAar, path.join(aarOutputDir, `${shortPluginName}.aar`));
}
} catch (e) {
throw new Error(`Failed to copy built aar to destination. ${e.message}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add the real error message to the output.

}
} else {
throw new Error(`No built aar found at ${pathToBuiltAar}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errors.fail()

}
}

/**
Expand Down Expand Up @@ -307,6 +356,11 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {

@hook("buildAndroidPlugin")
private async buildPlugin(pluginBuildSettings: IBuildAndroidPluginData): Promise<void> {
if (!pluginBuildSettings.androidToolsInfo) {
this.$androidToolsInfo.validateInfo({ showWarningsAsErrors: true, validateTargetSdk: true });
pluginBuildSettings.androidToolsInfo = this.$androidToolsInfo.getToolsInfo();
}

const gradlew = this.$hostInfo.isWindows ? "gradlew.bat" : "./gradlew";

const localArgs = [
Expand Down
11 changes: 2 additions & 9 deletions lib/services/android-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
return this._platformData;
}

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

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

public async removePluginNativeCode(pluginData: IPluginData, projectData: IProjectData): Promise<void> {
try {
// check whether the dependency that's being removed has native code
// TODO: Remove prior to the 4.1 CLI release @Pip3r4o @PanayotCankov
// the updated gradle script will take care of cleaning the prepared android plugins
if (!this.runtimeVersionIsGreaterThanOrEquals(projectData, "3.3.0")) {
const pluginConfigDir = path.join(this.getPlatformData(projectData).projectRoot, "configurations", pluginData.name);
if (this.$fs.exists(pluginConfigDir)) {
Expand Down Expand Up @@ -563,8 +559,6 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
if (shouldUseNewRoutine) {
this.provideDependenciesJson(projectData, dependencies);
} else {
// TODO: Remove prior to the 4.1 CLI release @Pip3r4o @PanayotCankov

const platformDir = path.join(projectData.platformsDir, AndroidProjectService.ANDROID_PLATFORM_NAME);
const buildDir = path.join(platformDir, "build-tools");
const checkV8dependants = path.join(buildDir, "check-v8-dependants.js");
Expand All @@ -580,7 +574,6 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
}

if (!shouldUseNewRoutine) {
// TODO: Remove prior to the 4.0 CLI release @Pip3r4o @PanayotCankov
const projectRoot = this.getPlatformData(projectData).projectRoot;
await this.cleanProject(projectRoot, projectData);
}
Expand Down
Loading