0

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.

lostintranslation
  • 23,756
  • 50
  • 159
  • 262

2 Answers2

4

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

lostintranslation
  • 23,756
  • 50
  • 159
  • 262
Sumit Kumar
  • 263
  • 1
  • 6
  • I am fairly certain that the debuggable property just makes the application debuggable, allowing for the logs to be shown in the console. However if R8 is removing the logs like it should be, even though the application is debuggable, those logs should have been removed from the build. This is a way to test that the logging code has been removed by R8 in a release build. – lostintranslation Mar 14 '22 at 13:53
  • 2
    @lostintranslation, Sumit Kumar is right. If application set to debug, proguard/R8 won't optimize. If code is optimised, how can you debug. You have conflicting settings in your gradle. Set debuggable to false in release and try your build. Use https://github.com/skylot/jadx tool to verify if log statements are being removed. – ddassa Mar 16 '22 at 12:03
1

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)

ouflak
  • 2,458
  • 10
  • 44
  • 49
Stoberdo
  • 11
  • 1
  • 4
  • No luck. Can I ask how you are verifying that your logging has indeed been removed? – lostintranslation Mar 15 '22 at 22:58
  • 1
    Strange. I verified that the logging is stripped away by decompiling my apk with `dex2jar` and then using a diff-tool to compare this to a version that uses no minification. – Stoberdo Mar 16 '22 at 07:41