android – Espresso testing disable animation-ThrowExceptions

Exception or error:

enter image description here

    public void test3_PaySuccessful(){

    ViewInteraction amountEditText = onView(
            allOf(withId(, isDisplayed()));
    amountEditText.perform(replaceText("SGD 0.010"), closeSoftKeyboard());

    //, withText("Proceed")
    ViewInteraction appCompatButton = onView(
            allOf(withId(, isDisplayed()));

    //, withText("Pay")
    ViewInteraction appCompatButton2 = onView(
            allOf(withId(, isDisplayed()));

    ViewInteraction appCompatButton3 = onView(
            allOf(withId(, withText("Confirm"), isDisplayed()));

    //have to disable animation in order to pass this.


I encountered an issue on doing Espresso testing with a view involving animation, I know Espresso cannot deal with animation, so i did below.
– disable my test device Window animation, transition animation and animator duration scale all set to OFF (this does not work)
– then i tried to add a flag in my code eg. espresso_testing = true. if true, my code will skip calling all startAnimation() function call. —> this is working. However, there is a requirement that I cannot change code on my app while writing espresso test case. Included a test case above.

Is there any other way to do this? Thanks in advance.

How to solve:

Make sure to keep your plugin updated:

buildscript {
  repositories {

  dependencies {
    classpath ''

Use the new flag in testOptions called animationsDisabled:

android {


  testOptions {
    animationsDisabled = true


You can try turning off animations on your device/emulator manually:

To avoid flakiness, we highly recommend that you turn off system
animations on the virtual or physical devices used for testing. On
your device, under Settings > Developer options, disable the following
3 settings:

Window animation scale Transition animation scale Animator duration


You can try using adb via command line:

# Turn off animations
adb shell settings put global window_animation_scale 0 &
adb shell settings put global transition_animation_scale 0 &
adb shell settings put global animator_duration_scale 0 &


You can try LinkedIn’s TestButler:



You can try creating a TestRule and Gradle task for your espresso tests:



True, you shouldn’t add test code in production code. The problem here lies in the animation. If you are performing animations using Handlers and Runnables then you can’t turn them off using the Developer Options. A common place where we use this to animate is in Custom views.

But even in custom views, ensure you use either ValueAnimator, ObjectAnimator or AnimatorSet to perform your animation. Only then you can turn off animations by turning off Animator duration scale in Developer options.

A good reference is the ProgressBar.


You could take a look at this repo

Build the project and download the generated .apk file and follow the instructions mentioned in that project to disable the animations and you should have a smooth sailing afterwards. You could also download the same .apk file from a lot of other sources.
Once you have the .apk file then issue the following commands:

adb install -r android_emulator_hacks.apk
adb shell pm grant no.finn.android_emulator_hacks android.permission.SET_ANIMATION_SCALE
adb shell am start -n no.finn.android_emulator_hacks/no.finn.android_emulator_hacks.HackActivity

This will take care of disabling the system animations for you.

Leave a Reply

Your email address will not be published. Required fields are marked *