Monday, May 12, 2014

android: How to turnoff logs while publishing application

As per Android, we need to turn off logging before publishing the application to Google play store.
We have lot of doubts like
How to remove all the debug logging calls before publishing?
How to achieve turning off logs in application?

Here we have a small solution to achieve.
First, we should be aware of the application tools like proguard and build system of eclipse and ant.
  1. We need to check whether the debuggable is true or false in manifest file.
  2. Know about BuildConfig.DEBUG.
  3. Usage of Proguard


android:debuggable
A value excepted by the android:debuggable from the  tag in your manifest file is true or false.
The default value is false, it specify the application can be debugged, even when running on a device in user mode. If it is true, then the application can’t be debugged.

But recent ADT plugin of eclipse or ant make sure depending upon the build system. The flag will be set to true, if we are exporting application in debug mode. When you export a signed release build, the flag will be set to false, thus turning off and on debug features automatically, without you having to remember to toggle a flag in manifest file.
 BuildConfig.DEBUG
In recent versions of the Android Developer Tools (ADT) for Eclipse, then android added new feature that allows you to run some code only in debug mode. Builds now generate a class called BuildConfig containing a DEBUG constant that is automatically set according to your build type. We can check the (BuildConfig.DEBUG) constant in your code to run debug-only functions such as outputting debug logs. During development it will be set to true, therefore allowing you to see our logging statements during development. For example, we can use it to control logging, like below:

if (BuildConfig.DEBUG) {
    Log.e(TAG, "error");  
    Log.w(TAG, "warn");  
    Log.i(TAG, "info");  
    Log.d(TAG, "debug");  
    Log.v(TAG, "verbose");  
}

Note:
Verbose should never be compiled into an application except during development. 
Debug logs are compiled in but stripped at runtime.
Error, warning and info logs are always kept.

Proguard find a far easier solution is to forget all the if checks all over the place and just use to strip out any Log.d() or Log.v() method calls when we call our ANT release target. We can find few optimization options of Proguard.

Especially we have assumenosideeffects method, that doesn’t have any side effects.  In the optimization step, ProGuard will then remove calls to such methods. ProGuard will analyze your program code to find such methods automatically. It will not analyze library code, for which this option can therefore be useful.  With some care, you can also use the option to remove logging code. 

Note that ProGuard applies the option to the entire hierarchy of the specified methods. It is applicable when optimizing. In general, making assumptions can be dangerous; you can easily break the processed code. Only use this option if you know what you're doing!

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.

Removing logging code

For example, this configuration removes invocations of the Android logging methods:
-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(...);
}

See also the examples  in the ProGuard manual.

Which is the best method to use, to remove logs from application while publishing?

Method 1: Even the debuggable is true or false, the logs will be printed. So, it is not right approach and correct.

Method 2: If
 BuildConfig.DEBUG, works as expected then it is the best method to control the logs but there are some issues reported like Issue 27940 , Issue 59295 and some issues with Gradle & studio.

Method 3: Proguard is configuration approach, It works the best compare to other approaches but we should be aware in using it.

Compare to first method, the second and third approach are best to remove logs from the application but as we developers should be aware while using it properly.



Thanks for reading :) 
Whether this post is helpful?

Have something to add to this post? If you have any other quick thoughts/hints that you think people will find useful? Share it in the comments.

No comments :

Post a Comment