build.gradle 11 KB

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