firebase / firebase-android-sdk

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

FirebaseApp is not initialized in this process #4693

Open hram opened 1 year ago

hram commented 1 year 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

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 commented 1 year ago

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

argzdev commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

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

argzdev commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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

chandmohd commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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
chubecode commented 1 year ago

How it's goin?

argzdev commented 1 year ago

Let me notify our engineers again and see if we can get some updates.

vineeshLee commented 1 year ago

iam also getting the same error

//app gradle implementation 'com.google.android.gms:play-services-maps:18.1.0' implementation 'com.google.firebase:firebase-crashlytics:18.3.7' implementation 'com.google.firebase:firebase-analytics:21.2.2'

//project level classpath 'com.google.gms:google-services:4.3.15' classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.5'

//Application class override fun onCreate() { super.onCreate() FirebaseApp.initializeApp(this) FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true)

}

iam able to open the app in debug mode but iam not able to open the release build ,when iam trying to open the release build iam getting the issue as mentioned below.

Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process . Make sure to call FirebaseApp.initializeApp(Context) first. at com.google.firebase.FirebaseApp.getInstance(FirebaseApp.java:179)

argzdev commented 1 year ago

Hi @vineeshLee, looking at your imports I don't see firebase-perf, so I'm not sure if you're experiencing the same issue here. Can you provide a full stacktrace so we can conduct an initial investigation?

ThrowJojo commented 1 year ago

Sorry to be a pain but are there any updates about this issue?

The only way I have found so far to circumvent it if you're using an SDK that has a remote process is by manually initialising Firebase and making sure it gets initialised before the SDK in question...

argzdev commented 1 year ago

Sorry for the radio silence here, @ThrowJojo. Our engineers are still working on this. We'll reply back here once we have further updates. Thanks!

abhishekrr commented 1 year ago

h @argzdev , are there any updates.

Tenkei commented 1 year ago

Let me share some new information on this issue:

visumickey commented 1 year ago

Thanks a lot for highlighting the issue. Similar to the reference mentioned above, this issue seem to be raising out of Firebase Performance/Remote Config using the Firebase executors instead of its own executors. We are working to reproduce/fix this issue at the earliest. Will keep this issue posted as and when we have updates.

Thanks for your patience as we work through this issue.

thisames commented 1 year ago

I have the same problem

nbcallum commented 1 year ago

any update?

thisames commented 1 year ago

putting 'com.google.gms.google-services' on plugins in app/build.gradle solved my problem

plugins {
    id 'com.google.gms.google-services'
}
abhishekrr commented 1 year ago

this is open from February, when can we expect it's resolution

zz446449676 commented 1 year ago

I have the same problem

engineerStuardo commented 1 year ago

So I was facing this problem and I'm working with React Native and in this case was integrating push notification for android with Braze and FCM. I did the following:

  1. Open Android Studio.
  2. Added apply plugin: 'com.google.gms.google-services' to app/build.gradle.
  3. Also added classpath 'com.google.gms:google-services:4.3.4' to build.gradle and clicked Sync Now and that fixed my issue.

Also in my case didn't have to add this FirebaseApp.initializeApp(this)... it worked with the above steps.

visumickey commented 1 year ago

After detailed analysis, here are our learnings on the issue:

Possibility 1: Firebase App can be initialized in multiple ways - Either through dependency on Google Services (using the plugin) or using configuration options. When using configuration options based initialization, typically this is done in the Application onCreate API. When using @AddTrace annotation for the Application's onCreate API, that would force referring to FirebaseApp's instance at the start of the onCreate function. Unfortunately that is going to cause an exception since FirebaseApp is not initialized in the beginning of the onCreate method. There is no cleaner way to fix this issue.

The recommendation in this case would be to refactor the implementation on the application's onCreate API into its own separate function and add @AddTrace annotation to that instead of the application's onCreate function. This should avoid the problem.

Possibility 2: Like the last comment above, you can depend on the GoogleServices which will initialize FirebaseApp before the Application's onCreate that can avoid the exception. That is exactly the reason why we did not have to do a separate FirebaseApp.initializeApp() in this approach.

But the caveat in this approach would be to avoid analytics dependency since that is not currently supported.

Note: We don't this is exactly because of the changes brought because of the Firebase Executor that has caused this issue. But changing to FirebaseExecutor has brought this needs to visibility (since FirebaseExecutor based approach avoids FirebaseApp initialization to later until necessary).

Please update the comment thread if the above approaches does not solve your problem.

abhishekrr commented 1 year ago

please note that this is occuring too when app has to startup a separate process , related to Firebase initialization on non main processes.

haavamoa commented 1 year ago

Same problem in .NET MAUI 😢

For MAUI I can confirm that adding Xamarin.GooglePlayServices.Analytics to your project (binding library for the above mentioned google services analytics package) work.

google-oss-bot commented 1 year ago

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!

hram commented 1 year ago

do not close it automatically

haavamoa commented 1 year ago

Hi, any progress on this issue?

vtabk2 commented 1 year ago

Thật đáng tiếc vì đến bây giờ sự cố vẫn chưa đc khắc phúc

visumickey commented 1 year ago

@hram Did the above suggestion work for you? Suggestions in this comment .

Paget96 commented 11 months ago

I have the same issue with my application that have foreground service as a separated process.

Without FirebaseApp.initializeApp() there are no initialization at all, adding this in onStartCommand() does the job, but only for service.

Adding FirebaseApp.initializeApp() into the application's onCreate make app crash when open for some users. Seems like for some reason firebase cannot be used in more than one process for some reason.

vtabk2 commented 11 months ago

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

leni8ec commented 11 months ago

In my case helped downgrade google-services from 4.4.0 to 4.3.15

gradle wrapper: 7.5.1 android gradle plugin: 7.3.1 crashlytics gradle plugin: 2.9.9 google-services: 4.3.15 (~4.4.0~) firebase bom: 32.5.0

bugsCreator commented 11 months ago

I faced same issue today.i tried everthing and at the end of the day it fixed🥴 . i'm adding build.gradle(project/module) here

Build.gradle(Project)

buildscript {
    dependencies {
        classpath 'com.google.gms:google-services:4.3.15'
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.3.0' apply false
    id 'com.android.library' version '7.3.0' apply false

}

Build.gradle(Module)

plugins {
    id 'com.android.application'
}

android {
    namespace 'tech.bugscreator.smartpay'
    compileSdk 34

    defaultConfig {
        applicationId "tech.bugscreator.smartpay"
        minSdk 24
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'com.google.android.material:material:1.10.0'
    implementation(platform("com.google.firebase:firebase-bom:32.5.0"))
    implementation("com.google.firebase:firebase-analytics")

    implementation("com.google.firebase:firebase-firestore")
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

}
apply plugin: 'com.google.gms.google-services'

i added google service dependency using classpath and in module level i apply plugin and it resolved my issue

Dishaha4379 commented 10 months ago

was facing issue with 'com.google.gms:google-services:4.4.0' but it solved when i changed it to 4.3.0 , Thanks to @engineerStuardo

SunnyBe commented 10 months ago

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

tprochazka commented 10 months ago

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

nAkhmedov commented 10 months ago

Downgrade to 4.3.15 helps to me

KaranAndro commented 9 months ago

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

trinhdinhdungcntt commented 8 months ago

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

thanks!!!!!!!!!!

ONVETI commented 7 months ago

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

ONVETI commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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.