build.gradle 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. apply from: 'app/config.gradle'
  2. buildscript {
  3. apply from: 'app/config.gradle'
  4. repositories {
  5. google()
  6. jcenter()
  7. }
  8. dependencies {
  9. classpath libraries.androidGradlePlugin
  10. classpath libraries.kotlinGradlePlugin
  11. }
  12. }
  13. allprojects {
  14. repositories {
  15. google()
  16. jcenter()
  17. mavenCentral()
  18. }
  19. }
  20. ext {
  21. supportedAbis = ["armv7", "arm64v8", "x86", "x86_64"]
  22. supportedTargets = ["release", "debug"]
  23. // Used by gradle to specify which architecture to build for by default when running `./gradlew build`.
  24. // This command is usually used by Android Studio.
  25. // If building manually on the command line, it's recommended to use the
  26. // `./gradlew generateGodotTemplates` build command instead after running the `scons` command.
  27. // The defaultAbi must be one of the {supportedAbis} values.
  28. defaultAbi = "arm64v8"
  29. }
  30. def rootDir = "../../.."
  31. def binDir = "$rootDir/bin/"
  32. def getSconsTaskName(String buildType) {
  33. return "compileGodotNativeLibs" + buildType.capitalize()
  34. }
  35. /**
  36. * Copy the generated 'android_debug.apk' binary template into the Godot bin directory.
  37. * Depends on the app build task to ensure the binary is generated prior to copying.
  38. */
  39. task copyDebugBinaryToBin(type: Copy) {
  40. dependsOn ':app:assembleDebug'
  41. from('app/build/outputs/apk/debug')
  42. into(binDir)
  43. include('android_debug.apk')
  44. }
  45. /**
  46. * Copy the generated 'android_release.apk' binary template into the Godot bin directory.
  47. * Depends on the app build task to ensure the binary is generated prior to copying.
  48. */
  49. task copyReleaseBinaryToBin(type: Copy) {
  50. dependsOn ':app:assembleRelease'
  51. from('app/build/outputs/apk/release')
  52. into(binDir)
  53. include('android_release.apk')
  54. }
  55. /**
  56. * Copy the Godot android library archive debug file into the app module debug libs directory.
  57. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  58. */
  59. task copyDebugAARToAppModule(type: Copy) {
  60. dependsOn ':lib:assembleDebug'
  61. from('lib/build/outputs/aar')
  62. into('app/libs/debug')
  63. include('godot-lib.debug.aar')
  64. }
  65. /**
  66. * Copy the Godot android library archive debug file into the root bin directory.
  67. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  68. */
  69. task copyDebugAARToBin(type: Copy) {
  70. dependsOn ':lib:assembleDebug'
  71. from('lib/build/outputs/aar')
  72. into(binDir)
  73. include('godot-lib.debug.aar')
  74. }
  75. /**
  76. * Copy the Godot android library archive release file into the app module release libs directory.
  77. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  78. */
  79. task copyReleaseAARToAppModule(type: Copy) {
  80. dependsOn ':lib:assembleRelease'
  81. from('lib/build/outputs/aar')
  82. into('app/libs/release')
  83. include('godot-lib.release.aar')
  84. }
  85. /**
  86. * Copy the Godot android library archive release file into the root bin directory.
  87. * Depends on the library build task to ensure the AAR file is generated prior to copying.
  88. */
  89. task copyReleaseAARToBin(type: Copy) {
  90. dependsOn ':lib:assembleRelease'
  91. from('lib/build/outputs/aar')
  92. into(binDir)
  93. include('godot-lib.release.aar')
  94. }
  95. /**
  96. * Generate Godot custom build template by zipping the source files from the app directory, as well
  97. * as the AAR files generated by 'copyDebugAAR' and 'copyReleaseAAR'.
  98. * The zip file also includes some gradle tools to allow building of the custom build.
  99. */
  100. task zipCustomBuild(type: Zip) {
  101. dependsOn ':generateGodotTemplates'
  102. doFirst {
  103. logger.lifecycle("Generating Godot custom build template")
  104. }
  105. from(fileTree(dir: 'app', excludes: ['**/build/**', '**/.gradle/**', '**/*.iml']), fileTree(dir: '.', includes: ['gradle.properties', 'gradlew', 'gradlew.bat', 'gradle/**']))
  106. include '**/*'
  107. archiveName 'android_source.zip'
  108. destinationDir(file(binDir))
  109. }
  110. /**
  111. * Master task used to coordinate the tasks defined above to generate the set of Godot templates.
  112. */
  113. task generateGodotTemplates(type: GradleBuild) {
  114. // We exclude these gradle tasks so we can run the scons command manually.
  115. for (String buildType : supportedTargets) {
  116. startParameter.excludedTaskNames += ":lib:" + getSconsTaskName(buildType)
  117. }
  118. 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. finalizedBy 'zipCustomBuild'
  138. }
  139. /**
  140. * Clean the generated artifacts.
  141. */
  142. task cleanGodotTemplates(type: Delete) {
  143. // Delete the generated native libs
  144. delete("lib/libs")
  145. // Delete the library generated AAR files
  146. delete("lib/build/outputs/aar")
  147. // Delete the godotpayment libs directory contents
  148. delete("plugins/godotpayment/libs")
  149. // Delete the generated godotpayment aar
  150. delete("plugins/godotpayment/build/outputs/aar")
  151. // Delete the app libs directory contents
  152. delete("app/libs")
  153. // Delete the generated binary apks
  154. delete("app/build/outputs/apk")
  155. // Delete the Godot templates in the Godot bin directory
  156. delete("$binDir/android_debug.apk")
  157. delete("$binDir/android_release.apk")
  158. delete("$binDir/android_source.zip")
  159. delete("$binDir/godot-lib.debug.aar")
  160. delete("$binDir/godot-lib.release.aar")
  161. finalizedBy getTasksByName("clean", true)
  162. }