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 ?
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).
Property File
We store a property file under
<project>/<package>/src/main/assets/config.properties
Loading properties
Use
public static final values.
and keep them in separate java file as follows: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
Ex. res/values/strings.xml
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).
**Pretty simple solutions is here **
happy coding
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 theinterface
can also be declared asabstract
.