How can I detect in my code that I am in Release mode or Debug mode?
10 Answers
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.
-
39
BuildConfig
is located in your app's package, e.g.import com.mycompany.myapp.BuildConfig;
Jul 12, 2015 at 20:44 -
16due to a bug in AndroiStudio this does not work anymore, it is always false, even in DEBUG mode Jul 12, 2015 at 23:35
-
2I am using v1.2.2 and BuildConfig.DEBUG is always false, then I tried the suggestion below which works for me - I will try yours as well - many thanks! Jul 13, 2015 at 9:10
-
4As it turns out, this won't work when using a library (always returns true) : stackoverflow.com/q/20176284/878126 . Wonder what's the best alternative Jun 21, 2016 at 9:33
-
3
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
-
11This answer will work in all cases regardless of library project or application project. Oct 26, 2017 at 9:48
-
-
3ok it just doesn't work in static context, see stackoverflow.com/questions/10641144/…– A1mFeb 28, 2019 at 22:06
-
Without any imports: if (( getApplicationInfo().flags & android.content.pm.ApplicationInfo.FLAG_DEBUGGABLE ) != 0). Nice if you are writing a library and don't want to constantly be modifying the file to reference your application's package. Does require context though.– Al RoMay 12 at 2:08
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.
-
2"Unless you are importing the wrong BuildConfig class"... Yeah, very good point :D Jun 9, 2017 at 14:24
-
Thanks! This was the problem in my project, somehow it was picking up library project's BuildConfig (which is always release mode until Android Studio 3 comes out) Jun 9, 2017 at 20:36
-
-
What if, indeed, I'm in a different module that of the project? For example I make an SDK for other apps and I wish to check it out? Mar 7, 2021 at 15:01
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.
-
You shouldn't call Crashlytics and Answers separately. Just use: Fabric.with(this, new Crashlytics()); to include Crashlytics and Answers. Dec 13, 2016 at 14:10
-
1Thanks, @MikeBonnell, I made the code change to the example code Jul 22, 2017 at 12:56
-
I don't see how this is any different from using BuildConfig.DEBUG - if you only set BuildConfig.ENABLE_CRASHLYTICS for your debug builds then BuildConfig.DEBUG and BuildConfig.ENABLE_CRASHLYTICS will always have the same value right?– k2colSep 23, 2017 at 12:28
-
I think dev's working on library projects had problems detecting debug/release builds using BuildConfig.DEBUG. There might have been an early Android Studio bug involved too... Sep 26, 2017 at 13:54
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.
-
2This is the correct answer. Returns "release" whereas BuildConfig.DEBUG always returns
true
. Oct 23, 2017 at 21:50 -
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
}
-
1Please add description to your answer. It would be more helpful than just a piece of code. Dec 11, 2017 at 13:39
-
I was using
if (BuildConfig.DEBUG) {}
in a dependant gradle module which had (of course) NO REFERENCE to the app's build.gradle file - this caused debug mode to be recognised in a wrong way.if (BuildConfig.BUILD_TYPE.equals("Debug")){ }
FIXED the issue. Thanks Feb 7, 2019 at 12:22 -
-
-
1@Kamlesh at the point of writing this answer Flutter was nowhere near our minds. Please be aware of that Oct 9, 2020 at 13:50
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.
}
-
-
Not working for me. I am getting an error undefined BuildConfig class in flutter.– KamleshOct 9, 2020 at 13:28
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
Build.IS_DEBUGGABLE could be all right. It comes from "ro.debuggable"
-
What if the release version was made to be debuggable, for some reason? Mar 7, 2021 at 15:03
-
-
1
-
1@KhalidLakhani Not only that, but a debug build can be non-debuggable Jul 22 at 18:20
-
1@KhalidLakhani Indeed. Would be weird to have it for the debug build of not being debuggable, though :) Jul 23 at 13:53