0

so.. I'm using Log.d("", ""); in many places in my app for debuging. but I don't want those logs in the store.

right now in order to hide them in the store version, I created a Constant.class and put a boolean there called debugMode, and wrapped every log i have in an if statement like this :

if (Constant.debugMode) {
Log.d(TAG, "check123");
}

and then when I build a google store apk I change that boolean to true instead of false.

that's kind of clumsy in my opinion, is there any more efficient way to do that?

JozeRi
  • 3,219
  • 6
  • 27
  • 45

2 Answers2

1

Make a simple logger class that has public static methods and a swich to enable logs only for debug versions of your app. Here is a sample code.

public class Logger {

    public static final boolean D = BuildConfig.DEBUG;

    public static void LogInfo(String TAG, String msg) {
        if (D) {
            Log.i(TAG, msg);
        }
    }

    public static void LogWarrning(String TAG, String msg) {
        if (D) {
            Log.w(TAG, msg);
        }
    }

    public static void LogDebug(String TAG, String msg) {
        if (D) {
            Log.d(TAG, msg);
        }
    }

    public static void LogError(String TAG, String msg) {
        if (D) {
            Log.e(TAG, msg);
        }
    }
}

Usage

Logger.LogDebug("Test", "Example");

This way you can keep the if clauses in one place and don't have to worry about them. Also I don't think it clumsy this way.

Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
0

I find a far easier solution is to forget all the if checks all over the place and just use ProGuard to strip out any Log.d() or Log.v() method calls when we call our Ant release target.

That way, we always have the debug info being output for regular builds and don't have to make any code changes for release builds. ProGuard can also do multiple passes over the bytecode to remove other undesired statements, empty blocks and can automatically inline short methods where appropriate.

For example, here's a very basic ProGuard config for Android:

-dontskipnonpubliclibraryclasses -dontobfuscate -forceprocessing -optimizationpasses 5

-keep class * extends android.app.Activity -assumenosideeffects class android.util.Log { public static * d(...); public static * v(...); } So you would save that to a file, then call ProGuard from Ant, passing in your just-compiled JAR and the Android platform JAR you're using.

@REF:Remove all debug logging calls before publishing: are there tools to do this?

Community
  • 1
  • 1
theapache64
  • 10,926
  • 9
  • 65
  • 108