Disable LogCat Output COMPLETELY in release Android app?

AndroidReleaseGoogle PlayLogcatAndroid Logcat

Android Problem Overview


Shutting off my own app's LogCat output before releasing an app to the market is straightforward. I also know how to selectively filter LogCat message by tag and/or id for my own debug convenience.

But now I am interested in something that may be much more difficult (perhaps impossible?): Disable all LogCat output, including & especially those coming from 3rd-party services like TtsService, GoogleLoginService, etc.

Is this possible?

To further clarify: I am not interested in filtering out messages for myself. I am rather interested in disabling 3rd-party messages for whoever downloads my app from the Android Market. Is this possible?

Android Solutions


Solution 1 - Android

You can use ProGuard to remove completely any lines where a return value is not used, by telling ProGuard to assume that there will be no problems.

The following proguard.cfg chunk instructs to remove Log.d, Log.v and Log.i calls.

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** w(...);
    public static *** v(...);
    public static *** i(...);
}

The end result is that these log lines are not in your release apk, and therefore any user with logcat won't see d/v/i logs.

Solution 2 - Android

if you don't use proguard, you have to manage the log yourself and in the manifest file make dubuggable false

<application
    android:name="MyApplication"
    android:icon="@drawable/gift"
    android:label="@string/app_name" android:debuggable="@bool/build_log">

Here my custom log class

public class Lol {

	public static final boolean ENABLE_LOG = true & MyApplication.sDebug;
	
	private static final boolean DEBUG = true & ENABLE_LOG;

	private static final boolean VERBOSE = true & ENABLE_LOG;

	private static final boolean TEMP = true & ENABLE_LOG;

	private static final boolean WARNING = true & ENABLE_LOG;

	private static final boolean INFO = true & ENABLE_LOG;

	private static final boolean ERROR = true & ENABLE_LOG;

	public static void obvious(String tag, String msg) {
		if (DEBUG) {
			msg = "*********************************\n" + msg
					+ "\n*********************************";
			Log.d(tag, msg);
		}
	}

	public static void d(String tag, String msg) {
		if (DEBUG)
			Log.d(tag, msg);
	}

	public static void d(boolean bool, String tag, String msg) {
		if (TEMP&bool)
			Log.d(tag, msg);
	}

	public static void i(String tag, String msg) {
		if (INFO)
			Log.i(tag, msg);
	}

	public static void e(String tag, String msg) {
		if (ERROR)
			Log.e(tag, msg);
	}

	public static void e(boolean bool, String tag, String msg) {
		if (TEMP&bool)
			Log.e(tag, msg);
	}

	public static void v(String tag, String msg) {
		if (VERBOSE)
			Log.v(tag, msg);
	}

	public static void w(String tag, String msg) {
		if (WARNING)
			Log.w(tag, msg);
	}

	public static String getStackTraceString(Exception e) {
		return Log.getStackTraceString(e);
	}

	public static void w(String tag, String msg, Exception e) {
		if (WARNING)
			Log.w(tag, msg,e);
	}
}

Solution 3 - Android

The great answer provided by David Caunt doesn't seem to work for the rules defined in proguard-android-optimize.txt.

Instead of using the wildcard ***, current versions of ProGuard seem to expect the return parameter's type qualifier:

-assumenosideeffects class android.util.Log {
    public static int   d(...);
    public static int   w(...);
    public static int   v(...);
    public static int   i(...);
    public static int wtf(...);
}

Solution 4 - Android

In app build.gradle file set:

release {
    minifyEnabled true
     ……
}

In proguard-rules.pro put:

-assumenosideeffects class android.util.Log {
  public static *** v(...);
  public static *** d(...);
  public static *** i(...);
  public static *** w(...);
  public static *** e(...);
}
-ignorewarnings

It worked for me.

Solution 5 - Android

You can put debuggable false on buildTypes release.

buildTypes {

     release {
        debuggable false
        ...
     }

}

Solution 6 - Android

I usually do next:

if (BuildConfig.DEBUG) Log.i(TAG, msg);

though if you have a lot of dependencies (libraries) and they written bad then yeah just use https://stackoverflow.com/a/5553290/4548520

making lines shorter:

private final static boolean DEBUG = BuildConfig.DEBUG;

if (DEBUG) Log.i(TAG, msg_1);

if (DEBUG) Log.e(TAG, msg_error_2);

Solution 7 - Android

I combined David Snabel-Caunt's accepted answer in addition to swapping out the default ProGuard settings ("proguard-android.txt" file) from the Android SDK for the "proguard-android-optimize.txt" optimized file. The file is also available in this Android SDK folder with the same rules but with optimizations enabled.

Solution 8 - Android

Instead of enumerating all the class's methods it looks like you can also simply use the <methods> shorthand :

-assumenosideeffects class a.b.c.DebugLogs {
    <methods>;
}

Worked for me with com.android.tools.build:gradle version 4.0.0.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndroid EveView Question on Stackoverflow
Solution 1 - AndroidDavid Snabel-CauntView Answer on Stackoverflow
Solution 2 - AndroidWin Myo HtetView Answer on Stackoverflow
Solution 3 - AndroidMapsyView Answer on Stackoverflow
Solution 4 - AndroidsnehalView Answer on Stackoverflow
Solution 5 - AndroidheronsanchesView Answer on Stackoverflow
Solution 6 - Androiduser25View Answer on Stackoverflow
Solution 7 - AndroidAlyosha_KaramazovView Answer on Stackoverflow
Solution 8 - AndroidMickäel A.View Answer on Stackoverflow