From 72b1db9a2f33ab5d5fffd8ba83c05901eff19bea Mon Sep 17 00:00:00 2001 From: Ax333l Date: Thu, 8 Jan 2026 23:27:02 +0100 Subject: [PATCH] fix(locales): use buildconfig instead of generating kt file --- app/build.gradle.kts | 61 +++++-------------- .../revanced/manager/util/SupportedLocales.kt | 22 ++++--- 2 files changed, 30 insertions(+), 53 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 05f7a000..b805efb6 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -143,6 +143,19 @@ android { (preRelease?.substringAfterLast('.')?.toInt() ?: 99) } vectorDrawables.useSupportLibrary = true + + val resDir = file("src/main/res") + val locales = resDir.listFiles() + .orEmpty() + //noinspection WrongGradleMethod + .filter { it.isDirectory && it.name.matches(Regex("values-[a-z]{2}(-r[A-Z]{2})?")) } + //noinspection WrongGradleMethod + .map { it.name.removePrefix("values-").replace("-r", "-") } + .sorted() + //noinspection WrongGradleMethod + .joinToString(prefix = "{", separator = ",", postfix = "}") { "\"$it\"" } + + buildConfigField("String[]", "SUPPORTED_LOCALES", locales) } buildTypes { @@ -230,10 +243,8 @@ android { buildConfig = true } - android { - androidResources { - generateLocaleConfig = true - } + androidResources { + generateLocaleConfig = true } externalNativeBuild { @@ -242,8 +253,6 @@ android { version = "3.22.1" } } - - sourceSets["main"].kotlin.srcDir(layout.buildDirectory.dir("generated/source/locales")) } kotlin { @@ -251,46 +260,6 @@ kotlin { } tasks { - val generateSupportedLocales by registering { - description = "Generate list of supported locales from resource directories" - - val resDir = file("src/main/res") - val outputDir = layout.buildDirectory.dir("generated/source/locales") - - inputs.dir(resDir) - outputs.dir(outputDir) - - doLast { - val locales = resDir.listFiles() - .orEmpty() - .filter { it.isDirectory && it.name.matches(Regex("values-[a-z]{2}(-r[A-Z]{2})?")) } - .map { it.name.removePrefix("values-").replace("-r", "-") } - .sorted() - .joinToString("\n ") { "Locale.forLanguageTag(\"$it\")," } - - val output = outputDir.get().asFile.resolve("app/revanced/manager/util/GeneratedLocales.kt") - output.parentFile.mkdirs() - output.writeText( - """ - |package app.revanced.manager.util - | - |import java.util.Locale - | - |object GeneratedLocales { - | val SUPPORTED_LOCALES = listOf( - | Locale.ENGLISH,$locales - | ) - |} - """.trimMargin() - ) - } - } - - preBuild { - dependsOn(generateSupportedLocales) - } - - // Needed by gradle-semantic-release-plugin. // Tracking: https://github.com/KengoTODA/gradle-semantic-release-plugin/issues/435. val publish by registering { diff --git a/app/src/main/java/app/revanced/manager/util/SupportedLocales.kt b/app/src/main/java/app/revanced/manager/util/SupportedLocales.kt index f4feeba1..bb5f3cce 100644 --- a/app/src/main/java/app/revanced/manager/util/SupportedLocales.kt +++ b/app/src/main/java/app/revanced/manager/util/SupportedLocales.kt @@ -1,21 +1,22 @@ package app.revanced.manager.util +import android.app.LocaleConfig import android.content.Context import android.os.Build import android.os.LocaleList import androidx.appcompat.app.AppCompatDelegate import androidx.core.os.LocaleListCompat +import app.revanced.manager.BuildConfig import java.util.Locale object SupportedLocales { fun getSupportedLocales(context: Context): List { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - runCatching { - android.app.LocaleConfig(context).supportedLocales?.toList() - }.getOrNull() ?: GeneratedLocales.SUPPORTED_LOCALES - } else { - GeneratedLocales.SUPPORTED_LOCALES - } + var result: List? = null + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) result = runCatching { + LocaleConfig(context).supportedLocales?.toList() + }.getOrNull() + + return result ?: generated } fun getCurrentLocale(): Locale? = @@ -29,4 +30,11 @@ object SupportedLocales { locale.getDisplayName(locale).replaceFirstChar { it.uppercase(locale) } private fun LocaleList.toList() = (0 until size()).map { get(it) } + + private val generated by lazy { + listOf( + Locale.ENGLISH, + *BuildConfig.SUPPORTED_LOCALES.map(Locale::forLanguageTag).toTypedArray() + ) + } }