ChatGPT解决这个技术问题 Extra ChatGPT

How do I detect if I am in release or debug mode?

How can I detect in my code that I am in Release mode or Debug mode?


C
CommonsWare

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.


BuildConfig is located in your app's package, e.g. import com.mycompany.myapp.BuildConfig;
due to a bug in AndroiStudio this does not work anymore, it is always false, even in DEBUG mode
I 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!
As 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
This answer will not work for library project (.aar)
a
amorenew

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


This answer will work in all cases regardless of library project or application project.
What needs to be imported for getApplicationInfo().flags to work?
ok it just doesn't work in static context, see stackoverflow.com/questions/10641144/…
V
Vansuita Jr.

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.

https://i.stack.imgur.com/5dPJd.png


"Unless you are importing the wrong BuildConfig class"... Yeah, very good point :D
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)
I am getting an error undefined BuildConfig class in flutter.
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?
S
Someone Somewhere

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.
Thanks, @MikeBonnell, I made the code change to the example code
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?
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...
P
Prudhvi

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.


This is the correct answer. Returns "release" whereas BuildConfig.DEBUG always returns true.
I am getting an error undefined BuildConfig class in flutter.
G
Giedrius Šlikas

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
}

Please add description to your answer. It would be more helpful than just a piece of code.
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
this is real answer, just change "Debug" to "debug"
I am getting an error undefined BuildConfig class in flutter.
@Kamlesh at the point of writing this answer Flutter was nowhere near our minds. Please be aware of that
S
Salim Lachdhaf

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.
}

Correct import is <package_name>.BuildConfig.
Not working for me. I am getting an error undefined BuildConfig class in flutter.
J
Jayesh Dankhara
if (BuildConfig.DEBUG) { 



}

This Is Working For Me


d
duskrivermoon

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?
@androiddeveloper can release version be made debuggable?
@KhalidLakhani Pretty sure it can.