如何仅在应用程序处于开发模式时打印日志?

Pri*_*ish -3 android android-logcat android-log

我只需要在开发应用程序时在我的应用程序中打印日志。那么有没有一种像布尔值这样的方法来决定我的应用程序是否处于某种模式,然后只允许打印日志?这将节省我每次准备签名构建时删除日志的时间。

我尝试了以下解决方案:

boolean isDebuggable = 0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE);
Run Code Online (Sandbox Code Playgroud)

和:

if (BuildConfig.DEBUG) {
  // do something for a debug build
}`
Run Code Online (Sandbox Code Playgroud)

它们不像我想要的那样工作。

谢谢你。

She*_*har 5

如果您使用的是 Android Studio,请尝试利用 gradle 文件。

  defaultConfig {
        applicationId "com.your.application.id"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        //Defining Log debugging
        buildConfigField "boolean", "LOG_DEBUG_MODE", "false"
        buildConfigField "boolean", "LOG_DEBUG_WITH_STACKTRACE_MODE", "false"
    }


    buildTypes {
        debug {
            testCoverageEnabled = "true"
            buildConfigField "boolean", "LOG_DEBUG_MODE", "true"
            buildConfigField "boolean", "LOG_DEBUG_WITH_STACKTRACE_MODE", "true"
        }

        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
Run Code Online (Sandbox Code Playgroud)