build.gradle 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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://s01.oss.sonatype.org/content/repositories/snapshots/"}
  18. }
  19. }
  20. ext {
  21. supportedAbis = ["arm32", "arm64", "x86_32", "x86_64"]
  22. supportedFlavors = ["editor", "template"]
  23. supportedAndroidDistributions = ["android", "horizonos", "picoos"]
  24. supportedFlavorsBuildTypes = [
  25. "editor": ["dev", "debug", "release"],
  26. "template": ["dev", "debug", "release"]
  27. ]
  28. supportedEditions = ["standard", "mono"]
  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 = ["arm64"]
  35. rootDir = "../../.."
  36. binDir = "$rootDir/bin/"
  37. androidEditorBuildsDir = "$binDir/android_editor_builds/"
  38. }
  39. def getSconsTaskName(String flavor, String buildType, String abi) {
  40. return "compileGodotNativeLibs" + flavor.capitalize() + buildType.capitalize() + abi.capitalize()
  41. }
  42. /**
  43. * Generate Godot gradle build template by zipping the source files from the app directory, as well
  44. * as the AAR files generated by 'copyDebugAAR', 'copyDevAAR' and 'copyReleaseAAR'.
  45. * The zip file also includes some gradle tools to enable gradle builds from the Godot Editor.
  46. */
  47. task zipGradleBuild(type: Zip) {
  48. onlyIf { generateGodotTemplates.state.executed || generateGodotMonoTemplates.state.executed || generateDevTemplate.state.executed }
  49. doFirst {
  50. logger.lifecycle("Generating Godot gradle build template")
  51. }
  52. from(fileTree(dir: 'app', excludes: ['**/build/**', '**/.gradle/**', '**/*.iml']), fileTree(dir: '.', includes: ['gradlew', 'gradlew.bat', 'gradle/**']))
  53. include '**/*'
  54. archiveFileName = 'android_source.zip'
  55. destinationDirectory = file(binDir)
  56. }
  57. /**
  58. * Returns true if the scons build tasks responsible for generating the Godot native shared
  59. * libraries should be excluded.
  60. */
  61. def excludeSconsBuildTasks() {
  62. return !isAndroidStudio() && !project.hasProperty("generateNativeLibs")
  63. }
  64. /**
  65. * Generates the list of build tasks that should be excluded from the build process.\
  66. */
  67. def templateExcludedBuildTask() {
  68. // We exclude these gradle tasks so we can run the scons command manually.
  69. def excludedTasks = []
  70. if (excludeSconsBuildTasks()) {
  71. logger.info("Excluding Android studio build tasks")
  72. for (String flavor : supportedFlavors) {
  73. String[] supportedBuildTypes = supportedFlavorsBuildTypes[flavor]
  74. for (String buildType : supportedBuildTypes) {
  75. for (String abi : selectedAbis) {
  76. excludedTasks += ":lib:" + getSconsTaskName(flavor, buildType, abi)
  77. }
  78. }
  79. }
  80. }
  81. return excludedTasks
  82. }
  83. /**
  84. * Generates the build tasks for the given flavor
  85. * @param flavor Must be one of the supported flavors ('template' / 'editor')
  86. * @param edition Must be one of the supported editions ('standard' / 'mono')
  87. * @param androidDistro Must be one of the supported Android distributions ('android' / 'horizonos' / 'picoos')
  88. */
  89. def generateBuildTasks(String flavor = "template", String edition = "standard", String androidDistro = "android") {
  90. if (!supportedFlavors.contains(flavor)) {
  91. throw new GradleException("Invalid build flavor: $flavor")
  92. }
  93. if (!supportedAndroidDistributions.contains(androidDistro)) {
  94. throw new GradleException("Invalid Android distribution: $androidDistro")
  95. }
  96. if (!supportedEditions.contains(edition)) {
  97. throw new GradleException("Invalid build edition: $edition")
  98. }
  99. if (edition == "mono" && flavor != "template") {
  100. throw new GradleException("'mono' edition only supports the 'template' flavor.")
  101. }
  102. String capitalizedAndroidDistro = androidDistro.capitalize()
  103. def buildTasks = []
  104. // Only build the binary files for which we have native shared libraries unless we intend
  105. // to run the scons build tasks.
  106. boolean excludeSconsBuildTasks = excludeSconsBuildTasks()
  107. boolean isTemplate = flavor == "template"
  108. String libsDir = isTemplate ? "lib/libs/" : "lib/libs/tools/"
  109. for (String target : supportedFlavorsBuildTypes[flavor]) {
  110. File targetLibs = new File(libsDir + target)
  111. String targetSuffix = target
  112. if (target == "dev") {
  113. targetSuffix = "debug.dev"
  114. }
  115. if (!excludeSconsBuildTasks || (targetLibs != null
  116. && targetLibs.isDirectory()
  117. && targetLibs.listFiles() != null
  118. && targetLibs.listFiles().length > 0)) {
  119. String capitalizedTarget = target.capitalize()
  120. String capitalizedEdition = edition.capitalize()
  121. if (isTemplate) {
  122. // Copy the Godot android library archive file into the app module libs directory.
  123. // Depends on the library build task to ensure the AAR file is generated prior to copying.
  124. String copyAARTaskName = "copy${capitalizedTarget}AARToAppModule"
  125. if (tasks.findByName(copyAARTaskName) != null) {
  126. buildTasks += tasks.getByName(copyAARTaskName)
  127. } else {
  128. buildTasks += tasks.create(name: copyAARTaskName, type: Copy) {
  129. dependsOn ":lib:assembleTemplate${capitalizedTarget}"
  130. from('lib/build/outputs/aar')
  131. include("godot-lib.template_${targetSuffix}.aar")
  132. into("app/libs/${target}")
  133. }
  134. }
  135. // Copy the Godot android library archive file into the root bin directory.
  136. // Depends on the library build task to ensure the AAR file is generated prior to copying.
  137. String copyAARToBinTaskName = "copy${capitalizedTarget}AARToBin"
  138. if (tasks.findByName(copyAARToBinTaskName) != null) {
  139. buildTasks += tasks.getByName(copyAARToBinTaskName)
  140. } else {
  141. buildTasks += tasks.create(name: copyAARToBinTaskName, type: Copy) {
  142. dependsOn ":lib:assembleTemplate${capitalizedTarget}"
  143. from('lib/build/outputs/aar')
  144. include("godot-lib.template_${targetSuffix}.aar")
  145. into(binDir)
  146. }
  147. }
  148. // Copy the generated binary template into the Godot bin directory.
  149. // Depends on the app build task to ensure the binary is generated prior to copying.
  150. String copyBinaryTaskName = "copy${capitalizedEdition}${capitalizedTarget}BinaryToBin"
  151. if (tasks.findByName(copyBinaryTaskName) != null) {
  152. buildTasks += tasks.getByName(copyBinaryTaskName)
  153. } else {
  154. buildTasks += tasks.create(name: copyBinaryTaskName, type: Copy) {
  155. String filenameSuffix = edition == "mono" ? "${edition}${capitalizedTarget}" : target
  156. dependsOn ":app:assemble${capitalizedEdition}${capitalizedTarget}"
  157. from("app/build/outputs/apk/${edition}/${target}") {
  158. include("android_${filenameSuffix}.apk")
  159. }
  160. from("app/build/outputs/native-debug-symbols/${edition}${capitalizedTarget}") {
  161. include("native-debug-symbols.zip")
  162. rename ("native-debug-symbols.zip", "android-template-${edition}-${target}-native-debug-symbols.zip")
  163. }
  164. into(binDir)
  165. }
  166. }
  167. } else {
  168. // Copy the generated editor apk to the bin directory.
  169. String copyEditorApkTaskName = "copyEditor${capitalizedAndroidDistro}${capitalizedTarget}ApkToBin"
  170. if (tasks.findByName(copyEditorApkTaskName) != null) {
  171. buildTasks += tasks.getByName(copyEditorApkTaskName)
  172. } else {
  173. buildTasks += tasks.create(name: copyEditorApkTaskName, type: Copy) {
  174. dependsOn ":editor:assemble${capitalizedAndroidDistro}${capitalizedTarget}"
  175. from("editor/build/outputs/apk/${androidDistro}/${target}") {
  176. include("android_editor-${androidDistro}-${target}*.apk")
  177. }
  178. from("editor/build/outputs/native-debug-symbols/${androidDistro}${capitalizedTarget}") {
  179. include("native-debug-symbols.zip")
  180. rename ("native-debug-symbols.zip", "android-editor-${androidDistro}-${target}-native-debug-symbols.zip")
  181. }
  182. into(androidEditorBuildsDir)
  183. }
  184. }
  185. // Copy the generated editor aab to the bin directory.
  186. String copyEditorAabTaskName = "copyEditor${capitalizedAndroidDistro}${capitalizedTarget}AabToBin"
  187. if (tasks.findByName(copyEditorAabTaskName) != null) {
  188. buildTasks += tasks.getByName(copyEditorAabTaskName)
  189. } else {
  190. buildTasks += tasks.create(name: copyEditorAabTaskName, type: Copy) {
  191. dependsOn ":editor:bundle${capitalizedAndroidDistro}${capitalizedTarget}"
  192. from("editor/build/outputs/bundle/${androidDistro}${capitalizedTarget}")
  193. into(androidEditorBuildsDir)
  194. include("android_editor-${androidDistro}-${target}*.aab")
  195. }
  196. }
  197. }
  198. } else {
  199. logger.info("No native shared libs for target $target. Skipping build.")
  200. }
  201. }
  202. return buildTasks
  203. }
  204. /**
  205. * Generate the Godot Editor binaries for Android devices.
  206. *
  207. * Note: Unless the 'generateNativeLibs` argument is specified, the Godot 'tools' shared libraries
  208. * must have been generated (via scons) prior to running this gradle task.
  209. * The task will only build the binaries for which the shared libraries is available.
  210. */
  211. task generateGodotEditor {
  212. gradle.startParameter.excludedTaskNames += templateExcludedBuildTask()
  213. dependsOn = generateBuildTasks("editor", "standard", "android")
  214. }
  215. /**
  216. * Generate the Godot Editor binaries for HorizonOS devices.
  217. *
  218. * Note: Unless the 'generateNativeLibs` argument is specified, the Godot 'tools' shared libraries
  219. * must have been generated (via scons) prior to running this gradle task.
  220. * The task will only build the binaries for which the shared libraries is available.
  221. */
  222. task generateGodotHorizonOSEditor {
  223. gradle.startParameter.excludedTaskNames += templateExcludedBuildTask()
  224. dependsOn = generateBuildTasks("editor", "standard", "horizonos")
  225. }
  226. /**
  227. * Generate the Godot Editor binaries for PicoOS devices.
  228. *
  229. * Note: Unless the 'generateNativeLibs` argument is specified, the Godot 'tools' shared libraries
  230. * must have been generated (via scons) prior to running this gradle task.
  231. * The task will only build the binaries for which the shared libraries is available.
  232. */
  233. task generateGodotPicoOSEditor {
  234. gradle.startParameter.excludedTaskNames += templateExcludedBuildTask()
  235. dependsOn = generateBuildTasks("editor", "standard", "picoos")
  236. }
  237. /**
  238. * Master task used to coordinate the tasks defined above to generate the set of Godot templates.
  239. */
  240. task generateGodotTemplates {
  241. gradle.startParameter.excludedTaskNames += templateExcludedBuildTask()
  242. dependsOn = generateBuildTasks("template")
  243. finalizedBy 'zipGradleBuild'
  244. }
  245. /**
  246. * Master task used to coordinate the tasks defined above to generate the set of Godot templates
  247. * for the 'mono' edition of the engine.
  248. */
  249. task generateGodotMonoTemplates {
  250. gradle.startParameter.excludedTaskNames += templateExcludedBuildTask()
  251. dependsOn = generateBuildTasks("template", "mono")
  252. finalizedBy 'zipGradleBuild'
  253. }
  254. task clean(type: Delete) {
  255. dependsOn 'cleanGodotEditor'
  256. dependsOn 'cleanGodotTemplates'
  257. }
  258. /**
  259. * Clean the generated editor artifacts.
  260. */
  261. task cleanGodotEditor(type: Delete) {
  262. // Delete the generated native tools libs
  263. delete("lib/libs/tools")
  264. // Delete the library generated AAR files
  265. delete("lib/build/outputs/aar")
  266. // Delete the generated binary apks
  267. delete("editor/build/outputs/apk")
  268. // Delete the generated aab binaries
  269. delete("editor/build/outputs/bundle")
  270. // Delete the Godot editor apks & aabs in the Godot bin directory
  271. delete(androidEditorBuildsDir)
  272. }
  273. /**
  274. * Clean the generated template artifacts.
  275. */
  276. task cleanGodotTemplates(type: Delete) {
  277. // Delete the generated native libs
  278. delete("lib/libs")
  279. // Delete the library generated AAR files
  280. delete("lib/build/outputs/aar")
  281. // Delete the app libs directory contents
  282. delete("app/libs")
  283. // Delete the generated binary apks
  284. delete("app/build/outputs/apk")
  285. // Delete the Godot templates in the Godot bin directory
  286. delete("$binDir/android_debug.apk")
  287. delete("$binDir/android-template-standard-debug-native-debug-symbols.zip")
  288. delete("$binDir/android_dev.apk")
  289. delete("$binDir/android-template-standard-dev-native-debug-symbols.zip")
  290. delete("$binDir/android_release.apk")
  291. delete("$binDir/android-template-standard-release-native-debug-symbols.zip")
  292. delete("$binDir/android_monoDebug.apk")
  293. delete("$binDir/android-template-mono-debug-native-debug-symbols.zip")
  294. delete("$binDir/android_monoDev.apk")
  295. delete("$binDir/android-template-mono-dev-native-debug-symbols.zip")
  296. delete("$binDir/android_monoRelease.apk")
  297. delete("$binDir/android-template-mono-release-native-debug-symbols.zip")
  298. delete("$binDir/android_source.zip")
  299. delete("$binDir/godot-lib.template_debug.aar")
  300. delete("$binDir/godot-lib.template_debug.dev.aar")
  301. delete("$binDir/godot-lib.template_release.aar")
  302. // Cover deletion for the libs using the previous naming scheme
  303. delete("$binDir/godot-lib.debug.aar")
  304. delete("$binDir/godot-lib.dev.aar")
  305. delete("$binDir/godot-lib.release.aar")
  306. }