1

I have an Android project that is communicating with an API. However, the API URL is hardcoded into the code.

Coming from a Rails background, it is possible to communicate to a staging server or a production server based on the Rails environment on which is defined in a config file.

I found BuildConfig.java which contains a DEBUG variable but warns:

/** Automatically generated file. DO NOT MODIFY */

How can I determine which server to communicate to with Android with best practice? Is there a config file to do so?

Marco Lau
  • 577
  • 1
  • 7
  • 15
  • [This](http://stackoverflow.com/a/23844716/640731) answer might give you some ideas. You will have to add a run time check to switch between different urls – Krishnabhadra Jan 29 '15 at 09:21
  • @Krishnabhadra I got the idea that I should be using `BuildConfig.DEBUG`. However, seeing as there is no config directory in the project, I am wondering whether I should create one. I don't believe it is best practice to just run an if statement in the source file to choose one URL or the other. – Marco Lau Jan 29 '15 at 09:41
  • possible duplicate of [Android - switching between Development and Production Web Services](http://stackoverflow.com/questions/10486189/android-switching-between-development-and-production-web-services) – Antti Haapala -- Слава Україні Feb 06 '15 at 07:22

2 Answers2

0

You can use the Product Flavors for your app i.e. one flavor for dev, another for staging and another for production. Give unique applicationId for each flavor.

Then in your code, check what product flavor you're using by checking the value of BuildConfig.APPLICATION_ID and set the base url accordingly.

Hope this helps.

Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114
-2

You can use different Base Url for both staging server or a production server from Rails .And on testing for Staging put the Staging URl and in main Put the Production URl. exp: http://stage.example.com/filename.

http://example.com/filename.

A-Droid Tech
  • 2,301
  • 24
  • 33