diff --git a/firebase-crashlytics/test-app/src/main/kotlin/com/google/firebase/testing/crashlytics/BaseActivity.kt b/firebase-crashlytics/test-app/src/main/kotlin/com/google/firebase/testing/crashlytics/BaseActivity.kt index c4bd4e9fdd0..928ca82e59e 100644 --- a/firebase-crashlytics/test-app/src/main/kotlin/com/google/firebase/testing/crashlytics/BaseActivity.kt +++ b/firebase-crashlytics/test-app/src/main/kotlin/com/google/firebase/testing/crashlytics/BaseActivity.kt @@ -29,6 +29,7 @@ open class BaseActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + PreFirebaseProvider.initialize() FirebaseApp.initializeApp(this) Log.i(TAG, "onCreate - ${getProcessName()} - ${getImportance()}") } diff --git a/firebase-crashlytics/test-app/src/main/kotlin/com/google/firebase/testing/crashlytics/PreFirebaseProvider.kt b/firebase-crashlytics/test-app/src/main/kotlin/com/google/firebase/testing/crashlytics/PreFirebaseProvider.kt index b7c027e889b..008a267e832 100644 --- a/firebase-crashlytics/test-app/src/main/kotlin/com/google/firebase/testing/crashlytics/PreFirebaseProvider.kt +++ b/firebase-crashlytics/test-app/src/main/kotlin/com/google/firebase/testing/crashlytics/PreFirebaseProvider.kt @@ -27,17 +27,33 @@ class PreFirebaseProvider : ContentProvider(), UncaughtExceptionHandler { private var defaultUncaughtExceptionHandler: UncaughtExceptionHandler? = null companion object { + var expectedMessage: String? = null + private var defaultUncaughtExceptionHandler: UncaughtExceptionHandler? = null + /** - * Set an expected exception message. If the uncaught exception contains the given string, then - * the app will exit cleanly. Otherwise, it will propagate up to the default exception handler. + * Initializes the PreFirebaseProvider manually. */ - var expectedMessage: String? = null - } + fun initialize() { + if (defaultUncaughtExceptionHandler == null) { + defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler() + Thread.setDefaultUncaughtExceptionHandler(object : UncaughtExceptionHandler { + override fun uncaughtException(thread: Thread, throwable: Throwable) { + if (isExpected(throwable)) { + // Exit cleanly + exitProcess(0) + } else { + // Propagate up to the default exception handler + defaultUncaughtExceptionHandler?.uncaughtException(thread, throwable) + } + } + private fun isExpected(throwable: Throwable): Boolean = + expectedMessage?.let { throwable.message?.contains(it) } ?: false + }) + } + } + } override fun onCreate(): Boolean { - defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler() - Thread.setDefaultUncaughtExceptionHandler(this) - return false }