firebase / firebase-android-sdk

Firebase Android SDK
https://firebase.google.com
Apache License 2.0
2.23k stars 565 forks source link

FirebasePerformance crash for separate process java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process #6039

Open alexklints opened 1 week ago

alexklints commented 1 week ago

[READ] Step 1: Are you in the right place?

Issues filed here should be about bugs in the code in this repository. If you have a general question, need help debugging, or fall into some other category use one of these other channels:

[REQUIRED] Step 2: Describe your environment

[REQUIRED] Step 3: Describe the problem

Calling URL.openConnection() from separate process will lead crash with Firebase Performance. Stacktrace

FATAL EXCEPTION: DefaultDispatcher-worker-2
Process: com.alexklints.firebaseperformanceissue:Second, PID: 7698
java.lang.ExceptionInInitializerError
    at com.google.firebase.perf.config.ConfigResolver.<init>(ConfigResolver.java:78)
    at com.google.firebase.perf.config.ConfigResolver.getInstance(ConfigResolver.java:86)
    at com.google.firebase.perf.application.AppStateMonitor.<init>(AppStateMonitor.java:98)
    at com.google.firebase.perf.application.AppStateMonitor.getInstance(AppStateMonitor.java:87)
    at com.google.firebase.perf.metrics.NetworkRequestMetricBuilder.<init>(NetworkRequestMetricBuilder.java:92)
    at com.google.firebase.perf.metrics.NetworkRequestMetricBuilder.builder(NetworkRequestMetricBuilder.java:84)
    at com.google.firebase.perf.network.FirebasePerfUrlConnection.instrument(FirebasePerfUrlConnection.java:189)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.alexklints.firebaseperformanceissue:Second. Make sure to call FirebaseApp.initializeApp(Context) first.
    at com.google.firebase.FirebaseApp.getInstance(FirebaseApp.java:179)
    at com.google.firebase.perf.config.RemoteConfigManager.getInitialStartupMillis(RemoteConfigManager.java:92)
    at com.google.firebase.perf.config.RemoteConfigManager.<init>(RemoteConfigManager.java:86)
    at com.google.firebase.perf.config.RemoteConfigManager.<clinit>(RemoteConfigManager.java:53)
    ... 16 moreFATAL EXCEPTION: DefaultDispatcher-worker-2

Steps to reproduce:

  1. Add Firebase Perf to project.
  2. Make network request with UrlConnection from activity.
  3. All works.
  4. Than move activity to separate process. Activity just for simple example. It can be a service in a separate process.
  5. Crash occurs.
  6. Expected behavior: Requests will be executed correctly in a separate process without activating FirebaseApp.

It works correct up to com.google.firebase:firebase-bom:31.0.0.

Relevant Code:

Example with network request from separate process

google-oss-bot commented 1 week ago

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

lehcar09 commented 1 week ago

Hi @alexklints, thank you for for reaching out and raising the issue. Thank you for sharing an MCVE. I was able to reproduce the issue from version 31.2.0 up to the latest version. I'll notify our engineers about and see what we can do here. Thanks!

mrober commented 1 week ago

Firebase only initializes automatically on the default process of an app. If you have a multi-process app, you need to initialize manually in each process or not use Firebase in the other processes.

To initialize manually, you could do something like this:

class MyApp : Application() {
  override fun onCreate() {
    super.onCreate()

    // Initialize Firebase in other processes.
    if (getProcessName() != applicationInfo.processName) {
      FirebaseApp.initializeApp(applicationContext)
    }
  }
}
alexklints commented 1 week ago

Hi.

I understand, but the thing is, the behavior used to be different. Firebase Performance did not attempt to intercept the network if there was no initialization in the process. There are such examples of an issue where developers tried to implement this behavior:

mrober commented 14 hours ago

I took a look, and this was fixed in Firebase Perf 21.0.1, can you try the latest version @alexklints?