Android Unit Tests with proguard enabled

Denny1989 picture Denny1989 · Oct 21, 2014 · Viewed 9.1k times · Source

I have a problem that Proguard strips out methods of my debug APK (I need to run proguard on debug beccause of method dex file limit), even if they are used in the Test apk. E.g. i use GSON addProeprty method in Unit test, but not in the App apk. This method gets stripped away and causes the test to fail. But i do not want to configure proguard to just keep all of GSOn because of the dex file limit, but also do not want to list all methods seperately. is there a way to tell rpguard to consider the unit tests as source code entry points?

Answer

theJosh picture theJosh · Mar 11, 2016

This is what I did.

Add a custom proguard rules file.

/project/app/proguard-test-rules.pro

# Proguard rules that are applied to your test apk/code.
-ignorewarnings

-keepattributes *Annotation*

-dontnote junit.framework.**
-dontnote junit.runner.**

-dontwarn android.test.**
-dontwarn android.support.test.**
-dontwarn org.junit.**
-dontwarn org.hamcrest.**
-dontwarn com.squareup.javawriter.JavaWriter
# Uncomment this if you use Mockito
#-dontwarn org.mockito.**

The add the following to your build.gradle for your app. To use the proguard file when testing.

/project/app/build.gradle

android {
    debug {
        minifyEnabled true
        testProguardFile 'proguard-test-rules.pro'
    }
}