Generate null MapTiler BuildConfig values when not provided

Remove the default values from the Gradle file, the public MapTiler defaults
are now only defined in MapTilerDefaults. buildConfigFieldStr now accepts a
null value.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Benoit Marty
2026-10-14 10:11:08 +02:00
parent 0149e6f65b
commit 799f60b955
4 changed files with 20 additions and 24 deletions
+4 -9
View File
@@ -26,7 +26,7 @@ android {
defaultConfig { defaultConfig {
buildConfigFieldStr( buildConfigFieldStr(
name = "MAPTILER_BASE_URL", name = "MAPTILER_BASE_URL",
value = BuildTimeConfig.SERVICES_MAPTILER_BASE_URL ?: "https://api.maptiler.com/maps" value = BuildTimeConfig.SERVICES_MAPTILER_BASE_URL,
) )
buildConfigFieldStr( buildConfigFieldStr(
name = "MAPTILER_API_KEY", name = "MAPTILER_API_KEY",
@@ -35,8 +35,7 @@ android {
} else { } else {
System.getenv("ELEMENT_ANDROID_MAPTILER_API_KEY") System.getenv("ELEMENT_ANDROID_MAPTILER_API_KEY")
?: readLocalProperty("services.maptiler.apikey") ?: readLocalProperty("services.maptiler.apikey")
} },
?: ""
) )
buildConfigFieldStr( buildConfigFieldStr(
name = "MAPTILER_LIGHT_MAP_ID", name = "MAPTILER_LIGHT_MAP_ID",
@@ -45,9 +44,7 @@ android {
} else { } else {
System.getenv("ELEMENT_ANDROID_MAPTILER_LIGHT_MAP_ID") System.getenv("ELEMENT_ANDROID_MAPTILER_LIGHT_MAP_ID")
?: readLocalProperty("services.maptiler.lightMapId") ?: readLocalProperty("services.maptiler.lightMapId")
} },
// fall back to maptiler's default light map.
?: "basic-v2"
) )
buildConfigFieldStr( buildConfigFieldStr(
name = "MAPTILER_DARK_MAP_ID", name = "MAPTILER_DARK_MAP_ID",
@@ -56,9 +53,7 @@ android {
} else { } else {
System.getenv("ELEMENT_ANDROID_MAPTILER_DARK_MAP_ID") System.getenv("ELEMENT_ANDROID_MAPTILER_DARK_MAP_ID")
?: readLocalProperty("services.maptiler.darkMapId") ?: readLocalProperty("services.maptiler.darkMapId")
} },
// fall back to maptiler's default dark map.
?: "basic-v2-dark"
) )
} }
} }
@@ -19,14 +19,13 @@ val LocalMapTilerConfig = staticCompositionLocalOf { buildTimeMapTilerConfig() }
* The [MapTilerConfig] defined at build time, or null if no MapTiler API key has been provided. * The [MapTilerConfig] defined at build time, or null if no MapTiler API key has been provided.
*/ */
fun buildTimeMapTilerConfig(): MapTilerConfig? { fun buildTimeMapTilerConfig(): MapTilerConfig? {
return BuildConfig.MAPTILER_API_KEY // BuildConfig fields are null when not provided at build time.
.takeIf { it.isNotEmpty() } val apiKey: String? = BuildConfig.MAPTILER_API_KEY
?.let { apiKey -> if (apiKey.isNullOrEmpty()) return null
MapTilerConfig( return MapTilerConfig(
apiKey = apiKey, apiKey = apiKey,
lightStyleId = BuildConfig.MAPTILER_LIGHT_MAP_ID, lightStyleId = BuildConfig.MAPTILER_LIGHT_MAP_ID,
darkStyleId = BuildConfig.MAPTILER_DARK_MAP_ID, darkStyleId = BuildConfig.MAPTILER_DARK_MAP_ID,
baseUrl = BuildConfig.MAPTILER_BASE_URL baseUrl = BuildConfig.MAPTILER_BASE_URL,
) )
}
} }
@@ -11,7 +11,6 @@ package io.element.android.features.location.impl
import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat
import io.element.android.features.enterprise.api.remoteconfig.CustomMapTilerConfigProvider import io.element.android.features.enterprise.api.remoteconfig.CustomMapTilerConfigProvider
import io.element.android.features.enterprise.test.remoteconfig.aMapTilerConfig import io.element.android.features.enterprise.test.remoteconfig.aMapTilerConfig
import io.element.android.features.location.api.BuildConfig
import io.element.android.features.location.api.buildTimeMapTilerConfig import io.element.android.features.location.api.buildTimeMapTilerConfig
import io.element.android.libraries.matrix.test.AN_EXCEPTION import io.element.android.libraries.matrix.test.AN_EXCEPTION
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@@ -30,7 +29,7 @@ class DefaultLocationServiceTest {
) )
runCurrent() runCurrent()
assertThat(locationService.mapTilerConfig.value).isEqualTo(buildTimeMapTilerConfig()) assertThat(locationService.mapTilerConfig.value).isEqualTo(buildTimeMapTilerConfig())
assertThat(locationService.isServiceAvailable().value).isEqualTo(BuildConfig.MAPTILER_API_KEY.isNotEmpty()) assertThat(locationService.isServiceAvailable().value).isEqualTo(buildTimeMapTilerConfig() != null)
} }
@Test @Test
@@ -40,7 +39,7 @@ class DefaultLocationServiceTest {
) )
runCurrent() runCurrent()
assertThat(locationService.mapTilerConfig.value).isEqualTo(buildTimeMapTilerConfig()) assertThat(locationService.mapTilerConfig.value).isEqualTo(buildTimeMapTilerConfig())
assertThat(locationService.isServiceAvailable().value).isEqualTo(BuildConfig.MAPTILER_API_KEY.isNotEmpty()) assertThat(locationService.isServiceAvailable().value).isEqualTo(buildTimeMapTilerConfig() != null)
} }
@Test @Test
@@ -10,14 +10,17 @@ package extension
import com.android.build.api.dsl.VariantDimension import com.android.build.api.dsl.VariantDimension
/**
* Adds a String field to the generated BuildConfig class. If [value] is null, the field will be null.
*/
fun VariantDimension.buildConfigFieldStr( fun VariantDimension.buildConfigFieldStr(
name: String, name: String,
value: String, value: String?,
) { ) {
buildConfigField( buildConfigField(
type = "String", type = "String",
name = name, name = name,
value = "\"$value\"" value = value?.let { "\"$it\"" } ?: "null"
) )
} }