Where to store global constants in an Android application?

If values for your constants depend on environment (density, locale etc.) then you should use resources for storing them (integer, string, dimen etc.).

In another case you can put your global constants in one file (best practices - use prefixes for every set of constants) or put local constants in related classes (for instance, Intent holds flags. extras, categories and so on).


Use public static final values. and keep them in separate java file as follows:

    static String QC    = "http:/************";
    static String DEV   = "http:/************";
    static String CLOUD = "http:/************";


    static String SERVICEURL = CLOUD ; //Use this SERVICEURL in your code at run time

Another solution might be to use the resource file (if you are content with storing only string values).

This could be used to store constants such as the account that this application manages:

Ex. WelcomeActivity.java

AccountManager am = AccountManager.get(WelcomeActivity.this);
Account account = am.getAccountsByType(getResources().getString(R.string.ACCOUNT_TYPE))[0];

Ex. res/values/strings.xml

<resources>
    <string name="ACCOUNT_NAME">com.acme.MyAccountSignature</string>
</resources>

This would also allow you to modify this without the need to recompile (similarly to how you would normally decouple translations, which the strings.xml file is best used for).


Create a class constants in your base package folder.

(or create an interface instead of a class so there is no need to reference the class everytime, however this is bad practice due to code readability, but it will work)

Fill it with public static final values.

Moreover, both the class as well as the interface can also be declared as abstract.