I have the same problem. I could not understand an absolutely elegant solution, but I will also post my approach.
What I was trying to do was override indeterminateDrawable on a ProgressBar. If there is a simple animation without animation and the Espresso test, there is no problem with downtime.
Unfortunately, main and androidTest handled the same way. I did not find a way to override the styles of my ProgressBar.
Now this has led to the pooling of some ideas from https://gist.github.com/Mauin/62c24c8a53593c0a605e#file-progressbar-java and How to determine if an Android application runs a UI test using Espresso .
First, I created custom ProgressBar classes, one for debugging and one for release. The release version only calls superconstructors and does nothing. The debug version overrides the setIndeterminateDrawable method. With this, I could install a simple option, not an animated one.
Release Code:
public class ProgressBar extends android.widget.ProgressBar { public ProgressBar(Context context) { super(context); } public ProgressBar(Context context, AttributeSet attrs) { super(context, attrs); } public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } }
Debug Code:
public class ProgressBar extends android.widget.ProgressBar { public ProgressBar(Context context) { super(context); } public ProgressBar(Context context, AttributeSet attrs) { super(context, attrs); } public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @SuppressWarnings("deprecation") @Override public void setIndeterminateDrawable(Drawable d) { if (isRunningTest()) { d = getResources().getDrawable(R.drawable.ic_replay); } super.setIndeterminateDrawable(d); } private boolean isRunningTest() { try { Class.forName("base.EspressoTestBase"); return true; } catch (ClassNotFoundException e) { } return false; } }
As you can see, I also added a check if my application runs the Espresso test, while the class I'm looking for is the base of my Espresso tests.
The bad news is that you need to update all your code in order to use your own ProgressBar. But itβs good that your release code does not have a significant impact on this decision.