(Android) Build Config — Customable Constant Field

Tegar Bangun Suganda
2 min readJan 29, 2021

--

Mountain Lake Reflection on UHD Wallpaper

Build Config is a java class that Gradle generates it at build time so your app can inspect current information of your build. By default, BuildConfig.java always generated at your app build time in each android module, and it comes with variables e.g., DEBUG, APPLICATION_ID, BUILD_TYPE and etc.

build.gradle in app module

That is how I use buildConfigField that later it will generates variable in BuildConfig.java as I mention before. Let’s make it easier to read.

buildConfigField FirstParameter, SecondParameter, ThirdParameter// OrbuildConfigField(FirstParameter, SecondParameter, ThirdParameter)
  • buildConfigField is custom field definition for the generated BuildConfig.java
  • FirstParameter is field’s type data what you determine to be used
  • SecondParameter is field’s name inside BuildConfig.java
  • ThirdParameter is field’s value inside BuildConfig.java

They have 3 things in common, FirstParameter, SecondParameter, ThirdParameter always written inside double quotes. You can use def too then use it as variable for SecondParameter or ThirdParameter. For ThirdParameter if you use String as field’s value, it must be written as escaped string because it has two double quotes nested.

an example of BuildConfig.java that generated with debug build

You can use it easily inside your class, only need to import your BuildConfig.java then use it BuildConfig.SERVER_BASE_URL .

Take a note, if you do modify of your existing BuildConfig class e.g., add a new fields or modify its value, you need to Rebuild your project. It will delete the existed one, and generates a new one.

Make sure you use it wisely, usually I use it as differentiator between debug and release build. It is not a class that always inside your module, it was generated, and generating class of course would taking your build time if you define more fields inside debug or release build.

--

--

Tegar Bangun Suganda
Tegar Bangun Suganda

No responses yet