Using proguard with espresso/androidTest

2020-08-15 03:40发布

问题:

I'm trying to configure proguard to use it with my espresso UI test flavor. The thing is Proguard tends to ignore my debug proguard config.

This is how the config looks:

buildTypes {
    debug {
        minifyEnabled true
        proguardFiles 'proguard-debug.pro'
        testProguardFile 'proguard-debug.pro'
        signingConfig signingConfigs.release
    }
}

I added testProguardFile but it doesn't seem to work on androidTest. I'm running mockDebug flavor variant. When I just run the app it works fine, but when I try to run test which is located in adnroidTest it won't run due to proguard warnings, like the proguard file wasn't processed at all and the file is pretty straight forward:

proguard-debug.pro

-dontobfuscate
-dontoptimize
-dontwarn

Before someone starts to advise me turning off proguard for debug builds: I need to have it enabled because of multidex.

回答1:

If you want your test build as close as possible from the real deal, try this one:

# build.gradle
debug {
    minifyEnabled true
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    testProguardFile 'proguard-test.pro'
}

and

# proguard-test.pro:
-include proguard-rules.pro
-keepattributes SourceFile,LineNumberTable

On other hand, if you need it only because multidex, if your are using minSdkVersion < 21, ProGuard is tied to multidex flag and run automatically.



回答2:

You also need to add the default proguard rules:

proguardFiles getDefaultProguardFile('proguard-android.txt')

This line can be removed, as it is a duplicate:

testProguardFile 'proguard-debug.pro'