Where to store global constants in an Android appl

2020-07-01 07:50发布

I would know what is the best practice for storing global constants which can change with the environnement (debug, preprod, prod, release, etc) at compile time.

In iOS, I used to keep all global constants in a header file and change it with pre-processor macro see this answer:

Where to store global constants in an iOS application?

What solution should I use for Android ?

6条回答
别忘想泡老子
2楼-- · 2020-07-01 08:02

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

查看更多
ら.Afraid
3楼-- · 2020-07-01 08:08

Property File

We store a property file under <project>/<package>/src/main/assets/config.properties

Loading properties

   private static final String PROPS_NAME = "config.properties";
   private static Properties configuration;

   ...
   public static void init(Context ctx) {

        configuration = new Properties();
        InputStream rawResource = resources.getAssets().open(PROPS_NAME);
        configuration.load(rawResource);
查看更多
SAY GOODBYE
4楼-- · 2020-07-01 08:09

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
查看更多
甜甜的少女心
5楼-- · 2020-07-01 08:09

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

查看更多
一夜七次
6楼-- · 2020-07-01 08:12

**Pretty simple solutions is here **

public class Constants {
    /**
     * Object key prams when pass the json object from server.
     */
    public static final String KEY_EMAIL = "email";
    public static final String KEY_PASSWORD = "password";
    public static final String KEY_DEVICE_TOKEN = "device_token";
    public static final String KEY_DEVICE_TYPE = "device_type";
    public static final String KEY_NAME = "name";
    public static final String KEY_COUNTRY_CODE = "country_code";
    public static final String KEY_PHONE_CODE = "phone-code";
    public static final String KEY_GENDER = "gender";
    public static final String KEY_DATE_OF_BIRTH = "date_of_birth";
    public static final String KEY_USER_ID = "user_id";
    public static final String KEY_LIMIT = "limit";
    public static final String KEY_DRIVER_ID = "driver_id";
    public static final String KEY_LONGTITUDE = "logitude";
    public static final String KEY_LATTITUDE = "lattitude";
    public static final String KEY_RATING = "rating";
    public static final String KEY_DETAILS = "details";
    public static final String KEY_ACCESS_TOKEN= "access_token";
    /**
     * Fragments name
     */
    public static final String FRAG_ETA = "ETA";
    public static final String FRAG_ACCOUNT_FRAGMENT = "ACCOUNT_FRAGMENT";
    public static final String FRAG_SETTING_FRAGMENT = "SETTING_FRAGMENT";
    public static final String FRAG_MAP_FRAGMENT = "MAP_FRAGMENT";
    public static final String FRAG_FEEDBACK = "FEEDBACK";
    public static final String FRAG_RATE_FRAGMENT = "RATE_FRAGMENT";

    public static final String USA_CODE = "+1";

    public static final String DISTANCE_SEARCH = "DISTANCE_SEARCH";


}

happy coding

查看更多
等我变得足够好
7楼-- · 2020-07-01 08:13

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.

查看更多
登录 后发表回答