Skip to content

Commit 3a86383

Browse files
authored
Merge pull request #1280 from NativeScript/trifonov/buildscript-gradle
Add support for buildscript.gradle files in app and plugins
2 parents 3a84002 + 5de5e09 commit 3a86383

1 file changed

Lines changed: 102 additions & 78 deletions

File tree

test-app/app/build.gradle

Lines changed: 102 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ if (PASSED_TYPINGS_PATH != null) {
3636
TYPINGS_PATH = PASSED_TYPINGS_PATH
3737
}
3838

39-
def USER_PROJECT_ROOT = "$rootDir/../.."
40-
def PLATFORMS_ANDROID = "platforms/android"
4139
def PACKAGE_JSON = "package.json"
4240

4341
//static binding generator
@@ -59,59 +57,86 @@ def METADATA_OUT_PATH = "$projectDir/src/main/assets/metadata"
5957
def pluginsJarLibraries = new LinkedList<String>()
6058
def allJarLibraries = new LinkedList<String>()
6159

62-
// the build script will not work with previous versions of the CLI (3.1 or earlier)
63-
def dependenciesJson = file("$rootDir/dependencies.json")
64-
if (!dependenciesJson.exists()) {
65-
throw new BuildCancelledException("""
66-
'dependencies.json' file not found. Check whether the NativeScript CLI has prepared the project beforehand,
67-
and that your NativeScript version is 3.3, or a more recent one. To build an android project with the current
68-
version of the {N} CLI install a previous version of the runtime package - 'tns platform add android@3.2'.
69-
""")
70-
}
71-
72-
project.ext.extractedDependenciesDir = "${project.buildDir}/exploded-dependencies"
73-
def nativescriptDependencies = new JsonSlurper().parseText(dependenciesJson.text)
74-
7560
def computeCompileSdkVersion = { -> project.hasProperty("compileSdk") ? compileSdk : 28 }
7661
def computeTargetSdkVersion = { -> project.hasProperty("targetSdk") ? targetSdk : 28 }
7762
def computeBuildToolsVersion = { ->
7863
project.hasProperty("buildToolsVersion") ? buildToolsVersion : "28.0.3"
7964
}
8065

8166
project.ext.selectedBuildType = project.hasProperty("release") ? "release" : "debug"
82-
project.ext.appResourcesPath = ""
8367

84-
////////////////////////////////////////////////////////////////////////////////////
85-
///////////////////////////// CONFIGURATIONS ///////////////////////////////////////
86-
////////////////////////////////////////////////////////////////////////////////////
68+
buildscript {
69+
// we have moved these initializations here as we need getAppResourcesPath to search for buildscript.gradle files,
70+
// but the buildscript is executed before executing anything else from the gradle file
71+
def initialize = { ->
72+
// the build script will not work with previous versions of the CLI (3.1 or earlier)
73+
def dependenciesJson = file("$rootDir/dependencies.json")
74+
if (!dependenciesJson.exists()) {
75+
throw new BuildCancelledException("""
76+
'dependencies.json' file not found. Check whether the NativeScript CLI has prepared the project beforehand,
77+
and that your NativeScript version is 3.3, or a more recent one. To build an android project with the current
78+
version of the {N} CLI install a previous version of the runtime package - 'tns platform add android@3.2'.
79+
""")
80+
}
8781

88-
def getAppResourcesPath = { ->
89-
def relativePathToApp = "app"
90-
def relativePathToAppResources
91-
def absolutePathToAppResources
92-
def nsConfigFile = file("$USER_PROJECT_ROOT/nsconfig.json")
93-
def nsConfig
82+
project.ext.extractedDependenciesDir = "${project.buildDir}/exploded-dependencies"
83+
project.ext.nativescriptDependencies = new JsonSlurper().parseText(dependenciesJson.text)
84+
project.ext.PLATFORMS_ANDROID = "platforms/android"
85+
project.ext.USER_PROJECT_ROOT = "$rootDir/../.."
9486

95-
if (nsConfigFile.exists()) {
96-
nsConfig = new JsonSlurper().parseText(nsConfigFile.getText("UTF-8"))
97-
}
87+
project.ext.getAppResourcesPath = { ->
88+
def relativePathToApp = "app"
89+
def relativePathToAppResources
90+
def absolutePathToAppResources
91+
def nsConfigFile = file("$USER_PROJECT_ROOT/nsconfig.json")
92+
def nsConfig
9893

99-
if(nsConfig != null && nsConfig.appPath != null){
100-
relativePathToApp = nsConfig.appPath
101-
}
94+
if (nsConfigFile.exists()) {
95+
nsConfig = new JsonSlurper().parseText(nsConfigFile.getText("UTF-8"))
96+
}
10297

103-
if(nsConfig != null && nsConfig.appResourcesPath != null ) {
104-
relativePathToAppResources = nsConfig.appResourcesPath
105-
} else {
106-
relativePathToAppResources = "$relativePathToApp/App_Resources"
107-
}
98+
if(nsConfig != null && nsConfig.appPath != null){
99+
relativePathToApp = nsConfig.appPath
100+
}
101+
102+
if(nsConfig != null && nsConfig.appResourcesPath != null ) {
103+
relativePathToAppResources = nsConfig.appResourcesPath
104+
} else {
105+
relativePathToAppResources = "$relativePathToApp/App_Resources"
106+
}
108107

109-
absolutePathToAppResources = java.nio.file.Paths.get(USER_PROJECT_ROOT).resolve(relativePathToAppResources).toAbsolutePath()
108+
absolutePathToAppResources = java.nio.file.Paths.get(USER_PROJECT_ROOT).resolve(relativePathToAppResources).toAbsolutePath()
110109

111-
project.ext.appResourcesPath = absolutePathToAppResources
110+
project.ext.appResourcesPath = absolutePathToAppResources
112111

113-
return absolutePathToAppResources
112+
return absolutePathToAppResources
113+
}
114+
}
115+
def applyBuildScriptConfigurations = { ->
116+
def absolutePathToAppResources = getAppResourcesPath()
117+
def pathToBuildScriptGradle = "$absolutePathToAppResources/Android/buildscript.gradle"
118+
def buildScriptGradle = file(pathToBuildScriptGradle)
119+
if (buildScriptGradle.exists()) {
120+
println "\t + applying user-defined buildscript from ${buildScriptGradle}"
121+
apply from: pathToBuildScriptGradle, to: buildscript
122+
}
123+
124+
nativescriptDependencies.each {dep ->
125+
def pathToPluginBuildScriptGradle = "$rootDir/${dep.directory}/$PLATFORMS_ANDROID/buildscript.gradle"
126+
def pluginBuildScriptGradle = file(pathToPluginBuildScriptGradle)
127+
if(pluginBuildScriptGradle.exists()) {
128+
println "\t + applying user-defined buildscript from dependency ${pluginBuildScriptGradle}"
129+
apply from: pathToPluginBuildScriptGradle, to: buildscript
130+
}
131+
}
132+
}
133+
134+
initialize()
135+
applyBuildScriptConfigurations()
114136
}
137+
////////////////////////////////////////////////////////////////////////////////////
138+
///////////////////////////// CONFIGURATIONS ///////////////////////////////////////
139+
////////////////////////////////////////////////////////////////////////////////////
115140

116141
def applyBeforePluginGradleConfiguration = { ->
117142
def appResourcesPath = getAppResourcesPath()
@@ -145,34 +170,33 @@ def applyPluginGradleConfigurations = { ->
145170
}
146171

147172
def getAppIdentifier = { packageJsonMap ->
148-
def appIdentifier = ""
149-
if (packageJsonMap && packageJsonMap.nativescript) {
150-
appIdentifier = packageJsonMap.nativescript.id
151-
if (!(appIdentifier instanceof String)) {
152-
appIdentifier = appIdentifier.android
153-
}
154-
}
155-
156-
return appIdentifier
173+
def appIdentifier = ""
174+
if (packageJsonMap && packageJsonMap.nativescript) {
175+
appIdentifier = packageJsonMap.nativescript.id
176+
if (!(appIdentifier instanceof String)) {
177+
appIdentifier = appIdentifier.android
178+
}
179+
}
180+
181+
return appIdentifier
157182
}
158183

159184
def setAppIdentifier = { ->
160-
println "\t + setting applicationId"
161-
File packageJsonFile = new File("$rootDir/../../package.json")
162-
163-
if (packageJsonFile.exists()) {
164-
def content = packageJsonFile.getText("UTF-8")
165-
def jsonSlurper = new JsonSlurper()
166-
def packageJsonMap = jsonSlurper.parseText(content)
167-
def appIdentifier = getAppIdentifier(packageJsonMap)
168-
169-
if (appIdentifier) {
170-
project.ext.nsApplicationIdentifier = appIdentifier
171-
android.defaultConfig.applicationId = appIdentifier
172-
}
173-
}
174-
}
185+
println "\t + setting applicationId"
186+
File packageJsonFile = new File("$rootDir/../../package.json")
187+
188+
if (packageJsonFile.exists()) {
189+
def content = packageJsonFile.getText("UTF-8")
190+
def jsonSlurper = new JsonSlurper()
191+
def packageJsonMap = jsonSlurper.parseText(content)
192+
def appIdentifier = getAppIdentifier(packageJsonMap)
175193

194+
if (appIdentifier) {
195+
project.ext.nsApplicationIdentifier = appIdentifier
196+
android.defaultConfig.applicationId = appIdentifier
197+
}
198+
}
199+
}
176200

177201
android {
178202
compileSdkVersion computeCompileSdkVersion()
@@ -659,21 +683,21 @@ copyTypings.onlyIf { generateTypescriptDefinitions.didWork }
659683
generateTypescriptDefinitions.finalizedBy(copyTypings)
660684

661685
task validateAppIdMatch {
662-
doLast {
663-
def lineSeparator = System.getProperty("line.separator")
664-
665-
if (project.hasProperty("nsApplicationIdentifier") && !project.hasProperty("release")) {
666-
if(project.nsApplicationIdentifier != android.defaultConfig.applicationId) {
667-
def errorMessage = "${lineSeparator}WARNING: The Application identifier is different from the one inside \"package.json\" file.$lineSeparator" +
668-
"NativeScript CLI might not work properly.$lineSeparator" +
669-
"Remove applicationId from app.gradle and update the \"nativescript.id\" in package.json.$lineSeparator" +
670-
"Actual: ${android.defaultConfig.applicationId}$lineSeparator" +
671-
"Expected(from \"package.json\"): ${project.nsApplicationIdentifier}$lineSeparator"
672-
673-
logger.error(errorMessage)
674-
}
675-
}
676-
}
686+
doLast {
687+
def lineSeparator = System.getProperty("line.separator")
688+
689+
if (project.hasProperty("nsApplicationIdentifier") && !project.hasProperty("release")) {
690+
if(project.nsApplicationIdentifier != android.defaultConfig.applicationId) {
691+
def errorMessage = "${lineSeparator}WARNING: The Application identifier is different from the one inside \"package.json\" file.$lineSeparator" +
692+
"NativeScript CLI might not work properly.$lineSeparator" +
693+
"Remove applicationId from app.gradle and update the \"nativescript.id\" in package.json.$lineSeparator" +
694+
"Actual: ${android.defaultConfig.applicationId}$lineSeparator" +
695+
"Expected(from \"package.json\"): ${project.nsApplicationIdentifier}$lineSeparator"
696+
697+
logger.error(errorMessage)
698+
}
699+
}
700+
}
677701
}
678702

679703
////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)