build.gradle 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. apply from: 'app/config.gradle'
  2. buildscript {
  3. apply from: 'app/config.gradle'
  4. repositories {
  5. google()
  6. jcenter()
  7. }
  8. dependencies {
  9. classpath libraries.androidGradlePlugin
  10. classpath libraries.kotlinGradlePlugin
  11. }
  12. }
  13. allprojects {
  14. repositories {
  15. google()
  16. jcenter()
  17. mavenCentral()
  18. }
  19. }
  20. ext {
  21. supportedAbis = ["armv7", "arm64v8", "x86", "x86_64"]
  22. supportedTargets = ["release", "debug"]
  23. // Used by gradle to specify which architecture to build for by default when running `./gradlew build`.
  24. // This command is usually used by Android Studio.
  25. // If building manually on the command line, it's recommended to use the
  26. // `./gradlew generateGodotTemplates` build command instead after running the `scons` command.
  27. // The defaultAbi must be one of the {supportedAbis} values.
  28. defaultAbi = "arm64v8"
  29. }
  30. def rootDir = "../../.."
  31. def binDir = "$rootDir/bin/"
  32. def getSconsTaskName(String buildType) {
  33. return "compileGodotNativeLibs" + buildType.capitalize()
  34. }
  35. /**
  36. * Copy the generated 'android_debug.apk' binary template into the Godot bin directory.
  37. * Depends on the app build task to ensure the binary is generated prior to copying.
  38. */
  39. task copyDebugBinaryToBin(type: Copy) {
  40. dependsOn ':app:assembleDebug'
  41. from('app/build/outputs/apk/debug')
  42. into(binDir)
  43. include('android_debug.apk')
  44. }
  45. /**
  46. * Copy the generated 'android_release.apk' binary template into the Godot bin directory.
  47. * Depends on the app build task to ensure the binary is generated prior to copying.
  48. */
  49. task copyReleaseBinaryToBin(type: Copy) {
  50. dependsOn ':app:assembleRelease'
  51. from('app/build/outputs/apk/release')
  52. into(binDir)
  53. include('android_release.apk')
  54. }
  55. /**
  56. * Copy the Godot android library archive debug file into the app module debug libs directory.
  57. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  58. */
  59. task copyDebugAARToAppModule(type: Copy) {
  60. dependsOn ':lib:assembleDebug'
  61. from('lib/build/outputs/aar')
  62. into('app/libs/debug')
  63. include('godot-lib.debug.aar')
  64. }
  65. /**
  66. * Copy the Godot android library archive debug file into the root bin directory.
  67. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  68. */
  69. task copyDebugAARToBin(type: Copy) {
  70. dependsOn ':lib:assembleDebug'
  71. from('lib/build/outputs/aar')
  72. into(binDir)
  73. include('godot-lib.debug.aar')
  74. }
  75. /**
  76. * Copy the Godot android library archive release file into the app module release libs directory.
  77. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  78. */
  79. task copyReleaseAARToAppModule(type: Copy) {
  80. dependsOn ':lib:assembleRelease'
  81. from('lib/build/outputs/aar')
  82. into('app/libs/release')
  83. include('godot-lib.release.aar')
  84. }
  85. /**
  86. * Copy the Godot android library archive release file into the root bin directory.
  87. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  88. */
  89. task copyReleaseAARToBin(type: Copy) {
  90. dependsOn ':lib:assembleRelease'
  91. from('lib/build/outputs/aar')
  92. into(binDir)
  93. include('godot-lib.release.aar')
  94. }
  95. /**
  96. * Generate Godot custom build template by zipping the source files from the app directory, as well
  97. * as the AAR files generated by 'copyDebugAAR' and 'copyReleaseAAR'.
  98. * The zip file also includes some gradle tools to allow building of the custom build.
  99. */
  100. task zipCustomBuild(type: Zip) {
  101. onlyIf { generateGodotTemplates.state.executed || generateDevTemplate.state.executed }
  102. doFirst {
  103. logger.lifecycle("Generating Godot custom build template")
  104. }
  105. from(fileTree(dir: 'app', excludes: ['**/build/**', '**/.gradle/**', '**/*.iml']), fileTree(dir: '.', includes: ['gradle.properties', 'gradlew', 'gradlew.bat', 'gradle/**']))
  106. include '**/*'
  107. archiveName 'android_source.zip'
  108. destinationDir(file(binDir))
  109. }
  110. def templateExcludedBuildTask() {
  111. // We exclude these gradle tasks so we can run the scons command manually.
  112. def excludedTasks = []
  113. for (String buildType : supportedTargets) {
  114. excludedTasks += ":lib:" + getSconsTaskName(buildType)
  115. }
  116. return excludedTasks
  117. }
  118. def templateBuildTasks() {
  119. def tasks = []
  120. // Only build the apks and aar files for which we have native shared libraries.
  121. for (String target : supportedTargets) {
  122. File targetLibs = new File("lib/libs/" + target)
  123. if (targetLibs != null
  124. && targetLibs.isDirectory()
  125. && targetLibs.listFiles() != null
  126. && targetLibs.listFiles().length > 0) {
  127. String capitalizedTarget = target.capitalize()
  128. // Copy the generated aar library files to the custom build directory.
  129. tasks += "copy" + capitalizedTarget + "AARToAppModule"
  130. // Copy the generated aar library files to the bin directory.
  131. tasks += "copy" + capitalizedTarget + "AARToBin"
  132. // Copy the prebuilt binary templates to the bin directory.
  133. tasks += "copy" + capitalizedTarget + "BinaryToBin"
  134. } else {
  135. logger.lifecycle("No native shared libs for target $target. Skipping build.")
  136. }
  137. }
  138. return tasks
  139. }
  140. /**
  141. * Master task used to coordinate the tasks defined above to generate the set of Godot templates.
  142. */
  143. task generateGodotTemplates(type: GradleBuild) {
  144. startParameter.excludedTaskNames = templateExcludedBuildTask()
  145. tasks = templateBuildTasks()
  146. finalizedBy 'zipCustomBuild'
  147. }
  148. /**
  149. * Generates the same output as generateGodotTemplates but with dev symbols
  150. */
  151. task generateDevTemplate (type: GradleBuild) {
  152. // add parameter to set symbols to true
  153. startParameter.projectProperties += [doNotStrip: true]
  154. startParameter.excludedTaskNames = templateExcludedBuildTask()
  155. tasks = templateBuildTasks()
  156. finalizedBy 'zipCustomBuild'
  157. }
  158. /**
  159. * Clean the generated artifacts.
  160. */
  161. task cleanGodotTemplates(type: Delete) {
  162. // Delete the generated native libs
  163. delete("lib/libs")
  164. // Delete the library generated AAR files
  165. delete("lib/build/outputs/aar")
  166. // Delete the app libs directory contents
  167. delete("app/libs")
  168. // Delete the generated binary apks
  169. delete("app/build/outputs/apk")
  170. // Delete the Godot templates in the Godot bin directory
  171. delete("$binDir/android_debug.apk")
  172. delete("$binDir/android_release.apk")
  173. delete("$binDir/android_source.zip")
  174. delete("$binDir/godot-lib.debug.aar")
  175. delete("$binDir/godot-lib.release.aar")
  176. finalizedBy getTasksByName("clean", true)
  177. }