On Android, it is best to keep settings like api url

In my Android application, I need to save the settings for the api url, which depends on the type of assembly (I will have one for debug / staging and release). Saving this in the strings file is not very good, since there are localized lines in this folder, and there is nothing localized in the api url.

What is the best place to keep this URL?

+5
source share
2 answers

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 do not want to save the URL in build.gradle , you can use the debug and release folders to create a special class that 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.

+10
source

The resource file is suitable for this, you do not need to paste into all broadcast folders, and what is nice with them is that you can override them from the library project into the main project.

If you do not want to use them, then the sample class with a constant string.

+1
source

Source: https://habr.com/ru/post/1205342/


All Articles