Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Android best place to keep settings like api url

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?

like image 975
Calin Avatar asked Oct 23 '14 08:10

Calin


People also ask

Where is api url stored in Android?

The best way in your case is store api url's in server because you can change them later. You'd create one url to fetch your api urls. Build your app based on an architecture like this. And don't forget anyone can see your server urls.

How do I hide api url and parameters in Android app?

I found a solution to hide base url to keep api secured with NDK. Keep base64 encoded string inside cpp file and call that from java class and decode base64. Include c++ (NDK) support to your project. You can include this to your new or old project.

What is Android App link?

Android App Links are HTTP URLs that bring users directly to specific content in your Android app. Android App Links can drive more traffic to your app, help you discover which app content is used most, and make it easier for users to share and find content in an installed app.

Where is config file in Android?

The config file must be available at the "/sdcard/config. txt" of the android hardware.


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 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.

like image 133
nhaarman Avatar answered Nov 05 '22 15:11

nhaarman


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.

like image 27
Stephane Mathis Avatar answered Nov 05 '22 16:11

Stephane Mathis