Force Crashlytics to send report right away – Android Test

I have a custom TestWatcher in which I hope to log failed tests. This would greatly improve my time of seeing failed tests instead of digging into logging of the CI. This is my code:

class CustomTestWatcher : TestWatcher() {
    override fun failed(
        e: Throwable?,
        description: Description?,
    ) {
        val err = "Thrown: ${e?.stackTraceToString() ?: "No throwable"}, description: $description"

        log { "Uploading......." }
        FirebaseCrashlytics.getInstance().log(err)
        FirebaseCrashlytics.getInstance().recordException(e ?: Throwable())
        FirebaseCrashlytics.getInstance().sendUnsentReports()

        log(false) { err }
    }
}

Now I KNOW Crashlytics will only upload crashes on app restart. The problem is that I am using these properties in my build.gradle which clears everything after the test, making the crashlytics reports undone:

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments clearPackageData: 'true'

Is there any hack I can add to crashlytics to FORCE to send it right away? I use it for testing purposes only.

Leave a Comment