如何檢測我是處於發佈模式還是調試模式?

本文翻譯自:How do I detect if I am in release or debug mode?

如何在代碼中檢測到我處於發佈模式或調試模式?


#1樓

參考:https://stackoom.com/question/1c35P/如何檢測我是處於發佈模式還是調試模式


#2樓

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帖子


#3樓

The simplest, and best long-term solution, is to use BuildConfig.DEBUG . 最簡單,最好的長期解決方案是使用BuildConfig.DEBUG This is a boolean value that will be true for a debug build, false otherwise: 這是一個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 BuildConfig or otherwise tweak the debug and release build 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:debuggable flag in the manifest. Illegal Argument的解決方案基於清單中android:debuggable標誌的值。 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. 但是,請記住,未來, debuggable標誌實際上是Gradle / Android Studio認爲“調試”版本的獨立概念。 Any build type can elect to set the debuggable flag to whatever value that makes sense for that developer and for that build type. 任何構建類型都可以選擇將debuggable標誌設置爲對該開發人員和該構建類型有意義的任何值。


#4樓

Due to the mixed comments about BuildConfig.DEBUG , I used the following to disable crashlytics (and analytics) in debug mode : 由於有關BuildConfig.DEBUG的混合評論,我使用以下內容在調試模式下禁用崩解(和分析):

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_CRASHLYTICS flag 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_CRASHLYTICS to anything you want. 在您的應用中使用相同的概念,並將ENABLE_CRASHLYTICS重命名爲您想要的任何內容。 I like this approach because I can see the flag in the configuration and I can control the flag. 我喜歡這種方法,因爲我可以看到配置中的標誌,我可以控制標誌。


#5樓

Yes, you will have no problems using: 是的,您使用時沒有問題:

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

Unless you are importing the wrong BuildConfig class. 除非您導入錯誤的BuildConfig類。 Make sure you are referencing your project's BuildConfig class, not from any of your dependency libraries. 確保引用項目的BuildConfig類,而不是任何依賴庫。

在此輸入圖像描述


#6樓

Alternatively, you could differentiate using BuildConfig.BUILD_TYPE; 或者,您可以使用BuildConfig.BUILD_TYPE區分;

If you're running debug build BuildConfig.BUILD_TYPE.equals("debug"); 如果你正在運行debug build BuildConfig.BUILD_TYPE.equals("debug"); returns true. 返回true。 And for release build BuildConfig.BUILD_TYPE.equals("release"); 對於發佈版BuildConfig.BUILD_TYPE.equals("release"); returns true. 返回true。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章