9

In my android app, I need to keep the settings for the api url, that varies based on the type of build (I will have one for debug/staging and release). Keeping this into the strings file does not feel right since this folder has localized strings and there is nothing to localize about the api url.

What is the best place to keep this url in?

Calin
  • 6,661
  • 7
  • 49
  • 80
  • which build tool are you using ? ant ? gradle ? – Zakaria Oct 23 '14 at 08:41
  • possible duplicate of [Gradle check constant value to show error if true](http://stackoverflow.com/questions/26489564/gradle-check-constant-value-to-show-error-if-true) – Zakaria Oct 23 '14 at 08:46

2 Answers2

19

You can use the BuildConfig class, for example:

android {
    buildTypes {
        debug {
            buildConfigField 'String', 'API_URL', 'http://...'
        }

        release {
            buildConfigField 'String', 'API_URL', 'http://...'
        }
    }
}

Or, if you don't want to store the URL's in build.gradle, you can use the debug and release folders to create a special class which stores the URL:

  • debug\src\java\com\myapp\ApiParam.java
  • release\src\java\com\myapp\ApiParam.java

Now, in your main project, you can reference the ApiParam class.

nhaarman
  • 98,571
  • 55
  • 246
  • 278
1

Resource file are fine for that, you don't have to put in in all the translation folders and what is nice about them is that you can override them from a library project to the main project.

If you don't want to use them, then a sample class with a constant string.

Stephane Mathis
  • 6,542
  • 6
  • 43
  • 69