51

I've written a wrapper on top of Log.java that is provided by android. My Class will add some other application level features in Logs.

Now the things is that I want to check from the code whether "debuggable" is set to 'true' or 'false' in androidManifest.xml file.

Can I do that? If yes, how?

Swati Garg
  • 995
  • 1
  • 10
  • 21
ABDroids
  • 3,235
  • 3
  • 20
  • 9
  • Just bear in mind this wrapping Log : http://stackoverflow.com/questions/4199563/android-util-log-when-publishing-what-can-i-do-not-do – Blundell Aug 11 '11 at 10:45

2 Answers2

102

Use PackageManager to get an ApplicationInfo object on your application, and check the flags field for FLAG_DEBUGGABLE.

boolean isDebuggable = (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE));
Rolf Staflin
  • 2,092
  • 2
  • 22
  • 19
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    if you are confused about the snippets; it's because android stored application info boolean value with `bit flags` a technique that use an efficient way to store a number of Boolean values using as little memory as possible. – buncis Sep 07 '19 at 15:35
55

You can now use the static boolean field BuildConfig.DEBUG to achieve the same thing. This class is generated during compilation and can be seen in your gen folder.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • This is the correct way to do it I think post build tools 17. – jophde Aug 18 '13 at 19:08
  • 1
    This is the better solution. Unlike the accepted answer this gives you a final field. That way debug code that is dependent on that flag becomes unreachable and gets stripped out by the compiler. – Broatian Mar 31 '14 at 00:45
  • 9
    **WARNING**: This method does not work. `BuildConfig.DEBUG` reports false positives in release builds. I have switched to the `ApplicationInfo.FLAG_DEBUGGABLE` method for the correct value. https://code.google.com/p/android/issues/detail?id=27940 – David Manpearl May 15 '14 at 01:10
  • 1
    **FALSE WARNING:** @David Manpearl, No way this can lead to false positive. I always use this in release apps without errors. – Snicolas May 15 '14 at 05:10
  • Worth noting that library `BuildConfig.DEBUG` value and application value may vary! When calling from library it will not return whether the resulting app is debuggable. – Eugen Pechanec Dec 26 '14 at 18:13
  • 10
    BuildConfig is a generated class, so it works fine if you're developing an application. If you're developing a library and you are wanting to test whether or not the application that is including your library is debuggable, you can't use BuildConfig (because your library knows nothing about the app's BuildConfig class). Use ApplicationInfo.FLAG_DEBUGGABLE as well. – Vito Andolini Apr 28 '16 at 20:51