Define Different Api Endpoint For Production And Staging In Build.gradle

Sometimes when you are in android development and you have production and staging, development.. environments then you have to build apks for them. Then what you do in general cases, change api url in your code and build apk. This is so tedious task which i have already faced when i started android as beginner.
But when i came to know the power of gradle, these problems vanishes.
Gradle is very powerful tool, and you can configure and customize most build stuff here.

In this post i m sharing how to define different api endpoints depending on build type.

Following is sample build.gradle from app Module

In above there are three build types Release, Debug and Stage.
You can see i have defined Custom Variables using def keyword and after that used buildConfigField statement to put these variable’s values to BuildConfig.java which is generated Automatically when you build your apk or sync gradle, Looks like following…

You can now use these values in your code like

Categories: Android,Gradle

Tags: ,

Leave A Reply

Your email address will not be published.