Tag Archives: Android Studio

Disallow debug logging in Android release build

Android_Robot_200So I’ve been doing a lot of Android development over the past several months, and came across the question of how to provide debug logging for a debug build of the library/app I’m working on, but to not let those debug logs flow through to the release build.

At first I mistakenly assumed that, hey, it’s a release build, so surely Log.d and Log.v calls won’t show up?  Wrong.  Android release builds don’t care about debug level, they’ll show whatever logging, at whatever logging level you tell it to.

So at first I sat and thought how I could fix the problem.  I came up with a few options:

  1. I could do what the Android Developer guide suggests one does, and comment out all my debug log lines before building a release build.  Ya, right, uh, no.
  2. I could wrap all the debug log lines with if(BuildConfig.DEBUG) { … }, but that felt unnecessarily painful to have to do.
  3. I could create my own Log class, with its own logic on when to actually log (such as a point 2 above, at the very least), and then just call the android.util.Log methods, but that kind of felt like overkill, as surely the expectation to have intelligent logging on Android is not that wrap the existing logging with your own…
  4. I could switch to old familiar Log4J (or Android equivalent); but yuck, that’s _really_ overkill just to get some logging on for an Android library.
  5. I could write some preprocessor and inject it into the gradle build process, that would go and strip out all the debug log calls to Log.v and Log.d, but, well, that felt like something somebody else would have done already (The world being full of lots of clever people and all…)

I then took to Google, and of course came upon several StackOverflow (Good ol’ SO) questions on the problem, and some good solutions.  It took a bit of reading, and finding more questions and answers before I found the exact solution that worked for me.  With one slight small caveat, which I’ll mention later.  Read on to hear about what I ended up using as a solution.

Continue reading