From 7787ccfcc281ec55732977dbf5520992894fc33c Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Thu, 10 Aug 2023 16:13:19 -0400 Subject: [PATCH] Add resources DSL This streamlines configuration of enabling `androidResources` and enforces use of a resource prefix to avoid conflicts. May your avatars never be wrongly sized again. --- docs/dsl.md | 6 ++++++ .../main/kotlin/slack/gradle/SlackExtension.kt | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/docs/dsl.md b/docs/dsl.md index 773188798..78115743e 100644 --- a/docs/dsl.md +++ b/docs/dsl.md @@ -65,6 +65,12 @@ By default, SGP disables androidTests in projects. These can be enabled via the This is important for opting in tests to [AndroidTest APK Aggregation](/utilities/#androidtest-apk-aggregation). +### Resources + +By default, we disable _Android_ resources (different from _Java_ resources) and libraries have to opt-in to using them. + +This can be enabled via the `resources()` feature, which will enable the relevant `BuildFeature` in the Android plugin and also takes a required `prefix` parameter that is used as the required `resourcePrefix` for that library's resources to avoid naming conflicts. + ## Android Application Features ### Permission AllowList diff --git a/slack-plugin/src/main/kotlin/slack/gradle/SlackExtension.kt b/slack-plugin/src/main/kotlin/slack/gradle/SlackExtension.kt index 73c3ac7e2..1b27d472f 100644 --- a/slack-plugin/src/main/kotlin/slack/gradle/SlackExtension.kt +++ b/slack-plugin/src/main/kotlin/slack/gradle/SlackExtension.kt @@ -18,6 +18,7 @@ package slack.gradle import com.android.build.api.dsl.CommonExtension +import com.android.build.gradle.LibraryExtension import com.squareup.anvil.plugin.AnvilExtension import dev.zacsweers.moshix.ir.gradle.MoshiPluginExtension import javax.inject.Inject @@ -813,6 +814,20 @@ public abstract class AndroidFeaturesHandler @Inject constructor() { androidExtension!!.testOptions.unitTests.isIncludeAndroidResources = true robolectric.setDisallowChanges(true) } + + /** + * **LIBRARIES ONLY** + * + * Enables android resources in this library and enforces use of the given [prefix] for all + * resources. + */ + public fun resources(prefix: String) { + val libraryExtension = + androidExtension as? LibraryExtension + ?: error("slack.android.features.resources() is only applicable in libraries!") + libraryExtension.resourcePrefix = prefix + libraryExtension.buildFeatures { androidResources = true } + } } @SlackExtensionMarker