Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to have a common section with buildConfigField and resValue in gradle?

Tags:

android

gradle

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.

like image 717
peuhse Avatar asked Sep 23 '14 11:09

peuhse


People also ask

How is BuildConfig generated?

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.

How do I get the current build type in gradle?

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 { ... }

What is BuildConfigField?

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.

Where is gradle settings in Android Studio?

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).


1 Answers

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

like image 142
Fifer Sheep Avatar answered Oct 02 '22 07:10

Fifer Sheep