(Android) Build Config — Customable Constant Field
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.
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 generatedBuildConfig.java
FirstParameter
is field’s type data what you determine to be usedSecondParameter
is field’s name insideBuildConfig.java
ThirdParameter
is field’s value insideBuildConfig.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.
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.
If you enjoyed with this article, you should check this out!