Disable Logcat Output Completely in Release Android App

Disable LogCat Output COMPLETELY in release Android app?

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.

Disable LogCat output in release apk?

Please check this thread : How do I enable/disable log levels in Android?

how to turn off log cat output after making release apk when minifyenabled is true in android

-assumenosideeffects is skipped when -dontoptimize is set (relevant documentation), which I believe is the default for the standard ProGuard configuration file provided by Android.

If you'd like to use -assumenosideeffects then you will have to use the proguard-android-optimize.txt configuration file from the {your-sdk-dir}/tools/proguard/ directory. This will be also applied to any libraries which you use in your application project, so their logging will be stripped as well if you're worried about that.

As an alternative you could use a wrapper class for Android's Log, wrapping all calls in conditions checking the BuildConfig.DEBUG flag, which is nicer since you will have a single access point for these checks as opposed to littering them all over your classes.

It is however worth noting that for the latter solution you will not get a performance improvement for release builds, as if you have lots of logging where you concatenate strings for your messages these will still be evaluated at runtime for the method calls (even if they will never be printed out).

How to remove all debug logging calls before building the release version of an Android app?

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.

See also the examples in the ProGuard manual.


Update (4.5 years later): Nowadays I used Timber for Android logging.

Not only is it a bit nicer than the default Log implementation — the log tag is set automatically, and it's easy to log formatted strings and exceptions — but you can also specify different logging behaviours at runtime.

In this example, logging statements will only be written to logcat in debug builds of my app:

Timber is set up in my Application onCreate() method:

if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
}

Then anywhere else in my code I can log easily:

Timber.d("Downloading URL: %s", url);
try {
// ...
} catch (IOException ioe) {
Timber.e(ioe, "Bad things happened!");
}

See the Timber sample app for a more advanced example, where all log statements are sent to logcat during development and, in production, no debug statements are logged, but errors are silently reported to Crashlytics.

How to remove Log.d() calls in release build of an Android app?

Make sure your proguard configuration does not have -dontoptimize in it. It's the proguard optimizer that removes method calls that have "no side effects".

The SDK default proguard-android.txt has -dontoptimize in it. Use proguard-android-optimize.txt instead, and don't add -dontoptimize in your project-specific proguard configuration.

Could you please state some source where I could read up on the subject. If it exists of course.

Well, for starters -assumenosideeffects is listed under optimization options in the documentation.

One more thing bothers is that, I have read that using proguard-android-optimize.txt can cause problems on some devices. So, it is a sort od catch 22 situation.

Proguard in general can have side effects. You need to test your proguard-processed application thoroughly.


If you want to remove logging from release builds without using proguard at all, you can e.g. add

if (BuildConfig.DEBUG)

before every logging call where BuildConfig is the build configuration generated by the Gradle Android plugin. Since the DEBUG there is a compile-time constant, the Java compiler sees the condition can never be true on a release configuration and won't emit the bytecode inside the conditional block.

Enable LogCat on Release Build in Android Studio

Add android:debuggable="true" (default is false) to your Manifest inside the <application> tag.

From the docs:

android:debuggable

Whether or not the application can be debugged,
even when running on a device in user mode — "true" if it can be, and
"false" if not.

respectively

You can disable debugging by removing the android:debuggable attribute
from the tag in your manifest file, or by setting the
android:debuggable attribute to false in your manifest file.

Edit

You may need to add the following to your build.gradle file inside the android{...} tag:

lintOptions {
checkReleaseBuilds false
}

And as a side-note: Right on the device the Logs are always written, no matter if your application's debuggable is set to false or true. But via the LogCat in Android Studio it's only possible if debuggable is set to true. (Just tested this)

How to turn off or intercept Logcat logs?

You can use ProGuard on the 3rd party library to strip it of all calls to the Log class as per this answer to a similar question: https://stackoverflow.com/a/2019002/1122135

If the source code is available for the library, I would recommend building your own version of the library without the debug output.



Related Topics



Leave a reply



Submit