摇篮检查恒定值如果为true,显示错误 [英] Gradle check constant value to show error if true

查看:156
本文介绍了摇篮检查恒定值如果为true,显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以设置摇篮检查像DEBUG常数变量的项目,并显示一个错误,如果它是试图做一个发布版本?

There's any way to setup gradle to check a constant variable like DEBUG in the project and show an error if it is trying to do a release version?

感谢

推荐答案

我会建议将它用不同的方式。相反,硬编码的其他变量,然后有它抛出一个生成错误,如果他们在释放正在设置的建立,设置生成文件的变量,并同它们进行调试不同的价值观和发布版本的类型。该工厂因为这是 buildConfigField 指令,并且该机制规定的行为像 BuildConfig.DEBUG 变量。

I would recommend going about it a different way. Instead of hardcoding other variables and then having it throw a build error if they're set in release builds, set those variables in the build file, and give them different values for the debug and release build types. The facility for this is the buildConfigField directive, and that mechanism sets variables that act like BuildConfig.DEBUG.

您可以做这样的事情在模块的 build.gradle

You can do something like this in your module's build.gradle:

buildTypes {
    debug {            
        buildConfigField "String", "MY_CONSTANT", '"debugValue"'
    }
    release {            
        buildConfigField "String", "MY_CONSTANT", '"releaseValue"'
    }
}

这将增加一个 BuildConfig.MY_CONSTANT ,将有建立的价值debugValue用于调试和releaseValue发布。

This will add a BuildConfig.MY_CONSTANT that will have the value "debugValue" for debug builds and "releaseValue" for release.

如果你想在调试不同的行为和发布版本,在code您可以在 BuildConfig.DEBUG 或你的常量使用条件:

If you want to have different behavior in debug and release builds, in your code you can use conditionals on BuildConfig.DEBUG or on your constants:

if (BuildConfig.DEBUG) {
    // Do something here only for debug builds
}

等。

请注意,如果你真的不想在构建文件中设置的变量,你可以使用有条件模式就在code初始化全局静态变量:

Note that if you really don't want to set your variables in the build file, you could use that conditional pattern to initialize global static variables right in the code:

if (BuildConfig.DEBUG) {
    sGlobalVariable = "debug";
}

这篇关于摇篮检查恒定值如果为true,显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆