Reputation: 7359
How can I detect in my code that I am in Release mode or Debug mode?
Upvotes: 510
Views: 216579
Reputation: 531
applicationInfo.flags
is not available at compile time and is resolved in runtime, which will cause no optimizations to be applied to the code (for example, your logs will not be removed from the APK). BuildConfig
can affect the build speed and is Java dependent, so it won't work in KMP. My solution allows to make a lightweight compile time check: just add debug and release folders to the src directory of your gradle module and add a file with constants there - the build system will automatically select the appropriate option at compile time. You will be able to use this constant anywhere in the project.
Upvotes: 0
Reputation: 116362
You need to use this in the gradle/kts file , so that the BuildConfig file will be generated for you :
buildFeatures {
buildConfig = true
}
Then, reach the field BuildConfig.DEBUG
that belongs to the package name of your app.
Something similar to this which is less recommended (as it won't remove code when creating the release version) is to check if the app is debuggable:
val isDebuggable = context.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0
Upvotes: 21
Reputation: 1
Build.IS_DEBUGGABLE could be all right. It comes from "ro.debuggable"
Upvotes: 0
Reputation: 1201
I am using this solution in case to find out that my app is running on debug version.
if (BuildConfig.BUILD_TYPE.equals("debug")){
//Do something
}
Upvotes: 16
Reputation: 1082
Make sure that you are importing the correct BuildConfig class And yes, you will have no problems using:
if (BuildConfig.DEBUG) {
//It's not a release version.
}
Upvotes: 4
Reputation: 10338
Try the following:
boolean isDebuggable = ( 0 != ( getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE ) );
Kotlin:
val isDebuggable = 0 != applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE
It is taken from bundells post from here
Upvotes: 102
Reputation: 23787
Due to the mixed comments about BuildConfig.DEBUG
, I used the following to disable crashlytics (and analytics) in debug mode :
update /app/build.gradle
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "your.awesome.app"
minSdkVersion 16
targetSdkVersion 25
versionCode 100
versionName "1.0.0"
buildConfigField 'boolean', 'ENABLE_CRASHLYTICS', 'true'
}
buildTypes {
debug {
debuggable true
minifyEnabled false
buildConfigField 'boolean', 'ENABLE_CRASHLYTICS', 'false'
}
release {
debuggable false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
then, in your code you detect the ENABLE_CRASHLYTICS
flag as follows:
if (BuildConfig.ENABLE_CRASHLYTICS)
{
// enable crashlytics and answers (Crashlytics by default includes Answers)
Fabric.with(this, new Crashlytics());
}
use the same concept in your app and rename ENABLE_CRASHLYTICS
to anything you want. I like this approach because I can see the flag in the configuration and I can control the flag.
Upvotes: 50
Reputation: 2343
Alternatively, you could differentiate using BuildConfig.BUILD_TYPE;
If you're running debug build
BuildConfig.BUILD_TYPE.equals("debug");
returns true. And for release build BuildConfig.BUILD_TYPE.equals("release");
returns true.
Upvotes: 26
Reputation: 2099
Yes, you will have no problems using:
if (BuildConfig.DEBUG) {
//It's not a release version.
}
Unless you are importing the wrong BuildConfig class. Make sure you are referencing your project's BuildConfig class, not from any of your dependency libraries.
Upvotes: 79
Reputation: 1006674
The simplest, and best long-term solution, is to use BuildConfig.DEBUG
. This is a boolean
value that will be true
for a debug build, false
otherwise:
if (BuildConfig.DEBUG) {
// do something for a debug build
}
There have been reports that this value is not 100% reliable from Eclipse-based builds, though I personally have not encountered a problem, so I cannot say how much of an issue it really is.
If you are using Android Studio, or if you are using Gradle from the command line, you can add your own stuff to BuildConfig
or otherwise tweak the debug
and release
build types to help distinguish these situations at runtime.
The solution from Illegal Argument is based on the value of the android:debuggable
flag in the manifest. If that is how you wish to distinguish a "debug" build from a "release" build, then by definition, that's the best solution. However, bear in mind that going forward, the debuggable
flag is really an independent concept from what Gradle/Android Studio consider a "debug" build to be. Any build type can elect to set the debuggable
flag to whatever value that makes sense for that developer and for that build type.
Upvotes: 958