build.gradle 11 KB

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