How to manage multiple END_POINTS or BASE_URL when developing android app.

Build Variants


In this blog, we learn how to manage multiple End_Points/Base_url/Server_url for the project.
As every developer knows that there is multiple URL in their single project, 
for example one for development, one for production, one for testing, and many more.

Build Variants


in most of the case minimum, two URL is a must.
one for production and one for development.

so how to manage multiple URLs, 

1- if I am installing an app in debug mode( development), then I want the development URL, which will interact with my local DB.
2- and if I'm generating release mode .apk file then I want production URL which will interact with the server.

the first and very known pattern is to define both URLs with the same name, and when need to debug URL, "uncomment the test/debug end_point and comment the production URL " and when the need for production URL do the vice-versa.

is it a good way, a good programming approach?
no never.  
everyone knows that this task is very hectic.

here is the best way to handle multiple URLs.

1- look in your app-level grade, you will find buildTypes{}.
2- customize your build type like this.


buildTypes {
release {
buildConfigField "String",
"END_POINT",'"production_url"'----here is some default stmt----
    }
debug{

buildConfigField "String",
 "END_POINT", '"test_url"'    }
}

Note- "END_POINT" is just a string, you can write your own string.
3- sync your project
4- all set here
5- now where you want to use your URL use with the given statement

BuildConfig.END_POINT

example: 
retrofit=new Retrofit.Builder()
 .baseUrl(BuildConfig.END_POINT)
.client(client)
 .build();


where end_point is your defined string for base URL in Gradle.

what magic here?

for now, if you 're installing debug mode .apk, then debug END_POINT will use, and if you will install release .apk then release END_POINT will use automatically.

try it, surely you will like it.



thanks for being here.
#staySafe #stayCode #stayHappy 

for more interesting android blogs click here

2 comments: