aboutsummaryrefslogtreecommitdiff
path: root/src/android/app/build.gradle.kts
blob: 69dd6a8e4f1c1726c1fef08ac46304505f43e1e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later

// SPDX-FileCopyrightText: Copyright yuzu/Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

// import android.annotation.SuppressLint
import com.android.build.gradle.api.ApplicationVariant
import kotlin.collections.setOf
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType
import com.github.triplet.gradle.androidpublisher.ReleaseStatus
import org.gradle.api.tasks.Copy

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("kotlin-parcelize")
    kotlin("plugin.serialization") version "1.9.20"
    id("androidx.navigation.safeargs.kotlin")
    id("org.jlleitschuh.gradle.ktlint") version "11.4.0"
    id("com.github.triplet.play") version "3.8.6"
    id("idea")
}

/**
 * Use the number of seconds/10 since Jan 1 2016 as the versionCode.
 * This lets us upload a new build at most every 10 seconds for the
 * next 680 years.
 */
val autoVersion = (((System.currentTimeMillis() / 1000) - 1451606400) / 10).toInt()

val edenDir = project(":Eden").projectDir

@Suppress("UnstableApiUsage")
android {
    namespace = "org.yuzu.yuzu_emu"

    compileSdkVersion = "android-36"
    ndkVersion = "28.2.13676358"

    val isNightly =
        providers.gradleProperty("nightly").orNull?.toBooleanStrictOrNull() ?: false

    buildFeatures {
        viewBinding = true
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = "17"
    }

    packaging {
        // This is necessary for libadrenotools custom driver loading
        jniLibs.useLegacyPackaging = true
    }

    androidResources {
        generateLocaleConfig = true
    }

    defaultConfig {
        applicationId = "dev.eden.eden_emulator"
        minSdk = 24
        targetSdk = 36
        versionName = getGitVersion()
        versionCode = autoVersion

        externalNativeBuild {
            cmake {
                val extraCMakeArgs =
                    (project.findProperty("YUZU_ANDROID_ARGS") as String?)?.split("\\s+".toRegex())
                        ?: emptyList()

                arguments.addAll(
                    listOf(
                        "-DENABLE_QT=0", // Don't use QT
                        "-DENABLE_WEB_SERVICE=1", // Enable web service
                        "-DANDROID_ARM_NEON=true", // cryptopp requires Neon to work
                        "-DYUZU_USE_CPM=ON",
                        "-DCPMUTIL_FORCE_BUNDLED=ON",
                        "-DYUZU_USE_BUNDLED_FFMPEG=ON",
                        "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
                        "-DBUILD_TESTING=OFF",
                        "-DYUZU_TESTS=OFF",
                        "-DDYNARMIC_TESTS=OFF",
                        *extraCMakeArgs.toTypedArray()
                    )
                )

                if (isNightly) {
                    arguments.addAll(listOf(
                        "-DENABLE_UPDATE_CHECKER=ON",
                        "-DNIGHTLY_BUILD=ON",
                    ))
                }

                abiFilters("arm64-v8a")
            }
        }
    }

    val keystoreFile = System.getenv("ANDROID_KEYSTORE_FILE")
    signingConfigs {
        if (keystoreFile != null) {
            create("release") {
                storeFile = file(keystoreFile)
                storePassword = System.getenv("ANDROID_KEYSTORE_PASS")
                keyAlias = System.getenv("ANDROID_KEY_ALIAS")
                keyPassword = System.getenv("ANDROID_KEYSTORE_PASS")
            }
        }
        create("default") {
            storeFile = file("$projectDir/debug.keystore")
            storePassword = "android"
            keyAlias = "androiddebugkey"
            keyPassword = "android"
        }
    }

    // The app name is constructed with the appNameSuffix and appNameBase manifest placeholders
    // suffix is used for build type--remember to include a space beforehand

    // Define build types, which are orthogonal to product flavors.
    buildTypes {
        // Signed by release key, allowing for upload to Play Store.
        release {
            signingConfig = if (keystoreFile != null) {
                signingConfigs.getByName("release")
            } else {
                signingConfigs.getByName("default")
            }

            if (isNightly) {
                applicationIdSuffix = ".nightly"
                manifestPlaceholders += mapOf("appNameSuffix" to " Nightly")
            } else {
                manifestPlaceholders += mapOf("appNameSuffix" to "")
            }

            isMinifyEnabled = true
            isDebuggable = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }

        // builds a release build that doesn't need signing
        // Attaches 'debug' suffix to version and package name, allowing installation alongside the release build.
        register("relWithDebInfo") {
            isDefault = true
            signingConfig = signingConfigs.getByName("default")
            isDebuggable = true
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )

            manifestPlaceholders += mapOf("appNameSuffix" to " Debug Release")

            versionNameSuffix = "-relWithDebInfo"
            applicationIdSuffix = ".relWithDebInfo"
            isJniDebuggable = true
        }

        // Signed by debug key disallowing distribution on Play Store.
        // Attaches 'debug' suffix to version and package name, allowing installation alongside the release build.
        debug {
            signingConfig = signingConfigs.getByName("default")
            isDebuggable = true
            isJniDebuggable = true
            versionNameSuffix = "-debug"
            applicationIdSuffix = ".debug"

            manifestPlaceholders += mapOf("appNameSuffix" to " Debug")
        }
    }

    // appNameBase is used for the primary identifier
    // this should be "Eden <flavorName>"
    flavorDimensions.add("version")
    productFlavors {
        create("mainline") {
            dimension = "version"
            isDefault = true
            minSdk = 33

            manifestPlaceholders += mapOf("appNameBase" to "Eden")
            resValue("string", "app_name_suffixed", "Eden")

            ndk {
                abiFilters += listOf("arm64-v8a")
            }
        }

        create("genshinSpoof") {
            dimension = "version"
            minSdk = 35
            manifestPlaceholders += mapOf("appNameBase" to "Eden Optimized")
            resValue("string", "app_name_suffixed", "Eden Optimized")
            applicationId = "com.miHoYo.Yuanshen"

            externalNativeBuild {
                cmake {
                    arguments.add("-DGENSHIN_SPOOF=ON")
                }
            }

            ndk {
                abiFilters += listOf("arm64-v8a")
            }
        }

        create("legacy") {
            dimension = "version"
            minSdk = 29
            manifestPlaceholders += mapOf("appNameBase" to "Eden Legacy")
            resValue("string", "app_name_suffixed", "Eden Legacy")
            applicationId = "dev.legacy.eden_emulator"

            externalNativeBuild {
                cmake {
                    arguments.add("-DYUZU_LEGACY=ON")
                }
            }

            sourceSets {
                getByName("legacy") {
                    res.srcDirs("src/main/legacy")
                }
            }

            ndk {
                abiFilters += listOf("arm64-v8a")
            }
        }

        create("chromeOS") {
            dimension = "version"
            manifestPlaceholders += mapOf("appNameBase" to "Eden ChromeOS")
            resValue("string", "app_name_suffixed", "Eden ChromeOS")

            ndk {
                abiFilters += listOf("x86_64")
            }

            externalNativeBuild {
                cmake {
                    abiFilters("x86_64")
                }
            }
        }
    }

    externalNativeBuild {
        cmake {
            version = "3.31.5"
            path = file("${edenDir}/CMakeLists.txt")
        }
    }

    productFlavors.all {
        val currentName = manifestPlaceholders["appNameBase"] as? String ?: "Eden"
        val suffix = if (isNightly) " Nightly" else ""

        // apply nightly suffix I/A
        resValue("string", "app_name_suffixed", "$currentName$suffix")
        resValue("string", "app_name", "Eden$suffix")
    }

    sourceSets {
        named("main") {
            java.srcDir("${edenDir}/externals/generated/sdl/java")
            kotlin.srcDir("${edenDir}/externals/generated/sdl/java")
        }
    }
}

idea {
    module {
        // Inclusion to exclude build/ dir from non-Android
        excludeDirs.add(file("${edenDir}/build"))

        // also exclude CPM cache from automatic indexing
        excludeDirs.add(file("${edenDir}/.cache"))
    }
}

tasks.register<Delete>("ktlintReset", fun Delete.() {
    delete(File(layout.buildDirectory.toString() + File.separator + "intermediates/ktLint"))
})

val showFormatHelp = {
    logger.lifecycle(
        "If this check fails, please try running \"gradlew ktlintFormat\" for automatic " +
                "codestyle fixes"
    )
}
tasks.getByPath("ktlintKotlinScriptCheck").doFirst { showFormatHelp.invoke() }
tasks.getByPath("ktlintMainSourceSetCheck").doFirst { showFormatHelp.invoke() }
tasks.getByPath("loadKtlintReporters").dependsOn("ktlintReset")

ktlint {
    version.set("0.47.1")
    android.set(true)
    ignoreFailures.set(false)
    disabledRules.set(
        setOf(
            "no-wildcard-imports",
            "package-name",
            "import-ordering"
        )
    )
    reporters {
        reporter(ReporterType.CHECKSTYLE)
    }
}

play {
    val keyPath = System.getenv("SERVICE_ACCOUNT_KEY_PATH")
    if (keyPath != null) {
        serviceAccountCredentials.set(File(keyPath))
    }
    track.set(System.getenv("STORE_TRACK") ?: "internal")
    releaseStatus.set(ReleaseStatus.COMPLETED)
}

dependencies {
    implementation("androidx.core:core-ktx:1.15.0")
    implementation("androidx.appcompat:appcompat:1.7.0")
    implementation("androidx.recyclerview:recyclerview:1.4.0")
    implementation("androidx.constraintlayout:constraintlayout:2.2.1")
    implementation("androidx.fragment:fragment-ktx:1.8.6")
    implementation("androidx.documentfile:documentfile:1.0.1")
    implementation("com.google.android.material:material:1.12.0")
    implementation("androidx.preference:preference-ktx:1.2.1")
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.7")
    implementation("com.squareup.okhttp3:okhttp:4.12.0")
    implementation("io.coil-kt:coil:2.2.2")
    implementation("androidx.core:core-splashscreen:1.0.1")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.17.2")
    implementation("androidx.window:window:1.3.0")
    implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")
    implementation("org.commonmark:commonmark:0.22.0")
    implementation("androidx.navigation:navigation-fragment-ktx:2.8.9")
    implementation("androidx.navigation:navigation-ui-ktx:2.8.9")
    implementation("info.debatty:java-string-similarity:2.0.0")
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
    implementation("androidx.compose.ui:ui-graphics-android:1.7.8")
    implementation("androidx.compose.ui:ui-text-android:1.7.8")
    implementation("net.swiftzer.semver:semver:2.0.0")
}

fun runGitCommand(command: List<String>): String {
    return try {
        ProcessBuilder(command)
            .directory(project.rootDir)
            .redirectOutput(ProcessBuilder.Redirect.PIPE)
            .redirectError(ProcessBuilder.Redirect.PIPE)
            .start()
            .inputStream.bufferedReader()
            .use { it.readText() }
            .trim()
    } catch (e: Exception) {
        logger.error("Cannot find git")
        ""
    }
}

fun getGitVersion(): String {
    val gitVersion = runGitCommand(
        listOf(
            "git",
            "describe",
            "--always",
            "--long"
        )
    ).replace(Regex("(-0)?-[^-]+$"), "")
    val versionName = if (System.getenv("GITHUB_ACTIONS") != null) {
        System.getenv("GIT_TAG_NAME") ?: gitVersion
    } else {
        gitVersion
    }
    return versionName.ifEmpty { "0.0" }
}

afterEvaluate {
    val artifactsDir = layout.projectDirectory.dir("${edenDir}/artifacts")
    val outputsDir = layout.buildDirectory.dir("outputs").get()

    android.applicationVariants.forEach { variant ->
        val variantName = variant.name
        val variantTask = variantName.replaceFirstChar { it.uppercaseChar() }

        val flavor = variant.flavorName
        val type = variant.buildType.name

        val baseName = "app-$flavor-$type"

        val apkFile = outputsDir.file("apk/$flavor/$type/$baseName.apk")
        val aabFile = outputsDir.file("bundle/$variantName/$baseName.aab")

        val taskName = "copy${variantTask}Outputs"

        tasks.register<Copy>(taskName) {
            group = "publishing"
            description = "Copy APK and AAB for $variantName to $artifactsDir"

            from(apkFile)
            from(aabFile)
            into(artifactsDir)

            dependsOn("assemble${variantTask}")
            dependsOn("bundle${variantTask}")
        }
    }
}