Last active
August 17, 2017 10:03
-
-
Save yatatsu/ece3b5bc4584202de126f547814ffe7a to your computer and use it in GitHub Desktop.
Auto apply buildConfigFields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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