简体   繁体   English

禁用代码以在Android Gradle中进行发布构建

[英]Disabling code for release build in Android Gradle

I have const in code that contains some important information: 我的代码中包含一些重要信息的const:

const val IMPORTANT_CONST = "KEY INFORMATION"

I'm using this const only for debug mode and only temporary (after some time I'll remove it). 我仅在调试模式下使用此const,并且仅在临时模式下使用(一段时间后,我将其删除)。 For now, I'm using build config flag to know if I need to use this const. 现在,我正在使用build config标志来确定是否需要使用此const。

How can I hide this const or replace value to an empty string for my release build variant? 如何为我的发布版本变体隐藏此const或将值替换为空字符串?

You could add it as a BuildConfig field: 您可以将其添加为BuildConfig字段:

In your app gradle script: 在您的应用gradle脚本中:

buildTypes {
    release {
        buildConfigField("String", "IMPORTANT_CONST ", "\"\"")
    }
    debug {
        buildConfigField("String", "IMPORTANT_CONST ", "\"my secret stuff\"")
    }
}

And in your code: 在您的代码中:

Log.i("MyApp", "IMPORTANT_CONST = " + BuildConfig.IMPORTANT_CONST );

You can configure the constant in gradle like this: 您可以像这样在gradle中配置常量:

        android {
    buildTypes {
        debug {
            buildConfigField "String", "IMPORTANT_CONST", "\"KEY INFORMATION\""
        }

        release {
            buildConfigField "String", "IMPORTANT_CONST", "\"\""
        }
    }
}

You can access this variable using BuildConfig.IMPORTANT_CONST 您可以使用BuildConfig.IMPORTANT_CONST访问此变量。

Use BuildConfig which will provide build variant based on your gradle properties. 使用BuildConfig ,它将根据gradle属性提供构建变体。

if (BuildConfig.DEBUG) {
       //Set your constant 
    } else {
         //replace constant value   
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM