Remove Log.d calls via Proguard
Proguard is a tool on Android that shrinks, optimize, and obfuscate your code. We want to use this on our release builds. In Gradle, we enable this either in the buildType or product flavor closure. The details can be found in the Android Tools User GuideExample build.gradle:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
android { | |
compileSdkVersion 19 | |
buildToolsVersion "19.0.1" | |
defaultConfig { | |
minSdkVersion 10 | |
targetSdkVersion 19 | |
versionCode 1 | |
versionName "1.0" | |
} | |
buildTypes { | |
release { | |
runProguard true | |
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.txt' | |
} | |
} | |
} |
The second thing to notice here is the inclusion of a second txt file, the proguard-rules.txt. This text file is how we tell Proguard to remove calls to Log.d, Log.v, Log.i, and whatever else you think is safe to remove.
Example proguard-rules.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-assumenosideeffects class android.util.Log { | |
public static *** d(...); | |
public static *** v(...); | |
public static *** i(...); | |
} |
Please note that if for whatever reason you decide to use the returned integer from Log.d and assign it to a variable, Proguard will think that it maybe a useful method and won't remove it. So make sure that whatever method you decided to use this option with does not return a value that's being used, if your intention is to remove the call.
So now if we compile the release build, we would effectively remove the Log.d. So lets take the following example set of calls
Before Proguard:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Log.v(TAG, "I'm verbose"); | |
Log.d(TAG, "I like to debug"); | |
Log.d(TAG, new UserInfo(mUserId).getDisplayName()); | |
Log.w(TAG, "warning!"); | |
Log.e(TAG, "ERROR!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new u(i).g(); | |
Log.w(t, "warning!"); | |
Log.e(t, "ERROR!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (BuildConfig.DEBUG) { | |
Log.v(TAG, "I'm verbose"); | |
Log.d(TAG, "I like to debug"); | |
Log.d(TAG, new UserInfo(mUserId).getDisplayName()); | |
} | |
Log.w(TAG, "warning!"); | |
Log.e(TAG, "ERROR!"); |
to be continued...