I would recommend it differently. Instead of hard coding other variables and then causing a build error if they are set in release builds, set these variables in the build file and give them different values ββfor the debug and release build types. The object for this is the buildConfigField directive, and this mechanism sets variables that act like BuildConfig.DEBUG .
You can do something like this in your build.gradle module:
buildTypes { debug { buildConfigField "String", "MY_CONSTANT", '"debugValue"' } release { buildConfigField "String", "MY_CONSTANT", '"releaseValue"' } }
This will add BuildConfig.MY_CONSTANT , which will have a value of "debugValue" for debug builds and "releaseValue" for release.
If you want to have different behavior in debug and release assemblies, in your code you can use conventions on BuildConfig.DEBUG or on your constants:
if (BuildConfig.DEBUG) { // Do something here only for debug builds }
and etc.
Please note: if you really do not want to set your variables in the assembly file, you can use this conditional template to initialize global static variables directly in the code:
if (BuildConfig.DEBUG) { sGlobalVariable = "debug"; }
source share