How to configure proguard to ONLY remove android logging calls
Asked Answered
M

2

23

I'm trying to configure proguard to ONLY remove calls to android.util.Log from my Android app (for the release build). I specifically don't want proguard to do any obfuscation or minification of the code.

This is the configuration I've tried but it doesn't remove the Log calls (I assume because of the -keep class **)

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod

-keep class ** {
    *;
}

-assumenosideeffects class android.util.Log {
    *;
}

Is what I'm asking even possible with proguard?

Meaningless answered 22/3, 2013 at 13:26 Comment(0)
X
35

You can remove logging calls with this option in proguard-project.txt:

-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(...);
}

This option is only relevant if optimization is not disabled, like in proguard-android.txt. You have to specify proguard-android-optimize.txt instead, in project.properties:

proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt

You can disable shrinking and obfuscation if you wish. You can also preserve the internal API of your application from optimization if you wish:

-keep class myapp.** { *; }

Disabling these steps and keeping all code of course isn't optimal from a ProGuard point of view.

Xylia answered 23/3, 2013 at 22:56 Comment(3)
Seems like the problem was the default global proguard config disables optimization. For now I've just dropped proguard and wrapped my logging calls with a check for a debug flag. Anyway I'll accept your answer since it solves my original question.Meaningless
I noticed the web call can still show up in BasicNetwork.logSlowRequests and Request.finish following that...how would you remove that?Spirillum
@Eric Could you please provide some source on that. It is not like I do not believe you, I do. But this matter is unexplained and a source of a lot of grieve.Adames
U
13

In build.gradle

buildTypes {

        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

In proguard-rules.pro

-dontwarn **
-target 1.7
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose

-optimizations !code/simplification/arithmetic,!code/allocation/variable
-keep class **
-keepclassmembers class *{*;}
-keepattributes *

#This will not remove error log
-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(...);
}
Upandcoming answered 11/6, 2015 at 16:19 Comment(3)
You will see error logs. Remove # before public static int e(...); and before public static boolean isLoggable(java.lang.String, int); Also make sure you run Release build. The changes were for release build.Upandcoming
I was wrong Sandeep. It does work.. I apologize. I missed to change proguard-android.txt - > proguard-android-optimize.txtConnoisseur
I want to ask one question that How can we hide complete code from APK so that no one can get code and assets by reverse engineering.Clavichord

© 2022 - 2024 — McMap. All rights reserved.