Thursday 5 August 2021

Android Gradle Plugin 4.2.x changed behavior for assumenosideeffects

I noticed a behaviour change using the Android Gradle Plugin 4.2.x regarding the ProGuard assumenosideeffects rule. I use this rule in my Android library project to remove all trace and debug calls from the SLF4J logging facility.

Using AGP version 4.1.x the org.slf4j.Logger.trace method is removed as expected from my wrapper class when building a release variant. The line of code is not available anymore in the classes.jar of the resulting AAR file.

Using AGP 4.2.x the line is still present. This is reproduceable channging the version number with the setup below.

I added the android.util.Log dependency for testing reasons. The d method is removed after compilation in both AGP versions.

I would expect that the same rule works in both AGP versions. What I'm missing here? Is there something needed in addition?

In addition, why is the rule working for the android.util.Log example but not for org.slf4j.Logger.trace?

Here's a setup to reproduce this scenario:

buildscript {
    ext.kotlin_version = "1.5.20"
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
android {
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}
import android.util.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyLoggerWrapper {

    private final Logger logger;

    private MyLoggerWrapper(Logger logger) {
        this.logger = logger;
    }

    public static MyLoggerWrapper getLogger(Class<?> clazz) {
        return new MyLoggerWrapper(LoggerFactory.getLogger(clazz));
    }


    public void trace(String message) {
        logger.trace(message);
    }

    public void anotherTrace(String message) {
        Log.d("MYTAG",message);
    }
}
-keep class com.example.myapplication.MyLoggerWrapper { *; }

-assumenosideeffects public interface org.slf4j.Logger {
    public *** trace(...);
    public *** debug(...);
}

-assumenosideeffects public class android.util.Log {
    public *** d(...);
}


from Android Gradle Plugin 4.2.x changed behavior for assumenosideeffects

No comments:

Post a Comment