build.gradle 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. apply from: 'app/config.gradle'
  2. buildscript {
  3. apply from: 'app/config.gradle'
  4. repositories {
  5. google()
  6. mavenCentral()
  7. }
  8. dependencies {
  9. classpath libraries.androidGradlePlugin
  10. classpath libraries.kotlinGradlePlugin
  11. }
  12. }
  13. allprojects {
  14. repositories {
  15. google()
  16. mavenCentral()
  17. }
  18. }
  19. ext {
  20. supportedAbis = ["armv7", "arm64v8", "x86", "x86_64"]
  21. supportedTargets = ["release", "debug"]
  22. // Used by gradle to specify which architecture to build for by default when running `./gradlew build`.
  23. // This command is usually used by Android Studio.
  24. // If building manually on the command line, it's recommended to use the
  25. // `./gradlew generateGodotTemplates` build command instead after running the `scons` command.
  26. // The defaultAbi must be one of the {supportedAbis} values.
  27. defaultAbi = "arm64v8"
  28. }
  29. def rootDir = "../../.."
  30. def binDir = "$rootDir/bin/"
  31. def getSconsTaskName(String buildType) {
  32. return "compileGodotNativeLibs" + buildType.capitalize()
  33. }
  34. /**
  35. * Copy the generated 'android_debug.apk' binary template into the Godot bin directory.
  36. * Depends on the app build task to ensure the binary is generated prior to copying.
  37. */
  38. task copyDebugBinaryToBin(type: Copy) {
  39. dependsOn ':app:assembleDebug'
  40. from('app/build/outputs/apk/debug')
  41. into(binDir)
  42. include('android_debug.apk')
  43. }
  44. /**
  45. * Copy the generated 'android_release.apk' binary template into the Godot bin directory.
  46. * Depends on the app build task to ensure the binary is generated prior to copying.
  47. */
  48. task copyReleaseBinaryToBin(type: Copy) {
  49. dependsOn ':app:assembleRelease'
  50. from('app/build/outputs/apk/release')
  51. into(binDir)
  52. include('android_release.apk')
  53. }
  54. /**
  55. * Copy the Godot android library archive debug file into the app module debug libs directory.
  56. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  57. */
  58. task copyDebugAARToAppModule(type: Copy) {
  59. dependsOn ':lib:assembleDebug'
  60. from('lib/build/outputs/aar')
  61. into('app/libs/debug')
  62. include('godot-lib.debug.aar')
  63. }
  64. /**
  65. * Copy the Godot android library archive debug file into the root bin directory.
  66. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  67. */
  68. task copyDebugAARToBin(type: Copy) {
  69. dependsOn ':lib:assembleDebug'
  70. from('lib/build/outputs/aar')
  71. into(binDir)
  72. include('godot-lib.debug.aar')
  73. }
  74. /**
  75. * Copy the Godot android library archive release file into the app module release libs directory.
  76. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  77. */
  78. task copyReleaseAARToAppModule(type: Copy) {
  79. dependsOn ':lib:assembleRelease'
  80. from('lib/build/outputs/aar')
  81. into('app/libs/release')
  82. include('godot-lib.release.aar')
  83. }
  84. /**
  85. * Copy the Godot android library archive release file into the root bin directory.
  86. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  87. */
  88. task copyReleaseAARToBin(type: Copy) {
  89. dependsOn ':lib:assembleRelease'
  90. from('lib/build/outputs/aar')
  91. into(binDir)
  92. include('godot-lib.release.aar')
  93. }
  94. /**
  95. * Generate Godot custom build template by zipping the source files from the app directory, as well
  96. * as the AAR files generated by 'copyDebugAAR' and 'copyReleaseAAR'.
  97. * The zip file also includes some gradle tools to allow building of the custom build.
  98. */
  99. task zipCustomBuild(type: Zip) {
  100. onlyIf { generateGodotTemplates.state.executed || generateDevTemplate.state.executed }
  101. doFirst {
  102. logger.lifecycle("Generating Godot custom build template")
  103. }
  104. from(fileTree(dir: 'app', excludes: ['**/build/**', '**/.gradle/**', '**/*.iml']), fileTree(dir: '.', includes: ['gradlew', 'gradlew.bat', 'gradle/**']))
  105. include '**/*'
  106. archiveFileName = 'android_source.zip'
  107. destinationDirectory = file(binDir)
  108. }
  109. def templateExcludedBuildTask() {
  110. // We exclude these gradle tasks so we can run the scons command manually.
  111. def excludedTasks = []
  112. for (String buildType : supportedTargets) {
  113. excludedTasks += ":lib:" + getSconsTaskName(buildType)
  114. }
  115. return excludedTasks
  116. }
  117. def templateBuildTasks() {
  118. def tasks = []
  119. // Only build the apks and aar files for which we have native shared libraries.
  120. for (String target : supportedTargets) {
  121. File targetLibs = new File("lib/libs/" + target)
  122. if (targetLibs != null
  123. && targetLibs.isDirectory()
  124. && targetLibs.listFiles() != null
  125. && targetLibs.listFiles().length > 0) {
  126. String capitalizedTarget = target.capitalize()
  127. // Copy the generated aar library files to the custom build directory.
  128. tasks += "copy" + capitalizedTarget + "AARToAppModule"
  129. // Copy the generated aar library files to the bin directory.
  130. tasks += "copy" + capitalizedTarget + "AARToBin"
  131. // Copy the prebuilt binary templates to the bin directory.
  132. tasks += "copy" + capitalizedTarget + "BinaryToBin"
  133. } else {
  134. logger.lifecycle("No native shared libs for target $target. Skipping build.")
  135. }
  136. }
  137. return tasks
  138. }
  139. /**
  140. * Master task used to coordinate the tasks defined above to generate the set of Godot templates.
  141. */
  142. task generateGodotTemplates(type: GradleBuild) {
  143. startParameter.excludedTaskNames = templateExcludedBuildTask()
  144. tasks = templateBuildTasks()
  145. finalizedBy 'zipCustomBuild'
  146. }
  147. /**
  148. * Generates the same output as generateGodotTemplates but with dev symbols
  149. */
  150. task generateDevTemplate (type: GradleBuild) {
  151. // add parameter to set symbols to true
  152. startParameter.projectProperties += [doNotStrip: true]
  153. startParameter.excludedTaskNames = templateExcludedBuildTask()
  154. tasks = templateBuildTasks()
  155. finalizedBy 'zipCustomBuild'
  156. }
  157. /**
  158. * Clean the generated artifacts.
  159. */
  160. task cleanGodotTemplates(type: Delete) {
  161. // Delete the generated native libs
  162. delete("lib/libs")
  163. // Delete the library generated AAR files
  164. delete("lib/build/outputs/aar")
  165. // Delete the app libs directory contents
  166. delete("app/libs")
  167. // Delete the generated binary apks
  168. delete("app/build/outputs/apk")
  169. // Delete the Godot templates in the Godot bin directory
  170. delete("$binDir/android_debug.apk")
  171. delete("$binDir/android_release.apk")
  172. delete("$binDir/android_source.zip")
  173. delete("$binDir/godot-lib.debug.aar")
  174. delete("$binDir/godot-lib.release.aar")
  175. finalizedBy getTasksByName("clean", true)
  176. }