Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way to Include Debug Code?

I am programming Android applications, and the best way here may or may not be the same as Java in general.

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.

Is there an accepted or best way to accomplish this in 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.

like image 562
stormin986 Avatar asked Apr 27 '10 03:04

stormin986


1 Answers

Instead of using your own flag, you can use the flag set automatically by ADT, like this:

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.

Thus, you don't have to remember setting / resetting your own custom flag.

You can read more in this thread.

like image 179
HRJ Avatar answered Sep 28 '22 01:09

HRJ