Firebase Initialization Error in .NET MAUI: A Subtle JSON Format Issue

I recently found myself setting up push notifications for a .NET MAUI app on an Android emulator. All seemed to be going well until I hit an annoying roadblock:

Java.Lang.IllegalStateException: 'Default FirebaseApp is not initialized in this process com.companyname.maui. Make sure to call FirebaseApp.initializeApp(Context) first.'

This error threw me for a loop because everything seemed to be set up correctly. For reference, I’m using .NET 9, the latest NuGet packages, and I followed the documentation to the letter. Here’s a quick overview of what I had in place:

  • google-services.json file: Located in the correct directory (project/Platforms/Android) and set to the GoogleServicesJson build action, as required for MAUI projects.
  • No need to call InitializeApp manually: Firebase should handle this automatically when the JSON is properly configured.

And yet, despite my best efforts, the error persisted. I even tried calling FirebaseApp.initializeApp(Context) explicitly in the code, but it still didn’t work.

The Real Culprit: A Subtle Format Change

After banging my head against this for longer than I’d like to admit, I finally discovered the problem: my google-services.json file.

Now, let me explain. The file itself was in the correct location and set up properly, but it had been copied over from an older Xamarin.Forms project. At first glance, the contents looked fine. However, it turns out the format of the google-services.json file had changed slightly over the years, and the older format isn’t compatible with the latest packages in my MAUI project.

This means that if you’re migrating a project from Xamarin.Forms to MAUI, or using a JSON file from an older project, you might run into this exact issue without realizing it.

What Changed in the google-services.json File?

The differences in the file format weren’t obvious. On closer inspection, I noticed some structural changes in the newer versions of the file. Things like updated fields or slightly different key arrangements can throw off Firebase initialization if the file doesn’t meet its expected schema.

Unfortunately, there’s no easy way to debug this directly from the error message—it just complains that Firebase isn’t initialized without pointing to the file as the culprit.

The Fix

Once I suspected the file, the fix was straightforward:

  1. Go to your Firebase Console.
  2. Re-download the latest version of the google-services.json file for your project.
  3. Replace the old file with the new one in project/Platforms/Android.
  4. Make sure its build action is still set to GoogleServicesJson.

With the updated file in place, I rebuilt and ran the app. The error was gone, and push notifications worked as expected.

Lessons Learned

If you’re migrating a project from Xamarin.Forms to MAUI or using a pre-existing Firebase configuration, don’t assume that your old google-services.json file will work out of the box. Firebase’s configuration requirements can change subtly over time, and even small differences in the file format can lead to frustrating issues.

Always grab a fresh google-services.json file when setting up a new project or migrating to a new framework. It’s a quick step that could save you hours of troubleshooting.

That’s all sorted for now, but it was definitely a lesson in double-checking the finer details. Hopefully, this post saves someone else the headache I went through. If you’ve run into something similar or found other quirks in the Firebase + MAUI setup process, let me know in the comments!

Happy coding! 🎉

  • December 21, 2024