Java 包含调试代码的最佳方式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2718565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best Way to Include Debug Code?
提问by stormin986
I am programming Android applications, and the best way here may or may not be the same as Java in general.
我正在编写 Android 应用程序,这里最好的方法可能与 Java 相同,也可能不同。
I simply want to be able to set a debug flag that will only execute certain portions of code when it's set to true––equiv to C++ setting a preprocessor #define DEBUG and using #ifdef DEBUG.
我只是希望能够设置一个调试标志,当它设置为 true 时,它只会执行代码的某些部分——相当于 C++ 设置预处理器 #define DEBUG 并使用 #ifdef DEBUG。
Is there an accepted or best way to accomplish this in Java?
在 Java 中是否有公认的或最好的方法来实现这一点?
Right now I'm just going to set a variable in my Application object, but I don't imagine this is the best way.
现在我只想在我的 Application 对象中设置一个变量,但我认为这不是最好的方法。
采纳答案by yanchenko
That's the way I do it:
这就是我这样做的方式:
// in some.class.with.Constants
public static final boolean DEV_MODE = true;
// in some other class
import static some.class.with.Constants.DEV_MODE;
if(DEV_MODE){
Log.d('sometag', 'somemessage');
}
回答by drawnonward
if ( Debug.isDebuggerConnected() ) {
// debug stuff
}
回答by HRJ
Instead of using your own flag, you can use the flag set automatically by ADT, like this:
您可以使用 ADT 自动设置的标志,而不是使用您自己的标志,如下所示:
final static int appFlags = context.getApplicationInfo().flags;
final static boolean isDebug = (appFlags & ApplicationInfo.FLAG_DEBUGGABLE) != 0
The FLAG_DEBUGGABLE
bit is automatically set to true or false, depending on the "debuggable" attribute of the application (set in AndroidManifest.xml). The latest version of ADT (version 8) automatically sets this attribute for you when not exporting a signed package.
该FLAG_DEBUGGABLE
位会自动设置为 true 或 false,具体取决于应用程序的“debuggable”属性(在 AndroidManifest.xml 中设置)。不导出签名包时,最新版本的 ADT(版本 8)会自动为您设置此属性。
Thus, you don't have to remember setting / resetting your own custom flag.
因此,您不必记住设置/重置您自己的自定义标志。
You can read more in this thread.
您可以在此线程中阅读更多内容。
回答by fiction
I think that writing tests is better alternative than adding DEBUG code.
我认为编写测试比添加调试代码更好。
My point is that when you write test for some component/method/class you don't pollute your original source code with some redundant debug code.
我的观点是,当你为某些组件/方法/类编写测试时,你不会用一些冗余的调试代码污染你的原始源代码。
回答by IlDan
Revision 17 of SDK tools (March 2012) introduced a way to imitateC's #ifdef DEBUG
SDK 工具的修订版 17(2012 年 3 月)引入了一种模仿C 的方法#ifdef DEBUG
From the General Notes:
从一般注释:
Added a feature that allows you to run some code only in debug mode. Builds now generate a class called BuildConfig containing a DEBUG constant that is automatically set according to your build type. You can check the (BuildConfig.DEBUG) constant in your code to run debug-only functions.
添加了一项功能,允许您仅在调试模式下运行某些代码。构建现在生成一个名为 BuildConfig 的类,其中包含一个根据您的构建类型自动设置的 DEBUG 常量。您可以检查代码中的 (BuildConfig.DEBUG) 常量以运行仅调试功能。
回答by The Original Android
This works for me with code if (BuildConfig.DEBUG)
, using the BuildConfigclass. This is a safe and easy code to do. Be careful when using this style of code. Don't use it such that there are 2 different distinct branches of code, between Release and Debugversions. If you do, it might invalidate the app testing for the Release version. For me, I have used it only to skip calling Log
messaging.
这对我来说适用于代码if (BuildConfig.DEBUG)
,使用BuildConfig类。这是一个安全且简单的代码。使用这种风格的代码时要小心。不要使用它,以便在Release 和 Debug版本之间有 2 个不同的不同代码分支。如果这样做,它可能会使发布版本的应用程序测试无效。对我来说,我使用它只是为了跳过呼叫Log
消息。
More details on this class BuildConfig @ Build System Concepts.
有关此类 BuildConfig @Build System Concepts 的更多详细信息。
回答by Sunny Tambi
I suggest to use inbuilt android API BuildConfig
我建议使用内置的 android APIBuildConfig
if (BuildConfig.DEBUG) {
// do something for a debug build
}