Skip to content

Instantly share code, notes, and snippets.

@yatatsu
Last active August 17, 2017 10:03
Show Gist options
  • Save yatatsu/ece3b5bc4584202de126f547814ffe7a to your computer and use it in GitHub Desktop.
Save yatatsu/ece3b5bc4584202de126f547814ffe7a to your computer and use it in GitHub Desktop.
Auto apply buildConfigFields
ext.applyBuildConfigFields = { def variant, Map<String, Map<String, Object>> configs ->
def flavor = variant.productFlavors.get(0).name
configs.each { key, entries ->
def name = key.toUpperCase()
def entry = entries.find { it.key == flavor }
if (entry == null) throw new InvalidUserDataException("$key doesn't have $flavor value")
if (entry.value instanceof String) {
variant.buildConfigField(entry.value.class.name, name, "\"$entry.value\"")
} else {
variant.buildConfigField(entry.value.class.name, name, "$entry.value")
}
}
}
ext {
configs = [
test_string: [
mock: "1",
develop: "2",
staging: "3",
production: "4"
],
[
test_integer: [
mock: 1,
develop: 2,
staging: 3,
production: 4
]
]
}
android {
applicationVariants.all { variant ->
applyBuildConfigFields(variant, configs)
}
}
// generate below...
// class BuildConfig {
// public static final String TEST_STRING = "1";
// public static final Integer TEST_INTEGER = 1;
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment