androidfirebasefirebase-analytics

After upgrading Firebase SDK I get the error: There are multiple DataStores active for the same file: datastore/aqs/sessionConfigsDataStore.data


I have a runtime environment switcher where we delete and reinitialize the default Firebase app to point to the correct Firebase app based on the environment. This allows us to have separate analytics and Crashlytics for each environment for the same app

I upgraded my Firebase SDKs to the latest versions a couple of days ago. since then, our environment switcher crashes with this error:

java.lang.IllegalStateException: There are multiple DataStores active for the same file: files/datastore/aqs/sessionConfigsDataStore.data. You should either maintain your DataStore as a singleton or confirm that there is no two DataStore's active on the same file (by confirming that the scope is cancelled).

Here is our Firebase initialization code:

    suspend fun reset() {
        FirebaseApp.getApps(context).forEach {
            it.delete()
        }

        init()
    }

    suspend fun init() {
        val environment = environmentRepository.getEnvironment()
        val configFileName = environment.configFileName

        val inputStream = context.assets.open(configFileName)
        val jsonString = inputStream.bufferedReader().use { it.readText() }

        val firebaseOptionsJsonFile = gson.fromJson(jsonString, FirebaseJsonFileOptions::class.java)

        val firebaseOptions = firebaseOptionsJsonFile.mapToFirebaseOptions()

        val isNotAlreadyInitialized =
            FirebaseApp.getApps(context).none { it.name == DEFAULT_APP_NAME }

        if (isNotAlreadyInitialized) {
            FirebaseApp.initializeApp(context, firebaseOptions)
            Timber.d("$TAG: Firebase initialized with environment: $environment")
        } else {
            Timber.d("$TAG: FirebaseApp already initialized with environment: $environment")
        }
    }

I tried setting multiple apps instead of recreating the default app; however, I faced this limitation:

Note: On Android and Apple platforms, Analytics are only logged for the default app.

In my mind, there should be a better way to change the Firebase default app configs instead of deleting and recreating it. This way, we don't face the datastore crash


Solution

  • I don't think this is a very good strategy:

    I have a runtime environment switcher where we delete and reinitialize the default Firebase app to point to the correct Firebase app based on the environment.

    It's doubtful that the Android SDK was designed to be used in this way. Instead of this, you should pick an environment at the time of launch (perhaps through some settings), and continue with that for as long as the app remains running. When you need to switch environments, you should make a change in your settings for the new environment, then arrange to completely stop and re-launch the app with the new settings so that you don't have to try to make changes to the default FirebaseApp instance after it's already been created.

    See: