build.gradle 12 KB

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