Changing Constant Values ​​When Creating an Issue

I am developing in eclipse using ADT for android.
In my application, I have some constants that help me easily debug my application.
As an example, I have:
public static final boolean DEBUG_TOAST_LOGS = true;
which help me fry some magazines on the screen.
Every time I am going to create a release, I have to go through the constants and set their values ​​in accordance with what is suitable for the release, which is somehow painful.
Now I want to create an application using two configurations: one for debug mode and one for release mode. Release mode should set my constants to the appropriate values. How can i do this? What is your suggestion? What is the best way to fulfill my needs?

Any help would be appreciated.

+5
source share
2 answers

I'm not sure if you are using Gradle as your build system. If you do this, you can install specific resources such as assemblies, for example. boolean debug will be true for the debug build type and false for the release build type.

build.gradle

 android { defaultConfig { ... resValue "bool", "debug", "true" } buildTypes { release { ... resValue "bool", "debug", "false" } } ... } 

Application.java

 public class Application extends android.app.Application { @Override public void onCreate() { super.onCreate(); if (getResources().getBoolean(R.bool.debug)) { ... // debug logic here } ... } } 
+9
source

@hidro is great, but requires an unnecessary call to getResources()... every time you want to access a value.

There is another possibility:

build.gradle

 android { buildTypes { debug { buildConfigField "boolean", "DEBUG_TOAST_LOGS", "true" } release { buildConfigField "boolean", "DEBUG_TOAST_LOGS", "false" } } 

}

Then in your code you can write:

 if (BuildConfig.DEBUG_TOAST_LOGS) { // ... enjoy your toasts ... } 
+4
source

Source: https://habr.com/ru/post/1214373/


All Articles