In this post i want to explain you how i use Gradle variables to keep my buildscript clearer.

DefaultConfig and Dependencies

The problem

By default you add your dependencies under dependencies in your build.gradle . But the more dependencies your app has, the more confusing it can get if you want to check which version of a dependency you are using or when you want to update a specific one. For example all the google support libraries need the same version number. If you use hardcoded version numbers you need to update every single support library.

My solution

I saw this solution in an example project from google. I keep all variables in dedicated file called constants.gradle

To use these variables i add

on the top of my build.gradle.

Now i can use these variables instead of hard coded values. When using variables in a String you need to use double quotes instead of single qoutes.

As you can see i also used variables for things like applicationId or compileSdk. To use these variables you just use the variable name:


A different kind of variables or respectively constants are buildConfigField’s. You can use these fields to generate the value of a constant depending on your Gradle buildtype.

In this example i have two different base urls for every buildtype. To use the value of API_BASE_URL in my java code i just use BuildConfig.API_BASE_URL.