AppMetrica stopped working after Firebase was updated to version 31+

When using Firebase Performance Monitoring, updating Firebase to version 31+ causes AppMetrica SDK to stop sending events.

This is what happens when you update Firebase:

  1. When building your app, the Firebase Performance Monitoring plugin replaces the network code with its own classes. These changes are applied for all processes.

  2. When you launch the app using ContentProvider, it initializes FirebaseApp, which is necessary for FirebasePerfomance to work.

  3. When sending an AppMetrica network request, the FirebasePerfUrlConnection class that got replaced at step 1 throws an exception.

    java.lang.NoClassDefFoundError: com.google.firebase.perf.config.RemoteConfigManager
    ....
    Caused by: java.lang.ExceptionInInitializerError 
    at com.google.firebase.perf.config.RemoteConfigManager.getInstance(RemoteConfigManager.java:119)
    ....
    Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.firebaseperfomancewithappmetrica:Metrica. Make sure to call FirebaseApp.initializeApp(Context) first.

    This happens because at step 1 FirebaseApp was activated only for the main process, while the network stack was substituted across the board.

How to fix it

  1. Activate FirebaseApp for all processes, including the AppMetrica SDK process. For Application#onCreate(), call FirebaseApp.initializeApp(this) before activating the AppMetrica SDK.

    Example:

    class MainApplication : Application() {
        
        override fun onCreate() {
            super.onCreate()
            
            // Init FirebaseApp for all processes
            FirebaseApp.initializeApp(this)
            
            // Then activate AppMetrica SDK
            YandexMetrica.activate(
                this,
                YandexMetricaConfig.newConfigBuilder(API_KEY)
                    .build()
            )
        }
    }
  2. Temporarily opt out of using Firebase Performance. To do this, stop applying the firebase-performance-plugin by removing it from build.gradle com.google.firebase.firebase-perf.

  3. Use an older version of Firebase.

This doesn't seem to be the expected behavior of the Firebase SDK and may be fixed in future versions.

If you didn't find the answer you were looking for, you can use the feedback form to submit your question. Please describe the problem in as much detail as possible. Attach a screenshot if possible.