Set variables in build.gradle
Updated in Android GradleThe new Gradle based build system offers a lot of cool features for Android developers. One of those is the ability to parametrize application build process. This means that you can create different variants of the application and set variables based on the variant or build type. You can then reference these variables in the application code and tweak the application usage based on those.
Basics
In this tutorial we’ll focus on setting different variables based on the build, so all of the “magic” is done in the application’s build.gradle file.
There are two types of variables that can be set, build configurations and resources. They both server different purpose and we’re going to go through them next.
Build configuration variables
Build configuration variables, as the name implies, are used to set build
configuration variables. Variables introduced in build.gradle with
buildConfigField are added to the BuildConfig
class created at build time.
For example the following setup creates a enableDebugLogging boolean variable
to BuildConfig
class. The value of the variable depends on the build type,
when creating a release build the variable is false and true when building a
debug build.
buildTypes {
release {
buildConfigField "boolean", "enableDebugLogging", "false"
buildConfigField "String", "API_KEY", "\"0987654321\""
}
debug {
buildConfigField "boolean", "enableDebugLogging", "true"
buildConfigField "String", "API_KEY", "\"1234567890\""
}
}
With BuildConfig you can use variable in your code like this:
if (BuildConfig.enableDebugLogging) {
Log.d(TAG, "Debug message here");
}
So as the example shows, you can use the BuildConfig variables directly in your code. That is, the build process creates the Java variables the same way as the developer had written them.
Since buildConfigField creates Java code, all common Java types are supported as the field type. But do note that when creating a string value, you need to have the string in escaped quotes so that valid Java code is created to the BuildConfig file.
Resource variables
Resource variables are little different than build configuration variables. The basic idea is the same but with resource variables the build process creates resources not Java code. Resource variables are created with resValue keyword.
The following example shows how to introduce some resource variables in gradle.build file depending on the build type.
buildTypes {
release {
resValue "string", "build_type", "Release"
resValue "bool", "debug", "false"
resValue "dimen", "margin", "16dp"
}
debug {
resValue "string", "build_type", "Debug"
resValue "bool", "debug", "true"
resValue "dimen", "margin", "8dp"
}
}
When you use those variables in your code you use them same way as with the resources you added to the res directory:
buildTypeView.setText(R.string.build_type);
Other resource can be used directly in the xml files like this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin"
android:enabled="@bool/debug"/>
You can use all resource types as the type for resValue:
- array
- attr
- bool
- color
- declare-styleable
- dimen
- fraction
- id
- integer
- plurals
- string
- style
Conclusion
Setting variables based on build type and build variant is quite a handy feature. You can automatically enable and disable debug logging depending on which build are you working on. Also setting different ids and keys for third party libraries automatically based on build type is sometimes pretty handy.
The most powerful thing about the variables set in build.gradle is that they’re handled automatically, so there’s no need to anymore changes variables depending which build you’re making.
Screenshots
Source code
Source code for the whole example project is available here