Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: run application in dev/prod environment?

Tags:

android

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?

like image 785
Tool Avatar asked Dec 26 '22 11:12

Tool


2 Answers

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
}
like image 58
Raghav Sood Avatar answered Jan 08 '23 10:01

Raghav Sood


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
}
like image 28
Frank Avatar answered Jan 08 '23 09:01

Frank