Android 如何检测我是处于发布模式还是调试模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23844667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:31:30  来源:igfitidea点击:

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

androidandroid-build-type

提问by David

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

如何在我的代码中检测我处于发布模式还是调试模式?

回答by CommonsWare

The simplest, and best long-term solution, is to use BuildConfig.DEBUG. This is a booleanvalue that will be truefor a debug build, falseotherwise:

最简单、最好的长期解决方案是使用BuildConfig.DEBUG. 这是一个boolean,这将是价值true的调试版本,false否则:

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.

有报道称,这个值在基于 Eclipse 的构建中不是 100% 可靠的,尽管我个人没有遇到过问题,所以我不能说它到底有多大的问题。

If you are using Android Studio, or if you are using Gradle from the command line, you can add your own stuff to BuildConfigor otherwise tweak the debugand releasebuild types to help distinguish these situations at runtime.

如果您正在使用 Android Studio,或者如果您正在从命令行使用 Gradle,您可以添加自己的内容BuildConfig或以其他方式调整debugrelease构建类型,以帮助在运行时区分这些情况。

The solution from Illegal Argument is based on the value of the android:debuggableflag 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 debuggableflag is really an independent concept from what Gradle/Android Studio consider a "debug" build to be. Any build type can elect to set the debuggableflag to whatever value that makes sense for that developer and for that build type.

Illegal Argument 的解决方案基于android:debuggable清单中标志的值。如果这是您希望区分“调试”版本和“发布”版本的方式,那么根据定义,这是最好的解决方案。但是,请记住,该debuggable标志实际上是一个独立于 Gradle/Android Studio 认为的“调试”构建的概念。任何构建类型都可以选择将debuggable标志设置为对该开发人员和该构建类型有意义的任何值。

回答by Illegal Argument

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

它取自这里的bundells post

回答by 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.

除非您导入了错误的 BuildConfig 类。确保您引用的是项目的 BuildConfig 类,而不是来自任何依赖库。

enter image description here

在此处输入图片说明

回答by Someone Somewhere

Due to the mixed comments about BuildConfig.DEBUG, I used the following to disable crashlytics (and analytics) in debug mode :

由于关于 的混合评论BuildConfig.DEBUG,我使用以下内容在调试模式下禁用 crashlytics(和分析):

update /app/build.gradle

更新/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_CRASHLYTICSflag as follows:

然后,在您的代码中,您检测ENABLE_CRASHLYTICS标志如下:

    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_CRASHLYTICSto anything you want. I like this approach because I can see the flag in the configuration and I can control the flag.

在您的应用程序中使用相同的概念并重命名ENABLE_CRASHLYTICS为您想要的任何名称。我喜欢这种方法,因为我可以在配置中看到标志并且我可以控制标志。

回答by Prudhvi

Alternatively, you could differentiate using BuildConfig.BUILD_TYPE;

或者,您可以使用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.

如果您正在运行调试版本,则 BuildConfig.BUILD_TYPE.equals("debug");返回 true。对于发布构建BuildConfig.BUILD_TYPE.equals("release");返回true。

回答by 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
}

回答by Salim Lachdhaf

Make sure that you are importing the correct BuildConfig class And yes, you will have no problems using:

确保您正在导入正确的 BuildConfig 类,是的,使用以下方法不会有任何问题:

if (BuildConfig.DEBUG) {
   //It's not a release version.
}