build.gradle 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. buildscript {
  2. apply from: 'app/config.gradle'
  3. repositories {
  4. google()
  5. mavenCentral()
  6. maven { url "https://plugins.gradle.org/m2/" }
  7. }
  8. dependencies {
  9. classpath libraries.androidGradlePlugin
  10. classpath libraries.kotlinGradlePlugin
  11. classpath 'io.github.gradle-nexus:publish-plugin:1.1.0'
  12. }
  13. }
  14. plugins {
  15. id 'io.github.gradle-nexus.publish-plugin'
  16. }
  17. apply from: 'app/config.gradle'
  18. apply from: 'scripts/publish-root.gradle'
  19. allprojects {
  20. repositories {
  21. google()
  22. mavenCentral()
  23. }
  24. }
  25. ext {
  26. supportedAbis = ["armv7", "arm64v8", "x86", "x86_64"]
  27. supportedTargetsMap = [release: "release", dev: "debug", debug: "release_debug"]
  28. supportedFlavors = ["editor", "template"]
  29. // Used by gradle to specify which architecture to build for by default when running
  30. // `./gradlew build` (this command is usually used by Android Studio).
  31. // If building manually on the command line, it's recommended to use the
  32. // `./gradlew generateGodotTemplates` build command instead after running the `scons` command(s).
  33. // The {selectedAbis} values must be from the {supportedAbis} values.
  34. selectedAbis = ["arm64v8"]
  35. }
  36. def rootDir = "../../.."
  37. def binDir = "$rootDir/bin/"
  38. def getSconsTaskName(String flavor, String buildType, String abi) {
  39. return "compileGodotNativeLibs" + flavor.capitalize() + buildType.capitalize() + abi.capitalize()
  40. }
  41. /**
  42. * Copy the generated 'android_debug.apk' binary template into the Godot bin directory.
  43. * Depends on the app build task to ensure the binary is generated prior to copying.
  44. */
  45. task copyDebugBinaryToBin(type: Copy) {
  46. dependsOn ':app:assembleDebug'
  47. from('app/build/outputs/apk/debug')
  48. into(binDir)
  49. include('android_debug.apk')
  50. }
  51. /**
  52. * Copy the generated 'android_dev.apk' binary template into the Godot bin directory.
  53. * Depends on the app build task to ensure the binary is generated prior to copying.
  54. */
  55. task copyDevBinaryToBin(type: Copy) {
  56. dependsOn ':app:assembleDev'
  57. from('app/build/outputs/apk/dev')
  58. into(binDir)
  59. include('android_dev.apk')
  60. }
  61. /**
  62. * Copy the generated 'android_release.apk' binary template into the Godot bin directory.
  63. * Depends on the app build task to ensure the binary is generated prior to copying.
  64. */
  65. task copyReleaseBinaryToBin(type: Copy) {
  66. dependsOn ':app:assembleRelease'
  67. from('app/build/outputs/apk/release')
  68. into(binDir)
  69. include('android_release.apk')
  70. }
  71. /**
  72. * Copy the Godot android library archive debug file into the app module debug libs directory.
  73. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  74. */
  75. task copyDebugAARToAppModule(type: Copy) {
  76. dependsOn ':lib:assembleTemplateDebug'
  77. from('lib/build/outputs/aar')
  78. into('app/libs/debug')
  79. include('godot-lib.debug.aar')
  80. }
  81. /**
  82. * Copy the Godot android library archive debug file into the root bin directory.
  83. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  84. */
  85. task copyDebugAARToBin(type: Copy) {
  86. dependsOn ':lib:assembleTemplateDebug'
  87. from('lib/build/outputs/aar')
  88. into(binDir)
  89. include('godot-lib.debug.aar')
  90. }
  91. /**
  92. * Copy the Godot android library archive dev file into the app module dev libs directory.
  93. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  94. */
  95. task copyDevAARToAppModule(type: Copy) {
  96. dependsOn ':lib:assembleTemplateDev'
  97. from('lib/build/outputs/aar')
  98. into('app/libs/dev')
  99. include('godot-lib.dev.aar')
  100. }
  101. /**
  102. * Copy the Godot android library archive dev file into the root bin directory.
  103. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  104. */
  105. task copyDevAARToBin(type: Copy) {
  106. dependsOn ':lib:assembleTemplateDev'
  107. from('lib/build/outputs/aar')
  108. into(binDir)
  109. include('godot-lib.dev.aar')
  110. }
  111. /**
  112. * Copy the Godot android library archive release file into the app module release libs directory.
  113. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  114. */
  115. task copyReleaseAARToAppModule(type: Copy) {
  116. dependsOn ':lib:assembleTemplateRelease'
  117. from('lib/build/outputs/aar')
  118. into('app/libs/release')
  119. include('godot-lib.release.aar')
  120. }
  121. /**
  122. * Copy the Godot android library archive release file into the root bin directory.
  123. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  124. */
  125. task copyReleaseAARToBin(type: Copy) {
  126. dependsOn ':lib:assembleTemplateRelease'
  127. from('lib/build/outputs/aar')
  128. into(binDir)
  129. include('godot-lib.release.aar')
  130. }
  131. /**
  132. * Generate Godot custom build template by zipping the source files from the app directory, as well
  133. * as the AAR files generated by 'copyDebugAAR', 'copyDevAAR' and 'copyReleaseAAR'.
  134. * The zip file also includes some gradle tools to allow building of the custom build.
  135. */
  136. task zipCustomBuild(type: Zip) {
  137. onlyIf { generateGodotTemplates.state.executed || generateDevTemplate.state.executed }
  138. doFirst {
  139. logger.lifecycle("Generating Godot custom build template")
  140. }
  141. from(fileTree(dir: 'app', excludes: ['**/build/**', '**/.gradle/**', '**/*.iml']), fileTree(dir: '.', includes: ['gradlew', 'gradlew.bat', 'gradle/**']))
  142. include '**/*'
  143. archiveFileName = 'android_source.zip'
  144. destinationDirectory = file(binDir)
  145. }
  146. def templateExcludedBuildTask() {
  147. // We exclude these gradle tasks so we can run the scons command manually.
  148. def excludedTasks = []
  149. if (!isAndroidStudio()) {
  150. logger.lifecycle("Excluding Android studio build tasks")
  151. for (String flavor : supportedFlavors) {
  152. for (String buildType : supportedTargetsMap.keySet()) {
  153. if (buildType == "release" && flavor == "editor") {
  154. // The editor can't be used with target=release as debugging tools are then not
  155. // included, and it would crash on errors instead of reporting them.
  156. continue
  157. }
  158. for (String abi : selectedAbis) {
  159. excludedTasks += ":lib:" + getSconsTaskName(flavor, buildType, abi)
  160. }
  161. }
  162. }
  163. }
  164. return excludedTasks
  165. }
  166. def templateBuildTasks() {
  167. def tasks = []
  168. // Only build the apks and aar files for which we have native shared libraries.
  169. for (String target : supportedTargetsMap.keySet()) {
  170. File targetLibs = new File("lib/libs/" + target)
  171. if (targetLibs != null
  172. && targetLibs.isDirectory()
  173. && targetLibs.listFiles() != null
  174. && targetLibs.listFiles().length > 0) {
  175. String capitalizedTarget = target.capitalize()
  176. // Copy the generated aar library files to the custom build directory.
  177. tasks += "copy" + capitalizedTarget + "AARToAppModule"
  178. // Copy the generated aar library files to the bin directory.
  179. tasks += "copy" + capitalizedTarget + "AARToBin"
  180. // Copy the prebuilt binary templates to the bin directory.
  181. tasks += "copy" + capitalizedTarget + "BinaryToBin"
  182. } else {
  183. logger.lifecycle("No native shared libs for target $target. Skipping build.")
  184. }
  185. }
  186. return tasks
  187. }
  188. def isAndroidStudio() {
  189. def sysProps = System.getProperties()
  190. return sysProps != null && sysProps['idea.platform.prefix'] != null
  191. }
  192. task copyEditorDebugBinaryToBin(type: Copy) {
  193. dependsOn ':editor:assembleDebug'
  194. from('editor/build/outputs/apk/debug')
  195. into(binDir)
  196. include('android_editor.apk')
  197. }
  198. task copyEditorDevBinaryToBin(type: Copy) {
  199. dependsOn ':editor:assembleDev'
  200. from('editor/build/outputs/apk/dev')
  201. into(binDir)
  202. include('android_editor_dev.apk')
  203. }
  204. /**
  205. * Generate the Godot Editor Android apk.
  206. *
  207. * Note: The Godot 'tools' shared libraries must have been generated (via scons) prior to running
  208. * this gradle task. The task will only build the apk(s) for which the shared libraries is
  209. * available.
  210. */
  211. task generateGodotEditor {
  212. gradle.startParameter.excludedTaskNames += templateExcludedBuildTask()
  213. def tasks = []
  214. for (String target : supportedTargetsMap.keySet()) {
  215. if (target == "release") {
  216. // The editor can't be used with target=release as debugging tools are then not
  217. // included, and it would crash on errors instead of reporting them.
  218. continue
  219. }
  220. File targetLibs = new File("lib/libs/tools/" + target)
  221. if (targetLibs != null
  222. && targetLibs.isDirectory()
  223. && targetLibs.listFiles() != null
  224. && targetLibs.listFiles().length > 0) {
  225. tasks += "copyEditor${target.capitalize()}BinaryToBin"
  226. }
  227. }
  228. dependsOn = tasks
  229. }
  230. /**
  231. * Master task used to coordinate the tasks defined above to generate the set of Godot templates.
  232. */
  233. task generateGodotTemplates {
  234. gradle.startParameter.excludedTaskNames += templateExcludedBuildTask()
  235. dependsOn = templateBuildTasks()
  236. finalizedBy 'zipCustomBuild'
  237. }
  238. /**
  239. * Generates the same output as generateGodotTemplates but with dev symbols
  240. */
  241. task generateDevTemplate {
  242. // add parameter to set symbols to true
  243. gradle.startParameter.projectProperties += [doNotStrip: true]
  244. gradle.startParameter.excludedTaskNames += templateExcludedBuildTask()
  245. dependsOn = templateBuildTasks()
  246. finalizedBy 'zipCustomBuild'
  247. }
  248. /**
  249. * Clean the generated editor artifacts.
  250. */
  251. task cleanGodotEditor(type: Delete) {
  252. // Delete the generated native tools libs
  253. delete("lib/libs/tools")
  254. // Delete the library generated AAR files
  255. delete("lib/build/outputs/aar")
  256. // Delete the generated binary apks
  257. delete("editor/build/outputs/apk")
  258. // Delete the Godot editor apks in the Godot bin directory
  259. delete("$binDir/android_editor.apk")
  260. delete("$binDir/android_editor_dev.apk")
  261. finalizedBy getTasksByName("clean", true)
  262. }
  263. /**
  264. * Clean the generated template artifacts.
  265. */
  266. task cleanGodotTemplates(type: Delete) {
  267. // Delete the generated native libs
  268. delete("lib/libs")
  269. // Delete the library generated AAR files
  270. delete("lib/build/outputs/aar")
  271. // Delete the app libs directory contents
  272. delete("app/libs")
  273. // Delete the generated binary apks
  274. delete("app/build/outputs/apk")
  275. // Delete the Godot templates in the Godot bin directory
  276. delete("$binDir/android_debug.apk")
  277. delete("$binDir/android_dev.apk")
  278. delete("$binDir/android_release.apk")
  279. delete("$binDir/android_source.zip")
  280. delete("$binDir/godot-lib.debug.aar")
  281. delete("$binDir/godot-lib.dev.aar")
  282. delete("$binDir/godot-lib.release.aar")
  283. finalizedBy getTasksByName("clean", true)
  284. }