I want to make development easier by implementing a configuration parameter that determines whether the app should be run in 'DEV' mode or 'PROD' mode.
I want this parameter to be accessible from any file (based on this parameter different chunks of code will be executed).
What's the most practical way to store this parameter (which isn't accessible or changeable by the user)?
How can I access it from within the application?
Starting with ADT 17 (IIRC), you have this automatically as part of the auto generated BuildConfig
class.
The DEBUG
field is always true
when developing, but when you export a signed or unsigned apk, it is set to false
. You can use it as:
if(BuildConfig.DEBUG) {
//Debug mode
}
Or the other way around:
if(!BuildConfig.DEBUG) {
//Release mode
}
You can use an enum:
public enum BuildType {
Release, Pilot, Debug;
}
And assign it to a global variable:
public static final BuildType BUILD_TYPE = BuildType.Debug;
You can even create some methods in the enum that allow you switch over very specific parts of your application.
Now you can do stuff like this:
if (MyApplication.BUILD_TYPE != BuildType.Release) {
// some code that does not go in the release
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With