Skip to content
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

feat(android): allow to force gradle version throught nativescript-co… #5764

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions lib/definitions/project.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ interface INsConfigAndroid extends INsConfigPlaform {
enableLineBreakpoints?: boolean;

enableMultithreadedJavascript?: boolean;

gradleVersion?: string;
}

interface INsConfigHooks {
Expand Down
30 changes: 28 additions & 2 deletions lib/services/android-plugin-build-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,18 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
this.$fs.copyFile(path.join(dir, "*"), destination);
}
}

private extractNamespaceFromManifest(manifestPath:string): string {
const fileContent = this.$fs.readText(manifestPath);
const contentRegex = new RegExp('package="(.*)"');
const match = fileContent.match(contentRegex);
let namespace: string;
if (match) {
namespace = match[1];
const replacedFileContent = fileContent.replace(contentRegex, "");
this.$fs.writeFile(manifestPath, replacedFileContent);
}
return namespace;
}
private async setupGradle(
pluginTempDir: string,
platformsAndroidDirPath: string,
Expand All @@ -413,14 +424,29 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
const runtimeGradleVersions = await this.getRuntimeGradleVersions(
projectDir
);
let gradleVersion = runtimeGradleVersions.gradleVersion;
if (this.$projectData.nsConfig.android.gradleVersion) {
gradleVersion = this.$projectData.nsConfig.android.gradleVersion;
}
this.replaceGradleVersion(
pluginTempDir,
runtimeGradleVersions.gradleVersion
gradleVersion
);
this.replaceGradleAndroidPluginVersion(
buildGradlePath,
runtimeGradleVersions.gradleAndroidPluginVersion
);

// In gradle 8 every android project must have a namespace in "android"
// and the package property in manifest is now forbidden
// let s replace it
const manifestFilePath = this.getManifest(path.join(pluginTempDir, 'src', 'main'));
let pluginNamespace = this.extractNamespaceFromManifest(manifestFilePath);
if (!pluginNamespace) {
pluginNamespace = pluginName.replace(/@/g, '').replace(/[/-]/g, '.')
}

this.replaceFileContent(buildGradlePath, "{{pluginNamespace}}", pluginNamespace);
this.replaceFileContent(buildGradlePath, "{{pluginName}}", pluginName);
this.replaceFileContent(settingsGradlePath, "{{pluginName}}", pluginName);
}
Expand Down
29 changes: 29 additions & 0 deletions lib/services/android-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,24 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
gradleSettingsFilePath
);


const gradleVersion = projectData.nsConfig.android.gradleVersion;
if (gradleVersion) {
// user defined a custom gradle version, let's apply it
const gradleWrapperFilePath = path.join(
this.getPlatformData(projectData).projectRoot,
"gradle",
"wrapper",
"gradle-wrapper.properties"
);
shell.sed(
"-i",
/gradle-([0-9.]+)-bin.zip/,
`gradle-${gradleVersion}-bin.zip`,
gradleWrapperFilePath
);
}

try {
// will replace applicationId in app/App_Resources/Android/app.gradle if it has not been edited by the user
const appGradleContent = this.$fs.readText(projectData.appGradlePath);
Expand Down Expand Up @@ -487,6 +505,17 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
projectData.projectIdentifiers.android,
manifestPath
);
const buildGradlePath = path.join(
this.getPlatformData(projectData).projectRoot,
"app",
"build.gradle"
);
shell.sed(
"-i",
/__PACKAGE__/,
projectData.projectIdentifiers.android,
buildGradlePath
);
}

private getProjectNameFromId(projectData: IProjectData): string {
Expand Down
40 changes: 25 additions & 15 deletions vendor/gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ buildscript {
}
def computeKotlinVersion = { -> project.hasProperty("kotlinVersion") ? kotlinVersion : "1.8.20" }
def kotlinVersion = computeKotlinVersion()
def computeBuildToolsVersion = { ->
project.hasProperty("androiBuildToolsVersion") ? buildToolsVersion : "{{runtimeAndroidPluginVersion}}"
}
def androiBuildToolsVersion = computeBuildToolsVersion()
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:{{runtimeAndroidPluginVersion}}'
classpath "com.android.tools.build:gradle:$androiBuildToolsVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"

// NOTE: Do not place your application dependencies here; they belong
Expand Down Expand Up @@ -152,6 +156,7 @@ def computeBuildToolsVersion = { ->
}

android {
namespace "{{pluginNamespace}}"
def applyPluginGradleConfigurations = { ->
nativescriptDependencies.each { dep ->
def includeGradlePath = "${getDepPlatformDir(dep)}/include.gradle"
Expand All @@ -166,6 +171,11 @@ android {
compileSdkVersion computeCompileSdkVersion()
buildToolsVersion computeBuildToolsVersion()

compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}

defaultConfig {
targetSdkVersion computeTargetSdkVersion()
versionCode 1
Expand Down Expand Up @@ -205,24 +215,24 @@ task addDependenciesFromNativeScriptPlugins {
}
}

afterEvaluate {
def generateBuildConfig = project.hasProperty("generateBuildConfig") ? project.generateBuildConfig : false
def generateR = project.hasProperty("generateR") ? project.generateR : false
generateReleaseBuildConfig.enabled = generateBuildConfig
generateDebugBuildConfig.enabled = generateBuildConfig
generateReleaseResValues.enabled = generateR
generateDebugResValues.enabled = generateR
}

tasks.whenTaskAdded({ DefaultTask currentTask ->
if (currentTask.name == 'bundleRelease' || currentTask.name == 'bundleDebug') {
// This wont work with gradle 8 + should be the same as the code just after
// afterEvaluate {
// def generateBuildConfig = project.hasProperty("generateBuildConfig") ? project.generateBuildConfig : false
// def generateR = project.hasProperty("generateR") ? project.generateR : false
// generateReleaseBuildConfig.enabled = generateBuildConfig
// generateDebugBuildConfig.enabled = generateBuildConfig
// generateReleaseResValues.enabled = generateR
// generateDebugResValues.enabled = generateR
// }
project.tasks.configureEach {
if (name == 'bundleRelease') {
def generateBuildConfig = project.hasProperty("generateBuildConfig") ? project.generateBuildConfig : false
def generateR = project.hasProperty("generateR") ? project.generateR : false
if (!generateBuildConfig) {
currentTask.exclude '**/BuildConfig.class'
it.exclude '**/BuildConfig.class'
}
if (!generateR) {
currentTask.exclude '**/R.class', '**/R$*.class'
it.exclude '**/R.class', '**/R$*.class'
}
}
})
}