build.gradle 12 KB

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