Pro Tip: Android BuildConfig

The Android build system and platform have a lot of great ways to make it easier to set different variables for different build circumstances. The system even has a built-in one for detecting whether an APK was made using a release or debug configuration:

if (BuildConfig.DEBUG) {
Log.d("Ahoy!", "I am a debug build");
} else {
Log.d("Ahoy!", "We be release!");
}

That’s pretty useful and there are a lot of cases where you might want to know what configuration you’re running in.

What if you want to set some custom values for build and release. Sure, you could just have the above ‘if’ statement throughout your code but there’s also another option, your build.gradle:

buildTypes {
debug {
buildConfigField "boolean", "SOME_VAR", "true"
}
release {
buildConfigField "boolean", "SOME_VAR", "false"
}
}

After re-running the build process, you’ll be able to call on that “SOME_VAR” using that BuildConfig class:

BuildConfig.SOME_VAR;

Of course, these values needn’t be booleans, they can be numbers or strings or basically whatever you need. I hope that helps and if you need some Android or other mobile development work done, please reach out to us via the contact form on this page!