Jacek Kwiecień
Jacek Kwiecień

Reputation: 12637

Using proguard with espresso/androidTest

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.

Upvotes: 11

Views: 4178

Answers (2)

chrisandrew.cl
chrisandrew.cl

Reputation: 867

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.

Upvotes: 12

T. Neidhart
T. Neidhart

Reputation: 6200

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'

Upvotes: 3

Related Questions