Skip to content

Instantly share code, notes, and snippets.

@tugceaktepe
Created July 12, 2023 20:52
Show Gist options
  • Save tugceaktepe/335476c67bcb9ce280d6483630d89435 to your computer and use it in GitHub Desktop.
Save tugceaktepe/335476c67bcb9ce280d6483630d89435 to your computer and use it in GitHub Desktop.
module&&flavor buildconfig management
import com.android.build.api.dsl.ApplicationExtension
import com.android.build.api.dsl.ApplicationProductFlavor
import com.android.build.api.dsl.CommonExtension
import com.android.build.api.dsl.ProductFlavor
import org.gradle.api.Project
@Suppress("EnumEntryName")
enum class FlavorDimension {
contentType
}
@Suppress("EnumEntryName")
enum class MyFlavor(
val dimension: FlavorDimension,
val applicationIdSuffix: String? = null,
val appName: String? = "null"
) {
dev(FlavorDimension.contentType, applicationIdSuffix = ".dev", appName = "MyApp - Dev"),
stage(FlavorDimension.contentType, applicationIdSuffix = ".qa", appName = "MyApp - Stage"),
prod(FlavorDimension.contentType),
}
fun configureFlavors(
project: Project,
commonExtension: CommonExtension<*, *, *, *>,
flavorConfigurationBlock: ProductFlavor.(flavor: MyFlavor) -> Unit = {}
) {
commonExtension.apply {
flavorDimensions += FlavorDimension.contentType.name
productFlavors {
MyFlavor.values().forEach {
create(it.name) {
dimension = it.dimension.name
flavorConfigurationBlock(this, it)
if (this@apply is ApplicationExtension && this is ApplicationProductFlavor) {
applicationId = if (it.applicationIdSuffix != null) {
"com.example.myapp" + it.applicationIdSuffix
} else {
"com.example.myapp"
}
if (it.appName != null) {
resValue("string", "app_name", it.appName)
}
}
if (project.name == "common") {
buildFeatures.buildConfig = true
println("buildFeatures.buildConfig = ${buildFeatures.buildConfig} - ${project.name}")
buildConfigField(
"String",
"Header",
"\"Header\""
)
buildConfigField(
"String",
"PackageName",
"\"com.example.myapp\""
)
} else if (project.name == "network") {
buildFeatures.buildConfig = true
println("buildFeatures.buildConfig = ${buildFeatures.buildConfig} - ${project.name}")
when (name) {
"dev" -> {
buildConfigField(
"String",
"Url",
"\"https:example-dev.com\""
)
}
"qa" -> {
buildConfigField(
"String",
"Url",
"\"https:example-qa.com\""
)
}
else {
buildConfigField(
"String",
"Url",
"\"https:example.com\""
)
}
}
} else {
buildFeatures.buildConfig = false
println("buildFeatures.buildConfig = ${buildFeatures.buildConfig} - ${project.name}")
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment