How to make a library act as a no-op in Debug mode?

1.1k Views Asked by At

This question pertains specifically to Flurry, but I'd be curious how to do it for any 3rd party language.

Essentially, Flurry is causing a crash in Robolectric (as per: https://github.com/robolectric/robolectric-gradle-plugin/issues/144) and the only way I can get my unit tests to work is to remove all calls to Flurry (e.g. FlurryAgent.init, FlurryAgent.onStartSession, etc...).

Short of putting all of these under a BuildConfig.Debug 'if' block (which means having a lot of them), is there any way to globally cause all calls to FlurryAgent compile as no-ops in debug mode, for example?

LeakCanary is a good example of the effect I would like to see.

 dependencies {
   debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
   releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
 }
1

There are 1 best solutions below

4
On

We are also using Flurry. So for the rest of application Flurry is hidden by our Analytics class and this helps us easy to mock it and verify interaction with it.

To test Analytics itself we use another technics:

public class Analytics {
   ...
   protected startFlurry(Context context, String apiKey) {
      FlurryAgent.onStartSession(context, apiKey);
   }
   ...
}

And the test:

public class AnalyticsTest {
   ...
   @Before
   protected setUp() {
     analytics = new Analytics();
     analyticsSpy = spy(analytics);
     doNothing().when(analyticsSpy).startFlurry(any(Context.class), anyString());
   }
   ...
}

This is workaround but I don't have better solution for now.