Android java.lang.SecurityException Injecting to another application requires INJECT_EVENTS permission



I’m using ActivityInstrumentationTestCase2 to test my Android apps in combination with TouchUtils both part of the standard Android Testkit as described in Googles Android Developer documentation in section Activity Testing. My test work fine locally so I tried to run them on Cricle-CI. For using the Android Emulator on Circle-CI I created a circle.yml as described in your docs. However all my tests which utilize TouchUtils fail with a java.lang.SecurityException telling me that I don’t have the permissions to inject events in another application. I’ve seen that message before locally, since this happens when trying to simulate clicks on any activity while the testing device is locked. After some googling I found out, that it’s possible to unlock the emulator via command line with adb shell input keyevent 82 so I added this step to my circle.yml file. However this didn’t change anything and my tests still fail. Any ideas how I can unlock the Android Emulator running on Circle-CI or maybe any idea what else that exception may cause?

    # start the emulator
    - emulator -avd circleci-android22 -no-audio -no-window:
        background: true
        parallel: true
    # wait for it to have booted
    - circle-android wait-for-boot
    # run tests  against the emulator.
    # wakeup & unlock the emulator
    - sleep 3
    - fb-adb shell input keyevent 82
    - sleep 3
    - ./gradlew connectedAndroidTest
    # copy the build outputs to artifacts
    - cp -r cashier/build/outputs/apk $CIRCLE_ARTIFACTS
    # copy the test results to the test results directory.
    - cp -r cashier/build/outputs/androidTest-results $CIRCLE_TEST_REPORTS
    - cp -r cashier/build/outputs/reports $CIRCLE_TEST_REPORTS


Looks like I fixed it myself. Using a slightly higher sleep of 5 seconds did fix the issue. Guess the emulator is just quite slow.