Jens Klingenberg

Android LPT 1: Use variables in Gradle

Posted on January 4, 2018  •  2 minutes  • 333 words
Table of contents

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

DefaultConfig and Dependencies

The problem

dependencies {
...
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'
implementation "com.jakewharton:butterknife:8.8.1"
annotationProcessor "com.jakewharton:butterknife-compiler:8.8.1"
}

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
project.ext {
  applicationId = "jensklingenberg.de.exampleexoplayer"
  compileSdk = 27
  targetSdk = 27
  minSdk = 21
  versionCode = 1
  versionName = "1.0"
  buildTools = '27.0.1'
  junitVersion = '4.12'
  appVersion = '1.0'
  butterknifeVersion = '8.8.1'
  supportLibVersion = '27.0.2'
  exoplayerVersion = '2.6.0'
}

To use these variables i add

apply from: 'constants.gradle'

on the top of my build.gradle.

dependencies {
...
implementation "com.android.support:appcompat-v7:${supportLibVersion}"
implementation "com.google.android.exoplayer:exoplayer:${exoplayerVersion}"
implementation "com.jakewharton:butterknife:${butterknifeVersion}"
annotationProcessor "com.jakewharton:butterknife-compiler:${butterknifeVersion}"}

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:

compileSdkVersion compileSdk
defaultConfig {
applicationId applicationId
...
}

BuildConfigField

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

debug {
buildConfigField "String", "API_BASE_URL", '"https://test.de/testing/api/v1/"'
...
}
release { 
buildConfigField "String", "API_BASE_URL", '"https://test.de/prod/api/v1/"'
...
}
}
new Retrofit.Builder().addConverterFactory(MoshiConverterFactory.create(moshi))
            .addCallAdapterFactory(new LiveDataCallAdapterFactory())
            .baseUrl(BuildConfig.API_BASE_URL)
            .client(App.getInstance().getOkHttpClient())
.build();

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.

Let's connect: