Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FirebaseApp is not initialized in this process #4693

Closed
hram opened this issue Feb 17, 2023 · 66 comments
Closed

FirebaseApp is not initialized in this process #4693

hram opened this issue Feb 17, 2023 · 66 comments
Assignees

Comments

@hram
Copy link

hram commented Feb 17, 2023

[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:

  • For general technical questions, post a question on StackOverflow
    with the firebase tag.
  • For general Firebase discussion, use the firebase-talk
    google group.
  • For help troubleshooting your application that does not fall under one
    of the above categories, reach out to the personalized
    Firebase support channel.

[REQUIRED] Step 2: Describe your environment

  • Android Studio version:
  • Firebase Component: com.google.firebase:firebase-bom:31.2.2
  • Component version:

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

  1. Add annotation @AddTrace to function onCreate() in the Application class
  2. Init firebase FirebaseApp.initializeApp(this) inside function onCreate()
  3. Start app
  4. Application crashes on startup

Relevant Code:

Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process <my app id>:Metrica. 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:91)
        at com.google.firebase.perf.config.RemoteConfigManager.<init>(RemoteConfigManager.java:85)
        at com.google.firebase.perf.config.RemoteConfigManager.<clinit>(RemoteConfigManager.java:52)
        at com.google.firebase.perf.config.RemoteConfigManager.getInstance(RemoteConfigManager.java:119) 
        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.Trace.<init>(Trace.java:106) 
        at com.google.firebase.perf.metrics.Trace.create(Trace.java:98) 
        at com.google.firebase.perf.FirebasePerformance.startTrace(FirebasePerformance.java:213) 
        at <my app id>.App.onCreate(Unknown Source:6) 
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1118) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5791) 
        at android.app.ActivityThread.-wrap1(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
@google-oss-bot
Copy link
Contributor

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

@argzdev
Copy link
Contributor

argzdev commented Feb 17, 2023

Hi @hram, thanks for reaching out to us. I'm not experiencing the same behavior with the steps you've provided. I added @AddTrace to my application class, but there seems to be no issue.

Relevant code:

class MainApplication: Application() {
    @AddTrace(name = "onCreateTrace", enabled = true /* optional */)
    override fun onCreate() {
        FirebaseApp.initializeApp(this)
        super.onCreate()
    }
}

Dependencies:

  implementation platform('com.google.firebase:firebase-bom:31.2.2')
  implementation 'com.google.firebase:firebase-analytics-ktx'
  implementation 'com.google.firebase:firebase-perf-ktx'

Manifest:

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        ...
        android:name=".MainApplication"
        tools:targetApi="31">

Am I missing anything?

@google-oss-bot
Copy link
Contributor

Hey @hram. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

@tevjef
Copy link

tevjef commented Mar 1, 2023

@argzdev I believe the app has to startup a separate process where onCreate is called again. It seems Firebase now fails to initialize on non main processes.

<service
  android:name="com.company.NewProcess"
  android:process=":NewProcess" />

Starting this service would result in a crash.

@argzdev
Copy link
Contributor

argzdev commented Mar 1, 2023

Hi @tevjef, thanks for the insight. I'm not sure if I'm doing something wrong, but I still can't replicate the same behavior even with the service.

AndroidManifest:

<application
...
        <service
            android:name=".RandomService"
            android:process=":NewProcess" />
</application>

Random Service code:

class RandomService: Service() {
override fun onBind(p0: Intent?): IBinder? = null

    @AddTrace(name = "onCreateServiceTrace", enabled = true)
    override fun onCreate() {
        super.onCreate()
        Log.d(TAG, "onCreate: ")
        FirebaseApp.initializeApp(this)
    }

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        super.onStartCommand(intent, flags, startId)
        Log.d(TAG, "onStartCommand: ")
        return START_STICKY
    }

    override fun onDestroy() {
        super.onDestroy()
        stopForeground(STOP_FOREGROUND_REMOVE)
        stopSelf()
        Log.d(TAG, "Random Service is being killed")
    }
}

MainActivity:

    fun startRandomService(view: View) {
        Intent(this, RandomService::class.java).also { intent ->
            startService(intent)
        }
    }

    fun endRandomService(view: View) {
        Intent(this, RandomService::class.java).also { intent ->
            stopService(intent)
        }
    }

@hram
Copy link
Author

hram commented Mar 1, 2023

@argzdev why do you call FirebaseApp.initializeApp(this) before super.onCreate()?
what happens if you put FirebaseApp.initializeApp(this) after super.onCreate()?

@argzdev
Copy link
Contributor

argzdev commented Mar 1, 2023

Hi @hram, I tried it with both scenario (before or after onCreate()), but still unable to reproduce this behavior. I'm not sure what I'm missing here. That said, any chance you could share us a minimal reproducible example so I can investigate this further? Thanks!

@google-oss-bot
Copy link
Contributor

Hey @hram. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

@tevjef
Copy link

tevjef commented Mar 8, 2023

@argzdev https://github.com/tevjef/android-bugs/tree/firebase-android-sdk-issues-4693

When launching the app:

Observe firebase is initialized properly.
Click the Next button to start a service in a new process
Observe failure in Logcat this error

                                                     java.lang.ExceptionInInitializerError
                                                                                                    	at com.google.firebase.perf.config.RemoteConfigManager.getInstance(RemoteConfigManager.java:119)
                                                                                                    	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.Trace.<init>(Trace.java:106)
                                                                                                    	at com.google.firebase.perf.metrics.Trace.create(Trace.java:98)
                                                                                                    	at com.google.firebase.perf.FirebasePerformance.startTrace(FirebasePerformance.java:213)
                                                                                                    	at com.example.myapplication.App.onCreate(Unknown Source:2)
                                                                                                    	at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1266)
                                                                                                    	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6785)
                                                                                                    	at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
                                                                                                    	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2134)
                                                                                                    	at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                    	at android.os.Looper.loopOnce(Looper.java:201)
                                                                                                    	at android.os.Looper.loop(Looper.java:288)
                                                                                                    	at android.app.ActivityThread.main(ActivityThread.java:7898)
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
                                                                                                    Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.myapplication:NewProcess. 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:91)
                                                                                                    	at com.google.firebase.perf.config.RemoteConfigManager.<init>(RemoteConfigManager.java:85)
                                                                                                    	at com.google.firebase.perf.config.RemoteConfigManager.<clinit>(RemoteConfigManager.java:52)
                                                                                                    	at com.google.firebase.perf.config.RemoteConfigManager.getInstance(RemoteConfigManager.java:119) 
                                                                                                    	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.Trace.<init>(Trace.java:106) 
                                                                                                    	at com.google.firebase.perf.metrics.Trace.create(Trace.java:98) 
                                                                                                    	at com.google.firebase.perf.FirebasePerformance.startTrace(FirebasePerformance.java:213) 
                                                                                                    	at com.example.myapplication.App.onCreate(Unknown Source:2) 
                                                                                                    	at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1266) 
                                                                                                    	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6785) 
                                                                                                    	at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0) 
                                                                                                    	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2134) 
                                                                                                    	at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                                                    	at android.os.Looper.loopOnce(Looper.java:201) 
                                                                                                    	at android.os.Looper.loop(Looper.java:288) 
                                                                                                    	at android.app.ActivityThread.main(ActivityThread.java:7898) 
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) 

@argzdev
Copy link
Contributor

argzdev commented Mar 8, 2023

Thanks @tevjef, I was able to reproduce the same behavior now. I'll notify our engineers.

@chandmohd
Copy link

Hi @argzdev Any update on this ? ...
I'm trying to create foregroundService with new process and my service has some firebase usage .I'm getting error Default FirebaseApp is not initialized in this process com.example.dev:locationProcess.

@argzdev
Copy link
Contributor

argzdev commented Apr 17, 2023

Hi all, sorry for the radio silence. Our engineers are currently working on other issues at the moment. However, rest assured that this will be worked on as soon as possible. That said, I'd like to ask for help to kindly give an emoji thumbs up on the original author's post for tracking. This'll help us prioritize issues based on severity and demand. Thanks!

@ninh-huynh
Copy link

We also facing this issue when start a service in new process. The crash still happing in firebase BoM version com.google.firebase:firebase-bom:31.3.0. After investigating, the crash only happened if we used Firebase Perf in our app. We need to upgrade firebase-BoM because of this warning:

image

@SunnyBe
Copy link

SunnyBe commented Nov 21, 2023

Any work around for this issue? While we wait for the next update that hopefully fixes it

@tprochazka
Copy link

I even tried to downgrade to 4.3.15 and it still happening :-(

@nAkhmedov
Copy link

Downgrade to 4.3.15 helps to me

@KaranAndro
Copy link

KaranAndro commented Jan 8, 2024

@engineerStuardo Thank you. Its really helped. version : 4.3.4

@trinhdinhdungcntt
Copy link

classpath 'com.google.gms:google-services:4.3.8' is OK but classpath 'com.google.gms:google-services:4.4.0' crash

thanks!!!!!!!!!!

@ONVETI
Copy link

ONVETI commented Feb 18, 2024

classpath 'com.google.gms:google-services:4.3.8' is OK but classpath 'com.google.gms:google-services:4.4.1' crash

@ONVETI
Copy link

ONVETI commented Feb 18, 2024

classpath 'com.google.gms:google-services:4.3.8' is OK but classpath 'com.google.gms:google-services:4.4.0' crash

thank you bro!

@SunnyBe
Copy link

SunnyBe commented Feb 26, 2024

Hi @visumickey and @argzdev do we have any update on the fix.
I am still experiencing this issue and I tried the recommended fixes above and nothing works for me(unless downgrading dependencies, which is not what we want to do at the moment).

@vellrya
Copy link

vellrya commented Mar 4, 2024

The original problem was related to the :Metrica process (it is an analytics service of Yandex, the largest advertising provider in the CIS). In new versions of the library, the process has been renamed to :AppMetrica
The problem can be solved as follows:

class App : Application() {
    var mainProcess = false
    var metricaProcess = false
  
    override fun onCreate() {
        val processName = ProcessUtil.getProcessName(this)
        mainProcess = processName==packageName
        metricaProcess = processName.contains(":AppMetrica") || processName.contains(":Metrica")

        if (!metricaProcess) { // you do not need to initialise anything manually inside the Metrica process
            if (!mainProcess) {
                // if you created the service yourself in a separate process (via android:process=":Something"), you should manually initialise Firebase in that process.
                FirebaseApp.initializeApp(this)
            }
            
             // put old onCreate code here, e.g:
             // FirebaseCrashlytics.getInstance().setUserId("someId")
             // if (mainProcess) MobileAds.initialize(this) { } // you probably don't want ads in the background process
             // ...
             
        }
        super.onCreate()
    }
}

ProcessUtil.java:
https://gist.github.com/vellrya/6ab5d915f1002a8e1ae3205ba715f0d7

This method works with the newest versions of libraries, there is no need to downgrade dependency versions.

@mnk98
Copy link

mnk98 commented Mar 15, 2024

Yes, i just updated com.google.gms:google-services from 4.3.15 to 4.4.1 and it's started crashing for me for above issue. Added initialization in Activity as well as in APplication file, still crashes for me.
Line where it's breaking for above error, even i tried to initialize FirebaseApp before i am using that, still not fixed
FirebaseAuth.getInstance().currentUser

@HasanAlyazidi
Copy link

HasanAlyazidi commented Mar 21, 2024

I have tested the following versions of com.google.gms:google-services:
4.3.15: working ✅
4.4.0: not working ❌
4.4.1: not working ❌

Firebase (Flutter) versions:

firebase_core: ^2.27.1
firebase_auth: ^4.17.9
firebase_messaging: ^14.7.20

@b150132
Copy link

b150132 commented Apr 26, 2024

Hi, any progress on this issue? com.google.gms:google-services 4.4.1: not working

@ltrempe
Copy link

ltrempe commented Apr 29, 2024

I had this same issue due to an Activity being designated to a separate process. Downgrading versions did not work for me. However, manually initializing Firebase as outlined in #4599 resolved the issue.

  1. Disable the FirebaseInitProvider in the AndroidManifest
  2. Manually initialize Firebase in your Application via FirebaseApp.initializeApp(this, builder.build())

Unfortunately manually initializing Firebase apparently breaks _app_start tracing with BOM version 32.2.0 or later.

@ZhangZhizhen
Copy link

集成了下面的sdk 功能, firebase-crashlytics 统计到线上用户报错 Caused by java.lang.IllegalStateException
Default FirebaseApp is not initialized in this process com.moonjoy.cointalh. Make sure to call FirebaseApp.initializeApp(Context) first.

本地无法复现。请问是什么问题导致的?

classpath 'com.google.gms:google-services:4.3.15'

implementation 'com.google.firebase:firebase-crashlytics:18.6.4'
implementation 'com.google.firebase:firebase-analytics:21.6.2'
// Firebase Performance Monitoring 对勾
implementation ('com.google.firebase:firebase-perf:20.5.2'){
exclude group: 'com.squareup.okhttp3'
}
// Firebase messaging
implementation 'com.google.firebase:firebase-messaging:23.4.1'
// Firebase config
implementation 'com.google.firebase:firebase-config:21.6.3'

@ferbeab
Copy link

ferbeab commented Jul 11, 2024

Same here, upgrading to "com.google.gms:google-services:4.4.2" breaks my app. I will stay with "com.google.gms:google-services:4.3.10" until it gets resolved

@haavamoa
Copy link

Hi all, sorry for the radio silence. Our engineers are currently working on other issues at the moment. However, rest assured that this will be worked on as soon as possible. That said, I'd like to ask for help to kindly give an emoji thumbs up on the original author's post for tracking. This'll help us prioritize issues based on severity and demand. Thanks!

Any update on this @argzdev ? 😅😅

@williamdes
Copy link

Same here, upgrading to "com.google.gms:google-services:4.4.2" breaks my app. I will stay with "com.google.gms:google-services:4.3.10" until it gets resolved

Same for me

it seems that it could be due to some refactoring: google/play-services-plugins#269
News: https://github.com/google/play-services-plugins/tree/main/google-services-plugin#new-in-version-440

Place the google-services.json file for your project in the app/ directory.
Alternatively, you can use variant specific source-sets, for example: debug/google-services.json.

This did trigger the error Default FirebaseApp is not initialized in this process
Related issues:

@sachun78
Copy link

4.3.15는 괜찮은것 같습니다.

@varunam
Copy link

varunam commented Sep 10, 2024

Any progress on this issue? Even I am facing it after upgrading Firebase BoM to 33.2.0

@pratik-pattanaik
Copy link

Here's what the Javadoc from the FIrebaseApp.java class says :-

"Any FirebaseApp initialization must occur only in the main process of the app. Use of Firebase in processes other than the main process is not supported and will likely cause problems related to resource contention."

So, going by the maintainers' recommendation, we're not supposed to initialize Firebase in non-main processes. I did initalize Firebase in a non-main process in the Application onCreate() and it did stop the crash, but I'm not sticking to this solution because it may have side effects that may only be realized in production.

@ltrempe
Copy link

ltrempe commented Sep 15, 2024

If an exception is going to be thrown then we should be allowed to catch it. At the very least this should be failing gracefully. Analytics should not be crashing apps in production.

@EpariNigam
Copy link

Hi @visumickey and @argzdev do we have any update on the fix.

@ernazarsembayev
Copy link

Knock-knock-knock, is there any update?

@mrober
Copy link
Contributor

mrober commented Oct 10, 2024

@ernazarsembayev we landed a fix for this, it will go out in the next-next release due to timing.

mrober added a commit that referenced this issue Oct 11, 2024
…6371)

Fix IllegalStateException when starting a trace before Firebase
initializes.

This can happen if you manually initialize Firebase. See #4693
@mrober
Copy link
Contributor

mrober commented Nov 8, 2024

This fix was released in Perf version 21.0.2

@mrober mrober closed this as completed Nov 8, 2024
@firebase firebase locked and limited conversation to collaborators Dec 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests