Search code examples
androidgradleinstrumentation

proguard gradle debug build but not the tests


I enabled proguard for the debug build using:

android {
    buildTypes {
        debug {
            runProguard true
            proguardFile 'proguard-debug.txt'
        }
        release {
            runProguard true
            proguardFile 'proguard-project.txt'
            zipAlign true
        }
    }
}

The problem I'm experiencing when I do this is that the gradle build wants to proguard the tests during the proguardDebugTest task as well. I can't seem to modify to get access to this particular task. Is there a way I can proguard the debug apk but not the test apk?


Solution

  • Put

    gradle.projectsEvaluated {
        proguardDebugTest.enabled = false
    }
    

    it in your build script.

    There are two things to know here:

    • The general Gradle feature to enable / disable tasks.
    • The Android Gradle plugin specific deferred creation of tasks in afterEvaluate, so you need to also defer disabling of the task to afterEvaluate.

    EDIT:

    One small note: It disables the task but fails the build. This is because the :preDexDebugTest task wont run with proguard on. The best solution i've found so far is to have debug specific proguard config. More details here. Create a separate proguard config file, include the regular proguard file like so:

    -include proguard.cfg
    

    and add test config. For me it was:

    -dontwarn org.mockito.**
    -dontwarn sun.reflect.**
    -dontwarn android.test.**