Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have noticed that my release build, which i made in eclipse via:

Android Tools -> Export signed application package ...

My proguard-project.txt contains:

-assumenosideeffects class android.util.Log {
    public static boolean isLoggable(java.lang.String, int);
    public static int v(...);
    public static int i(...);
    public static int w(...);
    public static int d(...);
    public static int e(...);
}

Yet, if I run the app on a device und use CatLog to look at logs I see that there are all the Log.d(), Log.v() messages that shouldn't be there. Is there something else I should set? I have spent many hours googling but there are no clear instructions on that subject. Well most of the things i find are people with similar problems but no solutions.

like image 428
f470071 Avatar asked Oct 11 '15 16:10

f470071


People also ask

Why is it important to remove all debug log statements before releasing an app for general distribution and use?

Because they can contain user sensitive data.

What is Logcat D?

Logcat is a command-line tool that dumps a log of system messages, including stack traces when the device throws an error and messages that you have written from your app with the Log class. This page is about the command-line logcat tool, but you can also view log messages from the Logcat window in Android Studio.


1 Answers

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.

like image 127
laalto Avatar answered Sep 28 '22 07:09

laalto