-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
Changes from 6 commits
983c3b7
543ded6
9ae5b0a
9dcffbe
86f7f38
dcc78bf
1a881a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
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"]; | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
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}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be nice to add the message from real error here.
Also use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
); | ||
} | ||
|
||
// 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}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also we can add the real error message to the output. |
||
} | ||
} | ||
|
||
// 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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. after a further looking into that, it could be just |
||
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 = {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove this line and initialize the variable when declaring it. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
|
||
if (repositoriesAndDependenciesScopes.length > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
this.$fs.appendFile(buildGradlePath, "\n" + repositoriesAndDependenciesScopes.join("\n")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
} | ||
} | ||
} | ||
|
||
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}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
|
||
/** | ||
|
@@ -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 = [ | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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();)