Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating Espresso with Proguard and Gradle

The app I am working on is being built with Gradle. I am trying to add Espresso as a dependency for instrument tests. The app itself has a few dependencies, one of which is Guava 15.0. To make this work, I use the version of Espresso with explicit dependencies, and exclude the bundled Guava (using my own instead).

dependencies {
   ...
   instrumentTestCompile fileTree(dir: 'libs/espresso-dependencies', include: '*.jar')
   compile 'com.google.guava:guava:15.0' 
   ...
}

When I try to build with gradle connectedInstrumentTest, I get errors related to missing methods and classes.

java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState
at com.google.android.apps.common.testing.ui.espresso.base.InputManagerEventInjectionStrategy.<init>(InputManagerEventInjectionStrategy.java:35)

Adding -keep class com.google.common.** { *; } to my Proguard config makes everything work fine. What seems to be happening is that Proguard is only analyzing the classes used by main app and it is not looking for usage by the instrument test dependencies. Methods/classes which are not being used by my main app, but which are required for the instrument tests, appear to be optimized away.

How can I make Proguard keep the Guava classes/methods required by Espresso and its dependencies too? It does not seem practical to specify them all manually (there are many usages), and keeping all of them defeats the purpose of Proguard.

like image 417
antonyt Avatar asked Nov 28 '13 08:11

antonyt


1 Answers

Here's what worked for me:

In build.gradle I added this line to my defaultConfig section:

testProguardFile "test-proguard-rules.pro"

Then I created test-proguard-rules.pro with the following contents:

-dontobfuscate
-dontwarn

This tells gradle to use this separate proguard configuration for your test apk, the one containing your instrumentation tests. In this case you are telling proguard to not obfuscate your test apk, which is probably what you want. The main apk you are testing still gets obfuscated using your existing proguard config.

like image 187
Ilan Klinghofer Avatar answered Sep 28 '22 05:09

Ilan Klinghofer