Keeper Plugin
A simple Gradle plugin that hooks into Proguard/R8 to add extra keep rules based on what androidTest classes use from the target app's sources. This is necessary because AGP does not factor in androidTest usages of target app sources when running the minification step, which can result in runtime errors if APIs used by tests are removed.
This is a workaround until AGP supports this: https://issuetracker.google.com/issues/126429384.
This is optionally configurable via the keeper
extension. For example:
keeper {
automaticR8RepoManagement = false
r8JvmArgs = ["-Xdebug", "-Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=y"]
}
The general logic flow:
Create a custom
r8
configuration for the R8 dependency.Register two jar tasks. One for all the classes in its target
testedVariant
and one for all the classes in the androidTest variant itself. This will use their variant-provided JavaCompile tasks and KotlinCompile tasks if available.Register a
infer${androidTestVariant}UsageForKeeper
task that plugs the two aforementioned jars into R8'sTraceReferences
CLI and outputs the inferred proguard rules into a new intermediate .pro file.Finally - the generated file is wired in to Proguard/R8 via private task APIs and setting their
configurationFiles
to include our generated one.
Appropriate task dependencies (via inputs/outputs, not dependsOn
) are set up, so this is automatically run as part of the target app variant's full minified APK.
The tasks themselves take roughly ~20 seconds total extra work in the Slack android app, with the infer and app jar tasks each taking around 8-10 seconds and the androidTest jar taking around 2 seconds.