如何检测我是否处于发布或调试模式? [英] How do I detect if I am in release or debug mode?

查看:93
本文介绍了如何检测我是否处于发布或调试模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的代码中检测到处于释放模式或调试模式?

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

推荐答案

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

The simplest, and best long-term solution, is to use BuildConfig.DEBUG. This is a boolean value that will be true for a debug build, false otherwise:

if (BuildConfig.DEBUG) {
  // do something for a debug build
}

有报道说,虽然我个人没有遇到问题,但是这个值在基于Eclipse的构建中并不是100%可靠的,因此我无法说出它到底有多少问题.

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.

如果您使用的是Android Studio,或者从命令行使用Gradle,则可以将自己的内容添加到BuildConfig,或者通过其他方式调整debugrelease生成类型,以帮助区分这些情况.运行时.

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:debuggable标志的值.如果那是您希望将调试"构建与发布"构建区分开的方式,那么按照定义,这是最好的解决方案.但是,请记住,debuggable标志实际上是Gradle/Android Studio认为是调试"构建的独立概念.任何构建类型都可以选择将debuggable标志设置为对该开发人员和该构建类型有意义的任何值.

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

这篇关于如何检测我是否处于发布或调试模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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