build.gradle.kts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-FileCopyrightText: 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-3.0-or-later
  3. import android.annotation.SuppressLint
  4. import kotlin.collections.setOf
  5. import org.jetbrains.kotlin.konan.properties.Properties
  6. import org.jlleitschuh.gradle.ktlint.reporter.ReporterType
  7. plugins {
  8. id("com.android.application")
  9. id("org.jetbrains.kotlin.android")
  10. id("kotlin-parcelize")
  11. kotlin("plugin.serialization") version "1.9.20"
  12. id("androidx.navigation.safeargs.kotlin")
  13. id("org.jlleitschuh.gradle.ktlint") version "11.4.0"
  14. }
  15. /**
  16. * Use the number of seconds/10 since Jan 1 2016 as the versionCode.
  17. * This lets us upload a new build at most every 10 seconds for the
  18. * next 680 years.
  19. */
  20. val autoVersion = (((System.currentTimeMillis() / 1000) - 1451606400) / 10).toInt()
  21. @Suppress("UnstableApiUsage")
  22. android {
  23. namespace = "org.yuzu.yuzu_emu"
  24. compileSdkVersion = "android-34"
  25. ndkVersion = "26.1.10909125"
  26. buildFeatures {
  27. viewBinding = true
  28. }
  29. compileOptions {
  30. sourceCompatibility = JavaVersion.VERSION_17
  31. targetCompatibility = JavaVersion.VERSION_17
  32. }
  33. kotlinOptions {
  34. jvmTarget = "17"
  35. }
  36. packaging {
  37. // This is necessary for libadrenotools custom driver loading
  38. jniLibs.useLegacyPackaging = true
  39. }
  40. androidResources {
  41. generateLocaleConfig = true
  42. }
  43. defaultConfig {
  44. // TODO If this is ever modified, change application_id in strings.xml
  45. applicationId = "org.yuzu.yuzu_emu"
  46. minSdk = 30
  47. targetSdk = 34
  48. versionName = getGitVersion()
  49. // If you want to use autoVersion for the versionCode, create a property in local.properties
  50. // named "autoVersioned" and set it to "true"
  51. val properties = Properties()
  52. val versionProperty = try {
  53. properties.load(project.rootProject.file("local.properties").inputStream())
  54. properties.getProperty("autoVersioned") ?: ""
  55. } catch (e: Exception) { "" }
  56. versionCode = if (versionProperty == "true") {
  57. autoVersion
  58. } else {
  59. 1
  60. }
  61. ndk {
  62. @SuppressLint("ChromeOsAbiSupport")
  63. abiFilters += listOf("arm64-v8a")
  64. }
  65. buildConfigField("String", "GIT_HASH", "\"${getGitHash()}\"")
  66. buildConfigField("String", "BRANCH", "\"${getBranch()}\"")
  67. }
  68. val keystoreFile = System.getenv("ANDROID_KEYSTORE_FILE")
  69. signingConfigs {
  70. if (keystoreFile != null) {
  71. create("release") {
  72. storeFile = file(keystoreFile)
  73. storePassword = System.getenv("ANDROID_KEYSTORE_PASS")
  74. keyAlias = System.getenv("ANDROID_KEY_ALIAS")
  75. keyPassword = System.getenv("ANDROID_KEYSTORE_PASS")
  76. }
  77. }
  78. create("default") {
  79. storeFile = file("$projectDir/debug.keystore")
  80. storePassword = "android"
  81. keyAlias = "androiddebugkey"
  82. keyPassword = "android"
  83. }
  84. }
  85. // Define build types, which are orthogonal to product flavors.
  86. buildTypes {
  87. // Signed by release key, allowing for upload to Play Store.
  88. release {
  89. signingConfig = if (keystoreFile != null) {
  90. signingConfigs.getByName("release")
  91. } else {
  92. signingConfigs.getByName("default")
  93. }
  94. resValue("string", "app_name_suffixed", "yuzu")
  95. isMinifyEnabled = true
  96. isDebuggable = false
  97. proguardFiles(
  98. getDefaultProguardFile("proguard-android.txt"),
  99. "proguard-rules.pro"
  100. )
  101. }
  102. // builds a release build that doesn't need signing
  103. // Attaches 'debug' suffix to version and package name, allowing installation alongside the release build.
  104. register("relWithDebInfo") {
  105. isDefault = true
  106. resValue("string", "app_name_suffixed", "yuzu Debug Release")
  107. signingConfig = signingConfigs.getByName("default")
  108. isMinifyEnabled = true
  109. isDebuggable = true
  110. proguardFiles(
  111. getDefaultProguardFile("proguard-android.txt"),
  112. "proguard-rules.pro"
  113. )
  114. versionNameSuffix = "-relWithDebInfo"
  115. applicationIdSuffix = ".relWithDebInfo"
  116. isJniDebuggable = true
  117. }
  118. // Signed by debug key disallowing distribution on Play Store.
  119. // Attaches 'debug' suffix to version and package name, allowing installation alongside the release build.
  120. debug {
  121. signingConfig = signingConfigs.getByName("default")
  122. resValue("string", "app_name_suffixed", "yuzu Debug")
  123. isDebuggable = true
  124. isJniDebuggable = true
  125. versionNameSuffix = "-debug"
  126. applicationIdSuffix = ".debug"
  127. }
  128. }
  129. flavorDimensions.add("version")
  130. productFlavors {
  131. create("mainline") {
  132. isDefault = true
  133. dimension = "version"
  134. buildConfigField("Boolean", "PREMIUM", "false")
  135. }
  136. create("ea") {
  137. dimension = "version"
  138. buildConfigField("Boolean", "PREMIUM", "true")
  139. applicationIdSuffix = ".ea"
  140. }
  141. }
  142. externalNativeBuild {
  143. cmake {
  144. version = "3.22.1"
  145. path = file("../../../CMakeLists.txt")
  146. }
  147. }
  148. defaultConfig {
  149. externalNativeBuild {
  150. cmake {
  151. arguments(
  152. "-DENABLE_QT=0", // Don't use QT
  153. "-DENABLE_SDL2=0", // Don't use SDL
  154. "-DENABLE_WEB_SERVICE=0", // Don't use telemetry
  155. "-DBUNDLE_SPEEX=ON",
  156. "-DANDROID_ARM_NEON=true", // cryptopp requires Neon to work
  157. "-DYUZU_USE_BUNDLED_VCPKG=ON",
  158. "-DYUZU_USE_BUNDLED_FFMPEG=ON",
  159. "-DYUZU_ENABLE_LTO=ON",
  160. "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON"
  161. )
  162. abiFilters("arm64-v8a", "x86_64")
  163. }
  164. }
  165. }
  166. }
  167. tasks.create<Delete>("ktlintReset") {
  168. delete(File(buildDir.path + File.separator + "intermediates/ktLint"))
  169. }
  170. val showFormatHelp = {
  171. logger.lifecycle(
  172. "If this check fails, please try running \"gradlew ktlintFormat\" for automatic " +
  173. "codestyle fixes"
  174. )
  175. }
  176. tasks.getByPath("ktlintKotlinScriptCheck").doFirst { showFormatHelp.invoke() }
  177. tasks.getByPath("ktlintMainSourceSetCheck").doFirst { showFormatHelp.invoke() }
  178. tasks.getByPath("loadKtlintReporters").dependsOn("ktlintReset")
  179. ktlint {
  180. version.set("0.47.1")
  181. android.set(true)
  182. ignoreFailures.set(false)
  183. disabledRules.set(
  184. setOf(
  185. "no-wildcard-imports",
  186. "package-name",
  187. "import-ordering"
  188. )
  189. )
  190. reporters {
  191. reporter(ReporterType.CHECKSTYLE)
  192. }
  193. }
  194. dependencies {
  195. implementation("androidx.core:core-ktx:1.12.0")
  196. implementation("androidx.appcompat:appcompat:1.6.1")
  197. implementation("androidx.recyclerview:recyclerview:1.3.1")
  198. implementation("androidx.constraintlayout:constraintlayout:2.1.4")
  199. implementation("androidx.fragment:fragment-ktx:1.6.1")
  200. implementation("androidx.documentfile:documentfile:1.0.1")
  201. implementation("com.google.android.material:material:1.9.0")
  202. implementation("androidx.preference:preference-ktx:1.2.1")
  203. implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2")
  204. implementation("io.coil-kt:coil:2.2.2")
  205. implementation("androidx.core:core-splashscreen:1.0.1")
  206. implementation("androidx.window:window:1.2.0-beta03")
  207. implementation("androidx.constraintlayout:constraintlayout:2.1.4")
  208. implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")
  209. implementation("androidx.navigation:navigation-fragment-ktx:2.7.4")
  210. implementation("androidx.navigation:navigation-ui-ktx:2.7.4")
  211. implementation("info.debatty:java-string-similarity:2.0.0")
  212. implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
  213. }
  214. fun runGitCommand(command: List<String>): String {
  215. return try {
  216. ProcessBuilder(command)
  217. .directory(project.rootDir)
  218. .redirectOutput(ProcessBuilder.Redirect.PIPE)
  219. .redirectError(ProcessBuilder.Redirect.PIPE)
  220. .start().inputStream.bufferedReader().use { it.readText() }
  221. .trim()
  222. } catch (e: Exception) {
  223. logger.error("Cannot find git")
  224. ""
  225. }
  226. }
  227. fun getGitVersion(): String {
  228. val versionName = if (System.getenv("GITHUB_ACTIONS") != null) {
  229. val gitTag = System.getenv("GIT_TAG_NAME") ?: ""
  230. gitTag
  231. } else {
  232. runGitCommand(listOf("git", "describe", "--always", "--long"))
  233. .replace(Regex("(-0)?-[^-]+$"), "")
  234. }
  235. return versionName.ifEmpty { "0.0" }
  236. }
  237. fun getGitHash(): String =
  238. runGitCommand(listOf("git", "rev-parse", "--short", "HEAD")).ifEmpty { "dummy-hash" }
  239. fun getBranch(): String =
  240. runGitCommand(listOf("git", "rev-parse", "--abbrev-ref", "HEAD")).ifEmpty { "dummy-hash" }