build.gradle 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Gradle build config for Godot Engine's Android port.
  2. plugins {
  3. id 'com.android.application'
  4. id 'org.jetbrains.kotlin.android'
  5. }
  6. apply from: 'config.gradle'
  7. allprojects {
  8. repositories {
  9. google()
  10. mavenCentral()
  11. gradlePluginPortal()
  12. maven { url "https://plugins.gradle.org/m2/" }
  13. maven { url "https://s01.oss.sonatype.org/content/repositories/snapshots/"}
  14. // Godot user plugins custom maven repos
  15. String[] mavenRepos = getGodotPluginsMavenRepos()
  16. if (mavenRepos != null && mavenRepos.size() > 0) {
  17. for (String repoUrl : mavenRepos) {
  18. maven {
  19. url repoUrl
  20. }
  21. }
  22. }
  23. }
  24. }
  25. configurations {
  26. // Initializes a placeholder for the devImplementation dependency configuration.
  27. devImplementation {}
  28. // Initializes a placeholder for the monoImplementation dependency configuration.
  29. monoImplementation {}
  30. }
  31. dependencies {
  32. implementation "androidx.fragment:fragment:$versions.fragmentVersion"
  33. implementation "androidx.core:core-splashscreen:$versions.splashscreenVersion"
  34. if (rootProject.findProject(":lib")) {
  35. implementation project(":lib")
  36. } else if (rootProject.findProject(":godot:lib")) {
  37. implementation project(":godot:lib")
  38. } else {
  39. // Godot gradle build mode. In this scenario this project is the only one around and the Godot
  40. // library is available through the pre-generated godot-lib.*.aar android archive files.
  41. debugImplementation fileTree(dir: 'libs/debug', include: ['**/*.jar', '*.aar'])
  42. devImplementation fileTree(dir: 'libs/dev', include: ['**/*.jar', '*.aar'])
  43. releaseImplementation fileTree(dir: 'libs/release', include: ['**/*.jar', '*.aar'])
  44. }
  45. // Godot user plugins remote dependencies
  46. String[] remoteDeps = getGodotPluginsRemoteBinaries()
  47. if (remoteDeps != null && remoteDeps.size() > 0) {
  48. for (String dep : remoteDeps) {
  49. implementation dep
  50. }
  51. }
  52. // Godot user plugins local dependencies
  53. String[] pluginsBinaries = getGodotPluginsLocalBinaries()
  54. if (pluginsBinaries != null && pluginsBinaries.size() > 0) {
  55. implementation files(pluginsBinaries)
  56. }
  57. // Automatically pick up local dependencies in res://addons
  58. String addonsDirectory = getAddonsDirectory()
  59. if (addonsDirectory != null && !addonsDirectory.isBlank()) {
  60. implementation fileTree(dir: "$addonsDirectory", include: ['*.jar', '*.aar'])
  61. }
  62. // .NET dependencies
  63. String jar = '../../../../modules/mono/thirdparty/libSystem.Security.Cryptography.Native.Android.jar'
  64. if (file(jar).exists()) {
  65. monoImplementation files(jar)
  66. }
  67. }
  68. android {
  69. compileSdkVersion versions.compileSdk
  70. buildToolsVersion versions.buildTools
  71. ndkVersion versions.ndkVersion
  72. compileOptions {
  73. sourceCompatibility versions.javaVersion
  74. targetCompatibility versions.javaVersion
  75. }
  76. kotlinOptions {
  77. jvmTarget = versions.javaVersion
  78. }
  79. assetPacks = [":assetPacks:installTime"]
  80. namespace = 'com.godot.game'
  81. defaultConfig {
  82. // The default ignore pattern for the 'assets' directory includes hidden files and directories which are used by Godot projects.
  83. aaptOptions {
  84. ignoreAssetsPattern "!.svn:!.git:!.gitignore:!.ds_store:!*.scc:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"
  85. }
  86. ndk {
  87. String[] export_abi_list = getExportEnabledABIs()
  88. abiFilters export_abi_list
  89. }
  90. manifestPlaceholders = [
  91. godotEditorVersion: getGodotEditorVersion(),
  92. godotRenderingMethod: getGodotRenderingMethod()
  93. ]
  94. // Feel free to modify the application id to your own.
  95. applicationId getExportPackageName()
  96. versionCode getExportVersionCode()
  97. versionName getExportVersionName()
  98. minSdkVersion getExportMinSdkVersion()
  99. targetSdkVersion getExportTargetSdkVersion()
  100. missingDimensionStrategy 'products', 'template'
  101. }
  102. lintOptions {
  103. abortOnError false
  104. disable 'MissingTranslation', 'UnusedResources'
  105. }
  106. ndkVersion versions.ndkVersion
  107. packagingOptions {
  108. exclude 'META-INF/LICENSE'
  109. exclude 'META-INF/NOTICE'
  110. // 'doNotStrip' is enabled for development within Android Studio
  111. if (shouldNotStrip()) {
  112. doNotStrip '**/*.so'
  113. }
  114. jniLibs {
  115. // Setting this to true causes AGP to package compressed native libraries when building the app
  116. // For more background, see:
  117. // - https://developer.android.com/build/releases/past-releases/agp-3-6-0-release-notes#extractNativeLibs
  118. // - https://stackoverflow.com/a/44704840
  119. useLegacyPackaging shouldUseLegacyPackaging()
  120. }
  121. // Always select Godot's version of libc++_shared.so in case deps have their own
  122. pickFirst 'lib/x86/libc++_shared.so'
  123. pickFirst 'lib/x86_64/libc++_shared.so'
  124. pickFirst 'lib/armeabi-v7a/libc++_shared.so'
  125. pickFirst 'lib/arm64-v8a/libc++_shared.so'
  126. }
  127. signingConfigs {
  128. debug {
  129. if (hasCustomDebugKeystore()) {
  130. storeFile new File(getDebugKeystoreFile())
  131. storePassword getDebugKeystorePassword()
  132. keyAlias getDebugKeyAlias()
  133. keyPassword getDebugKeystorePassword()
  134. }
  135. }
  136. release {
  137. File keystoreFile = new File(getReleaseKeystoreFile())
  138. if (keystoreFile.isFile()) {
  139. storeFile keystoreFile
  140. storePassword getReleaseKeystorePassword()
  141. keyAlias getReleaseKeyAlias()
  142. keyPassword getReleaseKeystorePassword()
  143. }
  144. }
  145. }
  146. buildFeatures {
  147. buildConfig = true
  148. }
  149. buildTypes {
  150. debug {
  151. // Signing and zip-aligning are skipped for prebuilt builds, but
  152. // performed for Godot gradle builds.
  153. zipAlignEnabled shouldZipAlign()
  154. if (shouldSign()) {
  155. signingConfig signingConfigs.debug
  156. } else {
  157. signingConfig null
  158. }
  159. }
  160. dev {
  161. initWith debug
  162. // Signing and zip-aligning are skipped for prebuilt builds, but
  163. // performed for Godot gradle builds.
  164. zipAlignEnabled shouldZipAlign()
  165. if (shouldSign()) {
  166. signingConfig signingConfigs.debug
  167. } else {
  168. signingConfig null
  169. }
  170. }
  171. release {
  172. // Signing and zip-aligning are skipped for prebuilt builds, but
  173. // performed for Godot gradle builds.
  174. zipAlignEnabled shouldZipAlign()
  175. if (shouldSign()) {
  176. signingConfig signingConfigs.release
  177. } else {
  178. signingConfig null
  179. }
  180. }
  181. }
  182. flavorDimensions 'edition'
  183. productFlavors {
  184. standard {}
  185. mono {}
  186. }
  187. sourceSets {
  188. main {
  189. manifest.srcFile 'AndroidManifest.xml'
  190. java.srcDirs = ['src']
  191. res.srcDirs = ['res']
  192. aidl.srcDirs = ['aidl']
  193. assets.srcDirs = ['assets']
  194. }
  195. debug.jniLibs.srcDirs = ['libs/debug', 'libs/debug/vulkan_validation_layers']
  196. dev.jniLibs.srcDirs = ['libs/dev']
  197. release.jniLibs.srcDirs = ['libs/release']
  198. }
  199. applicationVariants.all { variant ->
  200. variant.outputs.all { output ->
  201. String filenameSuffix = variant.flavorName == "mono" ? variant.name : variant.buildType.name
  202. output.outputFileName = "android_${filenameSuffix}.apk"
  203. }
  204. }
  205. }
  206. task copyAndRenameBinary(type: Copy) {
  207. // The 'doNotTrackState' is added to disable gradle's up-to-date checks for output files
  208. // and directories. Otherwise this check may cause permissions access failures on Windows
  209. // machines.
  210. doNotTrackState("No need for up-to-date checks for the copy-and-rename operation")
  211. String exportPath = getExportPath()
  212. String exportFilename = getExportFilename()
  213. String exportEdition = getExportEdition()
  214. String exportBuildType = getExportBuildType()
  215. String exportBuildTypeCapitalized = exportBuildType.capitalize()
  216. String exportFormat = getExportFormat()
  217. boolean isAab = exportFormat == "aab"
  218. boolean isMono = exportEdition == "mono"
  219. String filenameSuffix = isAab ? "${exportEdition}-${exportBuildType}" : exportBuildType
  220. if (isMono) {
  221. filenameSuffix = isAab ? "${exportEdition}-${exportBuildType}" : "${exportEdition}${exportBuildTypeCapitalized}"
  222. }
  223. String sourceFilename = isAab ? "build-${filenameSuffix}.aab" : "android_${filenameSuffix}.apk"
  224. String sourceFilepath = isAab ? "$buildDir/outputs/bundle/${exportEdition}${exportBuildTypeCapitalized}/$sourceFilename" : "$buildDir/outputs/apk/$exportEdition/$exportBuildType/$sourceFilename"
  225. from sourceFilepath
  226. into exportPath
  227. rename sourceFilename, exportFilename
  228. }
  229. /**
  230. * Used to validate the version of the Java SDK used for the Godot gradle builds.
  231. */
  232. task validateJavaVersion {
  233. if (JavaVersion.current() != versions.javaVersion) {
  234. throw new GradleException("Invalid Java version ${JavaVersion.current()}. Version ${versions.javaVersion} is the required Java version for Godot gradle builds.")
  235. }
  236. }
  237. /*
  238. When they're scheduled to run, the copy*AARToAppModule tasks generate dependencies for the 'app'
  239. module, so we're ensuring the ':app:preBuild' task is set to run after those tasks.
  240. */
  241. if (rootProject.tasks.findByPath("copyDebugAARToAppModule") != null) {
  242. preBuild.mustRunAfter(rootProject.tasks.named("copyDebugAARToAppModule"))
  243. }
  244. if (rootProject.tasks.findByPath("copyDevAARToAppModule") != null) {
  245. preBuild.mustRunAfter(rootProject.tasks.named("copyDevAARToAppModule"))
  246. }
  247. if (rootProject.tasks.findByPath("copyReleaseAARToAppModule") != null) {
  248. preBuild.mustRunAfter(rootProject.tasks.named("copyReleaseAARToAppModule"))
  249. }