Denny1989
Denny1989

Reputation: 649

Android Unit Tests with proguard enabled

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?

Upvotes: 30

Views: 11491

Answers (4)

GLee
GLee

Reputation: 5093

None of the above answers did the trick for me. I had two issues: I needed to also use the default proguard file for testing, and my default proguard file was wrong.

  1. To use the default proguard file, in addition to your own:

    android {
        debug {
            minifyEnabled true
            testProguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project-test.pro'
        }
    }
    
  2. The default proguard file (and all of the tools/proguard folder) is apparently not replaced by default when you update the SDK tools through Android studio. My machine was using an outdated config file, which was causing weird proguard issues. To update proguard's default config, replace ~/Library/Android/Sdk/tools/proguard/proguard-android.txt with this.

Upvotes: 7

theJosh
theJosh

Reputation: 3084

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'
    }
}

Upvotes: 45

user823629
user823629

Reputation: 550

Instrumentation tests (and others?) do not use the same proguard file as your debug/release apk's. You might try setting the testProguardFile option inside the debug and release blocks. This test-specific proguard file can be very permissive because it's not being used for the debug/release apk's.

Upvotes: 4

Karsten
Karsten

Reputation: 310

I've solved this problem in my build by having an additional "dev" buildType where I enable proguard, but configure it to keep all code in my own package, and a few specific library classes that happen to be used from tests only. I also disable obfuscation in the dev buildType so that it can be debugged from an IDE.

For debug and release builds I use my "real" proguard settings including obfuscation and optimizations.

Upvotes: 1

Related Questions