Search code examples
androidproguardandroid-r8

building androidTest apk gets R8 errors: "already has a mapping"


I have added minifyEnabled=true to my 'release' build. The app runs correctly. And the androidTest apk runs correctly (all tests pass).

If I add any of the following to proguard-rules.pro:

-keepattributes LineNumberTable
-keepattributes LocalVariableTable
-keepattributes LocalVariableTypeTable

the app apk will build without error, but while building the androidTest apk I get ~4000 R8 "already has a mapping" errors for task:

:app:transformClassesAndResourcesWithR8ForReleaseAndroidTest

The (truncated) error log is

androidTest build error: already has a mapping

It appears that the methods that are getting the error are in 3rd party libraries (included as dependencies).

Thanks in advance for any help.


Solution

  • I solved the problem by adding an additional buildType for testing the 'release' configuration ('releaseTest'). It inherits from 'release', and sets debuggable=true.

    buildTypes {
        debug {
            ...
        }
        release {
            ...
            minifyEnabled true    // enable code shrinking & obfuscation
            shrinkResources true  // enable resource shrinking
            ...
        }
        releaseTest {
            // inherit from 'release' buildType
            initWith release
            // for dependencies that don't know what 'releaseTest' is
            matchingFallbacks = ['release']
    
            debuggable true
       }