lostintranslation
lostintranslation

Reputation: 24583

Using R8 and proguard to remove logging, but turn off everything else

I am trying to use R8 and proguard to remove logging from the release build. The catch is that I need to this be minimally invasive at the moment, so I would like to enable R8/proguard to remove logs, but turn off everything else. IE minifcation, obfuscation, etc.

build.gradle:

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

proguard-rules.pro:

-dontobfuscate
-dontoptimize
-dontshrink

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

However when building and deploying a release build the logs are not removed. I imagine that this is because assumenosideeffects runs as part of one of the options that I turned off.

I have also tried this:

-keep class ** { *; }

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

However that also still leaves logging.

Without moving to a different logging library or modifying code, is is possible to to remove logging with R8/proguard and not run anything else?

EDIT:

I an effort to figure out why proguard/r8 is not removing logs I created a brand new project. I added one line of logging with the following config:

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

However when I build the release version and install the APK I am still seeing logging, which means my log statement was not removed.

EDIT:

debuggable true 

Does skip r8 optimization. So this is not a good way to verify logs have been removed by proguard. Using a dex to jar application to verify is the way to go. dex2jar worked for me.

Upvotes: 2

Views: 3690

Answers (2)

Sumit Kumar
Sumit Kumar

Reputation: 292

Please remove debuggable true line from release block that's why you are seeing logs in build.

Upvotes: 6

Stoberdo
Stoberdo

Reputation: 21

I'm also using R8 to remove logs from release builds for some of my apps. The only difference I can see between your and my config are three asterisks before the v(), d(), ... functions.

So this is what's working for my release builds:

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

Edit: If this doesn't help, maybe this is cause by a behavior change in AGP (related question: Android Gradle Plugin 4.2.x changed behavior for assumenosideeffects)

Upvotes: 2

Related Questions