Support minification with better consumer proguard rules

Open
#2,167 3 comments 1 reaction 1 assignee View on GitHub

@brettchabot is already working on this.

Since Mar 11, 2024.

Assessment

This issue has not been assessed yet.

Description

Description

Currently, androidx.test doesn't bundle in sufficient proguard rules, which makes testing with minification difficult, especially in self-instrumenting tests which otherwise don't suffer from the test apk / app apk retention issues.

Note that AGP 8.3 recently added support for test minification

In androidx.benchmark we're looking to add these rules on our own for the time being, but it would be preferable and more generally useful to other libraries interested in minification if these could be handled directly by androidx.test.

Steps to Reproduce

Run sample, commenting out the testProguardRules in build.gradle

Note that sample includes simple case (ExampleTest) and more complex case (ExampleBenchmark). Benchmark gradle plugin already sets testBuildType="release"

Expected Results

Self-instrumenting com.android.library module tests don't require additional proguard rules by default

Actual Results

Several failures, requiring several workarounds in proguard file (not including those from b/328649293, which are an AGP issue):

### basic protection against junit/androidx.test reflection, shouldn't affect library/test code
# (fixes `Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass)`)
-keepclasseswithmembers class androidx.test.** { *; }
# not needed in this sample, but needed in practice in more complex samples in androidx repo
-keepclasseswithmembers class org.junit.** { *; }
# not needed in this sample, but needed in practice in more complex samples in androidx repo
-keepclasseswithmembers class junit.** { *; }
# (fixes `Missing classes detected while running R8.`)
-dontwarn com.google.errorprone.annotations.MustBeClosed

### keep test classes
# (fixes `Failed loading specified test class 'com.example.benchmark.ExampleTest'`)
-keepclasseswithmembers @org.junit.runner.RunWith class * { *; }

### needed for org.junit.Test annotation to be discoverable by reflection
# (fixes `Invalid test class 'com.example.benchmark.ExampleTest': No test methods found`)
-keepattributes *Annotation*
AndroidX Test and Android OS Versions
  • androidx.test.ext:junit:1.1.5
  • androidx.test:runner:1.5.2
  • Android OS version: N/A

The above rules could likely be made much more minimal, but in microbenchmark, we know androidx.test/junit/kotlin-test are never on the critical path, so we keep aggressively.

Link to a public git repo demonstrating the problem:

See attached repro project: androidxTestR8MicroSample.zip

Dominant language
Java
Stars
1.2k
Forks
342
Avg merge
1d 14h
Merged PRs (30d)
5

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from android/android-test

All issues in android/android-test

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.