44* gradle buildapk -Prelease (release mode)
55* gradle buildapk (debug mode -> default)
66* Options:
7- * -Prelease
8- * -PtargetSdk=[target_sdk] (default is 22)
9- * -PbuildToolsVersion=[build_tools_version] (default is 22.0.1)
10- * -PsupportVersion=[support_version] (default (22.2.0)
11- * -PcompileSdk=[compile_sdk_version] (default 22)
12- *
7+ * -Prelease //this flag will run build in release mode
138* -PksPath=[path_to_keystore_file]
149* -PksPassword=[password_for_keystore_file]
1510* -Palias=[alias_to_use_from_keystore_file]
1611* -Ppassword=[password_for_alias]
1712*
18- * -PrunSBGenerator
13+ * -PtargetSdk=[target_sdk] (default is 22)
14+ * -PbuildToolsVersion=[build_tools_version] (default is 22.0.1)
15+ * -PsupportVersion=[support_version] (default (22.2.0)
16+ * -PcompileSdk=[compile_sdk_version] (default 22)
17+ *
18+ * -PrunSBGenerator //this flag will triger static binding generation
1919*/
2020
21- def isWinOs = System . properties[' os.name' ]. toLowerCase(). contains(' windows' )
22- def metadataParams = new LinkedList <String > ()
23- def allJarPaths = new LinkedList <String > ()
24- def configurationsDir = " configurations"
25-
2621buildscript {
2722 repositories {
2823 jcenter()
@@ -35,6 +30,15 @@ buildscript {
3530
3631apply plugin : " com.android.application"
3732
33+ def isWinOs = System . properties[' os.name' ]. toLowerCase(). contains(' windows' )
34+ def metadataParams = new LinkedList <String > ()
35+ def allJarPaths = new LinkedList <String > ()
36+ def configurationsDir = " configurations"
37+ def createPluginConfigFile = false
38+ def configStage = " \n :config phase: "
39+ def nodeModulesDir = " ../../node_modules/"
40+ def pluginNames = new ArrayList<String > ()
41+
3842def compiteCompileSdkVersion () {
3943 if (project. hasProperty(" compileSdk" )) {
4044 return compileSdk
@@ -79,6 +83,11 @@ def renameResultApks (variant) {
7983 }
8084}
8185
86+
87+ // //////////////////////////////////////////////////////////////////////////////////
88+ // /////////////////////////// CONFIGURATIONS ///////////////////////////////////////
89+ // //////////////////////////////////////////////////////////////////////////////////
90+
8291android {
8392 compileSdkVersion compiteCompileSdkVersion()
8493 buildToolsVersion computeBuildToolsVersion()
@@ -117,13 +126,13 @@ android {
117126 applicationVariants. all { variant ->
118127 renameResultApks(variant)
119128
120- def tsk = variant. mergeAssets
129+ def mergeAssetsTask = variant. mergeAssets
121130 if (project. hasProperty(" runSBGenerator" )) {
122131 runBindingGenerator. dependsOn(copyMetadata)
123- tsk . dependsOn(runBindingGenerator)
132+ mergeAssetsTask . dependsOn(runBindingGenerator)
124133 }
125134 else {
126- tsk . dependsOn(copyMetadata)
135+ mergeAssetsTask . dependsOn(copyMetadata)
127136 }
128137 }
129138}
@@ -136,8 +145,6 @@ repositories {
136145 }
137146}
138147
139- def nodeModulesDir = " ../../node_modules/"
140-
141148dependencies {
142149 def suppotVer = " 22.2.0" ;
143150 if (project. hasProperty(" supportVersion" )) {
@@ -157,10 +164,16 @@ dependencies {
157164 // compile files("$rootDir/libs/aar") {
158165 // builtBy 'copyAarDependencies'
159166 // }
167+
160168}
161169
162170
171+ // //////////////////////////////////////////////////////////////////////////////////
172+ // /////////////////////////// CONFIGURATION PHASE //////////////////////////////////
173+ // //////////////////////////////////////////////////////////////////////////////////
174+
163175def createIncludeFile (filePath , fileName ) {
176+ println " \t +creating include.gradle file for: " + filePath
164177 def defaultIncludeFile = new File (filePath, " include.gradle" )
165178 defaultIncludeFile. write " "
166179 defaultIncludeFile << " android { \n "
@@ -173,10 +186,15 @@ def createIncludeFile (filePath, fileName) {
173186}
174187
175188task createDefaultIncludeFiles {
189+ description " creates default include.gradle files for added plugins if necessary"
190+ println " $configStage createDefaultIncludeFiles"
176191 def ft = file(configurationsDir)
177192
178193 ft. listFiles(). each { fl ->
179194 if (fl. isDirectory()) {
195+ createPluginConfigFile = true
196+ pluginNames. add(' "' + fl. name + ' "' )
197+
180198 def foundIncludeFile = false
181199 fl. listFiles(). each { subFile ->
182200 if (subFile. name == " include.gradle" ) {
@@ -185,79 +203,105 @@ task createDefaultIncludeFiles {
185203 }
186204
187205 if (! foundIncludeFile) {
188- println " +creating include.gradle file for: " + fl. getAbsolutePath()
189206 createIncludeFile(fl. getAbsolutePath() ,fl. name)
190207 }
191208 }
192209 }
193210}
194211
195- task deletePrevConfig (type : Delete ) {
196- delete " $configurationsDir /include.gradle"
197- }
198-
199- // creates product flavor config file
200- task setFlavorsBeforePluginExtend {
201- def configDir = file(configurationsDir)
202- if (configDir. exists()) {
203- def dirTree = configDir. listFiles()
204- List<String > pluginNames = new ArrayList<String > ()
205- dirTree. each { f ->
206- if (f. isDirectory()) {
207- println " +adding plugin: " + f. getName()
208- pluginNames. add(' "' + f. getName() + ' "' )
209- }
210- }
212+ task createPluginsConfigFile {
213+ description " creates product flavor config file based on what plugins are added"
214+ println " $configStage createPluginsConfigFile"
215+
216+ def flavorsFile = new File (" $configurationsDir /include.gradle" )
217+ flavorsFile. write " " // clear config file
218+
219+ if (createPluginConfigFile) {
220+ println " \t +creating product flavors include.gradle file in $configurationsDir folder..."
211221 def flavors = pluginNames. join(" ," )
212-
213- println " creating product flavors include.gradle file in $configurationsDir folder..."
214- def flavorsFile = new File (" $configurationsDir /include.gradle" )
215- flavorsFile. write " "
222+ println " \t +found plugins: " + flavors
216223 flavorsFile << " android { \n "
217224 flavorsFile << " \t flavorDimensions " + flavors + " \n "
218225 flavorsFile << " }\n "
219226 }
220227}
221228
222- // applies additional configuration
223229task pluginExtend {
230+ description " applies additional configuration"
231+ println " $configStage pluginExtend"
224232 def configDir = file(configurationsDir)
225233 if (configDir. exists()) {
226234 configDir. eachFileRecurse(groovy.io.FileType . FILES ) {
227235 if (it. name. equals(' include.gradle' )) {
228- println " including plugin name : " + it
236+ println " \t +applying configuration from : " + it
229237 apply from : it
230238 }
231239 }
232240 }
241+ else {
242+ println " \t +ERROR: there is no configurations dir"
243+ }
233244}
234245
246+ // // doesn't work unless task is explicitly called (TODO: research configurations hook)
247+ // addAarDependencies.dependsOn(copyAarDependencies)
248+ // createDefaultIncludeFiles.dependsOn(addAarDependencies)
249+ // createPluginsConfigFile.dependsOn(createDefaultIncludeFiles)
250+ // pluginExtend.dependsOn(createPluginsConfigFile)
235251
236- task copyAarDependencies (type : Copy ) {
237- // we need to copy all dependencies into a flat dir, as pointed by the repositories setup
252+
253+ // //////////////////////////////////////////////////////////////////////////////////
254+ // /////////////////////////// BEFORE EXECUTION /////////////////////////////////////
255+ // //////////////////////////////////////////////////////////////////////////////////
256+
257+ // we need to copy all dependencies into a flat dir, as pointed by the repositories configurations at the top
258+ task copyAarDependencies (type : Copy ) {
259+ println " $configStage copyAarDependencies"
238260 from fileTree(dir : nodeModulesDir, include : [" **/*.aar" ]). files
239261 into " libs/aar"
240262}
241263
242- task addAarDependencies << /* doLast*/ {
243- // manually traverse all the locally copied AAR files and add them to the dependency list
264+ task addAarDependencies << {
265+ println " $configStage addAarDependencies"
266+ // manually traverse all the locally copied AAR files and add them to the project compilation dependencies list
244267 FileTree tree = fileTree(dir : " libs/aar" , include : [" **/*.aar" ])
245268 tree. each { File file ->
246269 // remove the extension of the file (.aar)
247270 def length = file. name. length() - 4
248271 def fileName = file. name[0 .. < length]
272+ println " \t +adding dependency: " + file. getAbsolutePath()
249273 project. dependencies. add(" compile" , [name : fileName, ext : " aar" ])
250274 }
251275}
252276
277+
278+ // //////////////////////////////////////////////////////////////////////////////////
279+ // /////////////////////////// EXECUTUION PHASE /////////////////////////////////////
280+ // //////////////////////////////////////////////////////////////////////////////////
281+
282+ task cleanLocalAarFiles (type : Delete ) {
283+ delete fileTree(dir : " libs/aar" , include : [" *.aar" ])
284+ }
285+
286+ task deleteJavaDir (type : Delete ) {
287+ delete " $rootDir /src/main/java"
288+ }
289+
290+ task ensureMetadataOutDir {
291+ def outputDir = file(" $rootDir /metadata/output" )
292+ outputDir. mkdirs()
293+ }
294+
253295task collectAllJars {
296+ description " gathers all paths to jar dependencies before building metadata with them"
297+
254298 def explodedAarDir = project. buildDir. getAbsolutePath() + " /intermediates/exploded-aar/"
255299 def sdkPath = android. sdkDirectory. getAbsolutePath();
256300 def androidJar = sdkPath + " /platforms/" + android. compileSdkVersion + " /android.jar"
257301
258302 doFirst {
259-
260303 configurations. compile. each { File dependencyFile ->
304+ // println "\t+" + dependencyFile.getAbsolutePath()
261305 allJarPaths. add(dependencyFile. getAbsolutePath())
262306 }
263307
@@ -277,6 +321,7 @@ task collectAllJars {
277321}
278322
279323task isMetadataGenerationNecessary {
324+ description " checks if new metadata is necessary based on jar and aar dependencies used"
280325 def inputFile = new File (" build/metadataUTD" )
281326
282327 inputs. file inputFile
@@ -290,16 +335,8 @@ task isMetadataGenerationNecessary {
290335 }
291336}
292337
293- isMetadataGenerationNecessary. dependsOn(collectAllJars)
294-
295- task ensureMetadataOutDir {
296- def outputDir = file(" $rootDir /metadata/output" )
297- outputDir. mkdirs()
298- }
299-
300-
301338task buildMetadata (type : JavaExec ) {
302-
339+ description " builds metadata with provided jar dependencies "
303340 outputs. upToDateWhen {
304341 ! isMetadataGenerationNecessary. didWork
305342 }
@@ -314,6 +351,7 @@ task buildMetadata (type: JavaExec) {
314351}
315352
316353task copyMetadata (type : Copy ) {
354+ description " moves metadata to the right place before packaging is done"
317355 outputs. upToDateWhen {
318356 ! buildMetadata. didWork
319357 }
@@ -322,6 +360,25 @@ task copyMetadata(type: Copy) {
322360 into " src/main/assets/metadata"
323361}
324362
363+
364+ task deleteExplodedAarFolder (type : Delete ) {
365+ // done to build only necessary apk
366+ if (project. hasProperty(" release" )) {
367+ dependsOn assembleRelease
368+ }
369+ else {
370+ dependsOn assembleDebug
371+ }
372+
373+ def explodedAarDir = project. buildDir. getAbsolutePath() + " /intermediates/exploded-aar"
374+ delete explodedAarDir
375+ }
376+
377+
378+ // //////////////////////////////////////////////////////////////////////////////////
379+ // //////////////////////////// OPTIONAL TASKS //////////////////////////////////////
380+ // //////////////////////////////////////////////////////////////////////////////////
381+
325382task runBindingGenerator (type : Exec ) {
326383 String jarsDir = " -PjarsDir=$rootDir /metadata/libs" // re-make to use intermediates folder
327384 String jsCodeToParse = " -PjsCodeDir=$rootDir /src/main/assets/app"
@@ -336,42 +393,34 @@ task runBindingGenerator (type: Exec) {
336393 }
337394}
338395
339- task deleteJavaDir (type : Delete ) {
340- delete " $rootDir /src/main/java"
341- }
342396
343- task cleanLocalAarFiles ( type : Delete ) {
344- delete fileTree( dir : " libs/aar " , include : [ " *.aar " ])
345- }
397+ // //////////////////////////////////////////////////////////////////////////////////
398+ // //////////////////////////// EXECUTION ORDER /////////////////////////////////////
399+ // //////////////////////////////////////////////////////////////////////////////////
346400
347- // prepare task chain
401+ // -- configuration phase
348402// 1. Copy *.aar dependencies
349403// 2. Add *.aar dependencies
350- // 3. Build
351- // 4. Clean libs/aar folder
352- // 5. Ensure metadata out directory
353- // 6. Collect all jars
354- // 7. Build metadata
355- // 8. Copy metadata
356-
357- cleanLocalAarFiles. dependsOn(deleteJavaDir)
358- isMetadataGenerationNecessary. dependsOn(cleanLocalAarFiles)
359- ensureMetadataOutDir. dependsOn(isMetadataGenerationNecessary)
360- buildMetadata. dependsOn(ensureMetadataOutDir)
404+ // 3. create default include files
405+ // 4. create plugins config file
406+ // 5. plugin extend (apply from include files)
407+
408+ // --execution phase
409+ deleteJavaDir. dependsOn(cleanLocalAarFiles)
410+ ensureMetadataOutDir. dependsOn(deleteJavaDir)
411+ collectAllJars. dependsOn(ensureMetadataOutDir)
412+ isMetadataGenerationNecessary. dependsOn(collectAllJars)
413+ buildMetadata. dependsOn(isMetadataGenerationNecessary)
361414copyMetadata. dependsOn(buildMetadata)
362415
416+ // DO NOT UNCOMMENT
417+ // mergeAssets.dependsOn(copyMetadata) -> look in CONFIGURATIONS(top) in android.applicationVariants to see how it's done
418+
363419task buildapk {
364-
365- // this way copy arr dependencies and addArrDependencies are executed before configuration
366- println " execute: copyAarDependencies, addAarDependencies before configuration"
420+ // problem is compile dependencies need to be changed before configuration stage
421+ // and this is the only way so far
367422 tasks. copyAarDependencies. execute()
368- tasks. addAarDependencies. execute()
369- //
423+ tasks. addAarDependencies. execute()
370424
371- if (project. hasProperty(" release" )) {
372- dependsOn assembleRelease
373- }
374- else {
375- dependsOn assembleDebug
376- }
377- }
425+ dependsOn deleteExplodedAarFolder
426+ }
0 commit comments