Usama Sarwar
Usama Sarwar

Reputation: 9020

How to check programmatically whether app is running in debug mode or not?

I have to identify at some place in my app that, whether my app is running in debug mode or live mode. Is there any function or piece of code available to check that. that returns true/false in either case on/off. if so, please help me out. Thanks in advance.

Upvotes: 39

Views: 25178

Answers (3)

Ganesh Krishnan
Ganesh Krishnan

Reputation: 7395

if (BuildConfig.DEBUG) {
  // here be thine debug statement
}

Works really well across eclipse and Android Studio.

The other ones mentioned here often throws runtime exceptions for me

Upvotes: 15

android developer
android developer

Reputation: 116070

In case by "live mode" you mean signed for use on the play store, you can differentiate between the 2 states by checking the value of BuildConfig.DEBUG . Google has shown a video about it here

Upvotes: 13

Mark
Mark

Reputation: 7728

It is not clear from the question whether debug mode refers to:

  1. Whether the app is debuggable or not
  2. Whether the app is currently being debugged (e.g. over ADB)

The first is covered by CommonsWare's answer:

boolean isDebuggable = 0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE);

The second is:

boolean isBeingDebugged = android.os.Debug.isDebuggerConnected()

https://developer.android.com/reference/android/os/Debug.html#isDebuggerConnected()

Upvotes: 31

Related Questions