I have a product with multiple product flavors like:
buildTypes { debug { } release { } } productFlavors { flavor1 { buildConfigField "String" "country" "se" buildConfigField "String" "language" "sv-SE" buildConfigField "String" "appName" "Flavor1" } flavor2 { buildConfigField "String" "country" "se" buildConfigField "String" "language" "sv-SE" buildConfigField "String" "appName" "Flavor2" } flavor3 { buildConfigField "String" "country" "se" buildConfigField "String" "language" "sv-SE" buildConfigField "String" "appName" "Flavor3" } flavor4 { buildConfigField "String" "country" "se" buildConfigField "String" "language" "sv-SE" buildConfigField "String" "appName" "Flavor4" } flavor5 { buildConfigField "String" "country" "se" buildConfigField "String" "language" "no-NO" buildConfigField "String" "appName" "Flavor5" } }
I would prefer a common section with all properties and only override those that are different. Is this possible?
I would also like to put all flavors (and perhaps buildTypes) in it's own file to make it more readable. So whenever you have to change a flavor, you can easily find it in its own file, and not have to scroll over thousands of line which it will be if I have all flavors and buildTypes together with all the rest in the main build file.
There's a class called BuildConfig. java which is automatically generated by the build system. This class is updated automatically by Android's build system (like the R class). It already contains a static final boolean called DEBUG, which is normally set to true.
In your build. gradle (:app): tasks. all { Task task -> if (task.name == "preDebugBuild") { doFirst { //for debug build } } else if (task.name == "preReleaseBuild") { doFirst { //for release build } } } dependencies { ... }
BuildConfigField. Gradle allows buildConfigField lines to define constants. These constants will be accessible at runtime as static fields of the BuildConfig class. This can be used to create flavors by defining all fields within the defaultConfig block, then overriding them for individual build flavors as needed.
Open your project in Android Studio and select File > Settings... > Build, Execution, Deployment > Build Tools > Gradle (Android Studio > Preferences... > Build, Execution, Deployment > Build Tools > Gradle on a Mac).
Selvin is correct, use the defaultConfig
closure - there is no neater way! In the following example, flavors 1, 2 & 5 would set the default country and language to de
. Flavors 3 & 4 override this with their own languages.
defaultConfig { buildConfigField "String", "country", "de" buildConfigField "String", "language", "de" } buildTypes { debug { } release { } } productFlavors { flavor1 { buildConfigField "String", "appName", "Flavor1" } flavor2 { buildConfigField "String", "appName", "Flavor2" } flavor3 { buildConfigField "String", "country", "uk" buildConfigField "String", "language", "en_GB" buildConfigField "String", "appName", "Flavor3" } flavor4 { buildConfigField "String", "country", "fr" buildConfigField "String", "language", "fr" buildConfigField "String", "appName", "Flavor4" } flavor5 { buildConfigField "String", "appName", name.capitalize() } }
NOTE
Just an FYI that you can use name.capitalize()
to turn the name of any flavour, e.g. flavor5
, into the app name of Flavor5
by using the capitalize()
method - which will capitalize the first character in the String. However, this MUST go in the flavor, not defaultConfig
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With