Skip to content
0.2.0-dev Download ZIP
352 lines ·12.4 KB Raw
1// Gradle build config for Godot Engine's Android port.
2plugins {
3 id 'com.android.application'
4 id 'org.jetbrains.kotlin.android'
5}
6
7apply from: 'config.gradle'
8
9allprojects {
10 repositories {
11 google()
12 mavenCentral()
13 gradlePluginPortal()
14 maven { url = "https://plugins.gradle.org/m2/" }
15 maven { url = "https://central.sonatype.com/repository/maven-snapshots/"}
16
17 // Godot user plugins custom maven repos
18 String[] mavenRepos = getGodotPluginsMavenRepos()
19 if (mavenRepos != null && mavenRepos.size() > 0) {
20 for (String repoUrl : mavenRepos) {
21 maven {
22 url repoUrl
23 }
24 }
25 }
26 }
27}
28
29configurations {
30 // Initializes a placeholder for the monoImplementation dependency configuration.
31 monoImplementation {}
32}
33
34dependencies {
35 // Android instrumented test dependencies
36 androidTestImplementation "androidx.test.ext:junit:$versions.junitVersion"
37 androidTestImplementation "androidx.test.espresso:espresso-core:$versions.espressoCoreVersion"
38 androidTestImplementation "org.jetbrains.kotlin:kotlin-test:$versions.kotlinTestVersion"
39 androidTestImplementation "androidx.test:runner:$versions.testRunnerVersion"
40 androidTestUtil "androidx.test:orchestrator:$versions.testOrchestratorVersion"
41
42 implementation "androidx.fragment:fragment:$versions.fragmentVersion"
43 implementation "androidx.core:core-splashscreen:$versions.splashscreenVersion"
44 implementation "androidx.documentfile:documentfile:$versions.documentfileVersion"
45
46 implementation project(":dataLib")
47 implementation project(":fusedDepsLib")
48
49 if (rootProject.findProject(":lib")) {
50 implementation project(":lib")
51 } else if (rootProject.findProject(":godot:lib")) {
52 implementation project(":godot:lib")
53 } else {
54 // Godot gradle build mode. In this scenario this project is the only one around and the Godot
55 // library is available through the pre-generated godot-lib.*.aar android archive files.
56 debugImplementation fileTree(dir: 'libs/debug', include: ['**/*.jar', '*.aar'])
57 releaseImplementation fileTree(dir: 'libs/release', include: ['**/*.jar', '*.aar'])
58 }
59
60 // Godot user plugins remote dependencies
61 String[] remoteDeps = getGodotPluginsRemoteBinaries()
62 if (remoteDeps != null && remoteDeps.size() > 0) {
63 def platformPattern = /^\s*(platform|enforcedPlatform)\s*\(\s*['"]*(\S+)['"]*\s*\)$/
64 for (String dep : remoteDeps) {
65 def matcher = dep =~ platformPattern
66 if (matcher) {
67 switch (matcher[0][1]) {
68 case "platform":
69 implementation platform(matcher[0][2])
70 break
71
72 case "enforcedPlatform":
73 implementation enforcedPlatform(matcher[0][2])
74 break
75
76 default:
77 throw new GradleException("Invalid remote platform dependency: $dep")
78 break
79 }
80 } else {
81 // This is handled in ':fusedDepsLib:build.gradle'
82 }
83 }
84 }
85
86 // Godot user plugins local dependencies
87 String[] pluginsBinaries = getGodotPluginsLocalBinaries()
88 if (pluginsBinaries != null && pluginsBinaries.size() > 0) {
89 implementation files(pluginsBinaries)
90 }
91
92 // Automatically pick up local dependencies in res://addons
93 String addonsDirectory = getAddonsDirectory()
94 if (addonsDirectory != null && !addonsDirectory.isBlank()) {
95 implementation fileTree(dir: "$addonsDirectory", include: ['*.jar', '*.aar'])
96 }
97
98 // .NET dependencies
99 String jar = '../../../../modules/mono/thirdparty/libSystem.Security.Cryptography.Native.Android.jar'
100 if (file(jar).exists()) {
101 monoImplementation files(jar)
102 }
103}
104
105android {
106 compileSdkVersion versions.compileSdk
107 buildToolsVersion versions.buildTools
108 ndkVersion = versions.ndkVersion
109
110 compileOptions {
111 sourceCompatibility versions.javaVersion
112 targetCompatibility versions.javaVersion
113 }
114
115 kotlinOptions {
116 jvmTarget = versions.javaVersion
117 }
118
119 assetPacks = [":assetPackInstallTime"]
120
121 namespace = 'com.godot.game.app'
122
123 defaultConfig {
124 // The default ignore pattern for the 'assets' directory includes hidden files and directories which are used by Godot projects.
125 aaptOptions {
126 ignoreAssetsPattern = "!.svn:!.git:!.gitignore:!.ds_store:!*.scc:!CVS:!thumbs.db:!picasa.ini:!*~"
127 }
128
129 ndk {
130 debugSymbolLevel = 'NONE'
131 String[] export_abi_list = getExportEnabledABIs()
132 abiFilters export_abi_list
133 }
134
135 // Feel free to modify the application id to your own.
136 applicationId getExportPackageName()
137 versionCode getExportVersionCode()
138 versionName getExportVersionName()
139 minSdkVersion getExportMinSdkVersion()
140 targetSdkVersion getExportTargetSdkVersion()
141
142 missingDimensionStrategy 'products', 'template'
143
144 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
145
146 // The following argument makes the Android Test Orchestrator run its
147 // "pm clear" command after each test invocation. This command ensures
148 // that the app's state is completely cleared between tests.
149 testInstrumentationRunnerArguments clearPackageData: 'true'
150 }
151
152 testOptions {
153 execution = 'ANDROIDX_TEST_ORCHESTRATOR'
154 }
155
156 lintOptions {
157 abortOnError = false
158 disable 'MissingTranslation', 'UnusedResources'
159 }
160
161 ndkVersion = versions.ndkVersion
162
163 packagingOptions {
164 exclude 'META-INF/LICENSE'
165 exclude 'META-INF/NOTICE'
166
167 // Debug symbols are kept for development within Android Studio.
168 if (shouldNotStrip()) {
169 jniLibs {
170 keepDebugSymbols += '**/*.so'
171 }
172 }
173
174 jniLibs {
175 // Setting this to true causes AGP to package compressed native libraries when building the app
176 // For more background, see:
177 // - https://developer.android.com/build/releases/past-releases/agp-3-6-0-release-notes#extractNativeLibs
178 // - https://stackoverflow.com/a/44704840
179 useLegacyPackaging = shouldUseLegacyPackaging()
180 }
181
182 // Always select Godot's version of libc++_shared.so in case deps have their own
183 pickFirst 'lib/x86/libc++_shared.so'
184 pickFirst 'lib/x86_64/libc++_shared.so'
185 pickFirst 'lib/armeabi-v7a/libc++_shared.so'
186 pickFirst 'lib/arm64-v8a/libc++_shared.so'
187 }
188
189 signingConfigs {
190 debug {
191 if (hasCustomDebugKeystore()) {
192 storeFile new File(getDebugKeystoreFile())
193 storePassword getDebugKeystorePassword()
194 keyAlias getDebugKeyAlias()
195 keyPassword getDebugKeystorePassword()
196 }
197 }
198
199 release {
200 File keystoreFile = new File(getReleaseKeystoreFile())
201 if (keystoreFile.isFile()) {
202 storeFile keystoreFile
203 storePassword getReleaseKeystorePassword()
204 keyAlias getReleaseKeyAlias()
205 keyPassword getReleaseKeystorePassword()
206 }
207 }
208 }
209
210 buildFeatures {
211 buildConfig = true
212 }
213
214 buildTypes {
215
216 debug {
217 // Signing and zip-aligning are skipped for prebuilt builds, but
218 // performed for Godot gradle builds.
219 zipAlignEnabled shouldZipAlign()
220 if (shouldSign()) {
221 signingConfig = signingConfigs.debug
222 } else {
223 signingConfig = null
224 }
225 }
226
227 release {
228 minifyEnabled shouldMinify()
229 shrinkResources = false
230 proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
231
232 // Signing and zip-aligning are skipped for prebuilt builds, but
233 // performed for Godot gradle builds.
234 zipAlignEnabled shouldZipAlign()
235 if (shouldSign()) {
236 signingConfig = signingConfigs.release
237 } else {
238 signingConfig = null
239 }
240 }
241 }
242
243 flavorDimensions 'edition'
244
245 productFlavors {
246 // Product flavor for the standard (no .net support) builds.
247 standard {
248 getIsDefault().set(true)
249 }
250
251 // Product flavor for the Mono (.net) builds.
252 mono {}
253
254 // Product flavor used for running instrumented tests.
255 instrumented {
256 applicationIdSuffix ".instrumented"
257 versionNameSuffix "-instrumented"
258 }
259 }
260
261 sourceSets {
262 main.res.srcDirs += ['res']
263 debug.jniLibs.srcDirs = ['libs/debug', 'libs/debug/vulkan_validation_layers']
264 release.jniLibs.srcDirs = ['libs/release']
265 }
266
267 applicationVariants.all { variant ->
268 variant.outputs.all { output ->
269 String filenameSuffix = variant.flavorName == "mono" ? variant.name : variant.buildType.name
270 output.outputFileName = "android_${filenameSuffix}.apk"
271 }
272 }
273}
274
275task copyAndRenameBinary(type: Copy) {
276 // The 'doNotTrackState' is added to disable gradle's up-to-date checks for output files
277 // and directories. Otherwise this check may cause permissions access failures on Windows
278 // machines.
279 doNotTrackState("No need for up-to-date checks for the copy-and-rename operation")
280
281 String exportFormat = getExportFormat()
282
283 String sourceFilename = ""
284 String sourceFilepath = ""
285 if (exportFormat == "aar") {
286 sourceFilename = "fusedDepsLib.aar"
287
288 def fusedDepsLibProject = findProject(":fusedDepsLib")
289 sourceFilepath = "${fusedDepsLibProject.buildDir}/outputs/aar/$sourceFilename"
290 } else {
291 String exportEdition = getExportEdition()
292 String exportBuildType = getExportBuildType()
293 String exportBuildTypeCapitalized = exportBuildType.capitalize()
294
295 if (exportFormat == "aab") {
296 String filenameSuffix = "${exportEdition}-${exportBuildType}"
297 sourceFilename = "${project.name}-${filenameSuffix}.aab"
298 sourceFilepath = "$buildDir/outputs/bundle/${exportEdition}${exportBuildTypeCapitalized}/$sourceFilename"
299 } else {
300 String filenameSuffix = exportEdition == "mono" ? "${exportEdition}${exportBuildTypeCapitalized}" : exportBuildType
301 sourceFilename = "android_${filenameSuffix}.apk"
302 sourceFilepath = "$buildDir/outputs/apk/$exportEdition/$exportBuildType/$sourceFilename"
303 }
304 }
305
306 from sourceFilepath
307 into getExportPath()
308 rename sourceFilename, getExportFilename()
309}
310
311/**
312 * Used to validate the version of the Java SDK used for the Godot gradle builds.
313 */
314task validateJavaVersion {
315 if (!JavaVersion.current().isCompatibleWith(versions.javaVersion)) {
316 throw new GradleException("Invalid Java version ${JavaVersion.current()}. Version ${versions.javaVersion} is the minimum supported Java version for Godot gradle builds.")
317 }
318}
319
320/*
321 * Older versions of our vendor plugin include a loader that we no longer need.
322 * This code ensures those are removed.
323 */
324tasks.withType( com.android.build.gradle.internal.tasks.MergeNativeLibsTask) {
325 doFirst {
326 externalLibNativeLibs.each { jniDir ->
327 if (jniDir.getCanonicalPath().contains("godot-openxr-") || jniDir.getCanonicalPath().contains("godotopenxr")) {
328 // Delete the 'libopenxr_loader.so' files from the vendors plugin so we only use the version from the
329 // openxr loader dependency.
330 File armFile = new File(jniDir, "arm64-v8a/libopenxr_loader.so")
331 if (armFile.exists()) {
332 armFile.delete()
333 }
334 File x86File = new File(jniDir, "x86_64/libopenxr_loader.so")
335 if (x86File.exists()) {
336 x86File.delete()
337 }
338 }
339 }
340 }
341}
342
343/*
344When they're scheduled to run, the copy*AARToAppModule tasks generate dependencies for the 'app'
345module, so we're ensuring the ':app:preBuild' task is set to run after those tasks.
346 */
347if (rootProject.tasks.findByPath("copyDebugAARToAppModule") != null) {
348 preBuild.mustRunAfter(rootProject.tasks.named("copyDebugAARToAppModule"))
349}
350if (rootProject.tasks.findByPath("copyReleaseAARToAppModule") != null) {
351 preBuild.mustRunAfter(rootProject.tasks.named("copyReleaseAARToAppModule"))
352}