How Do I Stop Gradle Instrumented Test Runs From Uninstalling the App?

The connectedCheck task has the type DeviceProviderInstrumentTestTask. For a simple test run on one device it uses a SimpleTestRunner, which in turn uses a SimpleTestRunnable to actually execute the test. Here you find a structure of

try {
    // connect to device
    // install all APKs
    // run tests
} catch(Exception e) {
    // handle error
} finally {
    // get test report
    // uninstall all APKs
    // disconnect from device
}

I'm not perfectly sure if I've found the most recent implementations, but this exact behavior dates back several years. So I guess you can't achieve what you're asking for.


The instrumentation installs 2 APKs: the APK under test and the APK with the test code.

It also uninstalls both APKs before it tries to install the new ones and I don't know if it's possible to prevent the uninstall itself.

testApplicationId changes only application id for the APK with the test code (which is normally the same as for the main APK with ".test" appended) the application id of the APK under test remains still the same. But it is possible to create separate buildType for the APK under test (with exactly the same configuration as the debug build type) and use that.

Then connectedAndroidXYZTest could be used to run the tests (or createXYZCoverageReport).


Maybe try to run it via adb like this:

adb shell am instrument -w com.android.demo.app.tests/android.support.test.runner.AndroidJUnitRunner

It will not uninstall your app.

here it is described in more details.