0

What is the correct way to add debugging code to an Android app, such that the debugging code will only execute in test builds, and not in signed builds published on Google Play?

For example, in iOS apps I simply write:

#ifdef DEBUG
// put debug code here
#endif

Searching for solutions has led me to several different approaches, each one followed by a string of complaints about how it doesn't work as expected. But many of these solutions are quite old. Surely Google has fixed whatever problems existed here and there is a straightforward always-works solution for this now?

I'm building with Android Studio, but I'm hoping that the solution is not IDE-specific.

Thanks, Frank

Flarosa
  • 1,287
  • 1
  • 13
  • 27
  • Possible duplicate of [Detect if I am in release or debug mode in android](https://stackoverflow.com/questions/23844667/detect-if-i-am-in-release-or-debug-mode-in-android) – WoogieNoogie Sep 18 '17 at 20:53

1 Answers1

2

You should add the code that you want to execute only in debug mode inside the following:

if (BuildConfig.DEBUG) {
  // THIS WILL BE EXECUTED ONLY IN DEBUG MODE
}
Ugurcan Yildirim
  • 5,973
  • 3
  • 42
  • 73
  • Just to note, this solution is specific to the build tools. This comes for free when you use gradle with the android plugin to build your apps (default config in Android Studio). – Jon Sep 18 '17 at 21:20