Praddyum Verma
Praddyum Verma

Reputation: 21

Unity integrated with firebase works on Unity Editor but not on mobile device

I have created a simple app on Unity 2020.3.0.f1 with integration of Firebase sdk 7.1.0. I am basically doing a simple read and write to firebase. It's working well in Unity editor but not in a mobile build although the apk build is 100% successful.

Operating System: Ubuntu 20.04(LTS).

Here is my simple code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Firebase.Database;
using TMPro;

public class firebase_script : MonoBehaviour
{
    DatabaseReference reference;
    string text_place;
    public TextMeshProUGUI text;

    // Start is called before the first frame update
    void Start()
    {
        reference= FirebaseDatabase.DefaultInstance.RootReference;
    }
    public void get_updates(){
        reference.Child("plant").GetValueAsync().ContinueWith(task => {
            if(task.IsFaulted){
                Debug.Log("Failed to fetch Value");
            }
            else if(task.IsCompleted){
                DataSnapshot snapshot =task.Result;
                text_place=snapshot.Child("moisture").Value.ToString();
                
            }
        });
    }

    // Update is called once per frame
    void Update()
    {
        text.text=text_place;   
    }
}

Edit: When I see the logcat I find that Firebase app initialisation fails but no such error can be seen while working in unity editor and the app worked fine in Unity editor but not in android device

03-23 00:45:45.253  9618  9707 I Unity   : Company Name: <Striped>
03-23 00:45:45.253  9618  9707 I Unity   : Product Name: <Striped>
03-23 00:45:48.719  9618  9707 E Unity   : InitializationException:  Firebase app creation failed.
03-23 00:45:48.719  9618  9707 E Unity   :   at Firebase.FirebaseApp.CreateAndTrack (Firebase.FirebaseApp+CreateDelegate createDelegate, Firebase.FirebaseApp existingProxy) [0x000e3] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.719  9618  9707 E Unity   :   at Firebase.FirebaseApp.Create () [0x00027] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.719  9618  9707 E Unity   :   at Firebase.FirebaseApp.get_DefaultInstance () [0x00017] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.719  9618  9707 E Unity   :   at Firebase.Database.FirebaseDatabase.get_DefaultInstance () [0x00000] in <265179cd3d324ba1be0c68f88dea310a>:0 
03-23 00:45:48.719  9618  9707 E Unity   :   at firebase_script.Start () [0x00000] in <a8ca21255e29484580e851c97ded26fa>:0 
03-23 00:45:48.719  9618  9707 E Unity   : 
03-23 00:45:48.739  9618  9707 E Unity   : InitializationException:  Firebase app creation failed.
03-23 00:45:48.739  9618  9707 E Unity   :   at Firebase.FirebaseApp.CreateAndTrack (Firebase.FirebaseApp+CreateDelegate createDelegate, Firebase.FirebaseApp existingProxy) [0x000e3] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.739  9618  9707 E Unity   :   at Firebase.FirebaseApp.Create () [0x00027] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.739  9618  9707 E Unity   :   at Firebase.FirebaseApp.get_DefaultInstance () [0x00017] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.739  9618  9707 E Unity   :   at Firebase.Database.FirebaseDatabase.get_DefaultInstance () [0x00000] in <265179cd3d324ba1be0c68f88dea310a>:0 
03-23 00:45:48.739  9618  9707 E Unity   :   at button_script.Start () [0x00000] in <a8ca21255e29484580e851c97ded26fa>:0 
03-23 00:45:48.739  9618  9707 E Unity   : 
03-23 00:45:48.759  9618  9707 E Unity   : InitializationException:  Firebase app creation failed.
03-23 00:45:48.759  9618  9707 E Unity   :   at Firebase.FirebaseApp.CreateAndTrack (Firebase.FirebaseApp+CreateDelegate createDelegate, Firebase.FirebaseApp existingProxy) [0x000e3] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.759  9618  9707 E Unity   :   at Firebase.FirebaseApp.Create () [0x00027] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.759  9618  9707 E Unity   :   at Firebase.FirebaseApp.get_DefaultInstance () [0x00017] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.759  9618  9707 E Unity   :   at Firebase.Database.FirebaseDatabase.get_DefaultInstance () [0x00000] in <265179cd3d324ba1be0c68f88dea310a>:0 
03-23 00:45:48.759  9618  9707 E Unity   :   at button_script.Start () [0x00000] in <a8ca21255e29484580e851c97ded26fa>:0 
03-23 00:45:48.759  9618  9707 E Unity   : 
03-23 00:45:48.830  9618  9707 E Unity   : Failed to read Firebase options from the app's resources. Either make sure google-services.json is included in your build or specify options explicitly.
03-23 00:45:48.830  9618  9707 E Unity   : 
03-23 00:45:48.830  9618  9707 E Unity   : Failed to read Firebase options from the app's resources. Either make sure google-services.json is included in your build or specify options explicitly.

Upvotes: 2

Views: 5803

Answers (3)

Saeed Rahbari
Saeed Rahbari

Reputation: 31

The Answer is here: Install both Android and ios files on your unity.

I will give you my story and the path I followed so you wouldn't make my mistakes:

I had the same problem and I literally tried everything everyone suggested. I was stuck at this for days with no solution. It was hell. Nothing worked!!! The first solution that kinda worked, was creating a custom Firebase App. But still, analytics didn't work. I was losing all hope and were thinking of going back to older versions of Firebase (someone mentioned this as the solution that worked for them) Until... I found a somewhat overlooked comment on Firebase github.

The content of this comment was this: Recent Firebase versions need both the android and ios files to be installed on the system. I thought to myself "Maybe this is the answer. The official Firebase documents and support team, tell us that it is not needed. But it's always giving me the error."

So I went ahead and downloaded the packs for ios. But I couldn't download them from Unity Hub. So I went to Unity Archives and downloaded the install file to my version (after mistakingly downloading a newer edition...) and only kept the check on ios files.

It started downloading and then there was this error: The correct version of Unity couldn't be found. So I started the installation again, but this time I gave the correct adress on my pc and after downloading the same thing over again, the option for ios build was unlocked.

Good... Now I only needed to at the Firebase configs again. I added them to the "StreamingAssets" folder as some people have suggested. Firebase tells you that you can upload them anywhere. But some people said this is the safe spot. (I'm not sure if it affects the results or not.)

And voila. the moment I copied it, a dialog opened: Firebase was asking for my permission on data gathering. And then another json for desktop was autocreated and a ray of hope shone in my heart. Could this be it?

I built the project right then... the first time I thought it didn't work. It was kind of heart breaking. But then, I noticed that IT IS WORKING!!! AT LAST! AFTER SO MANY TRIALS AND ERRORS! Everything was fine now.

This is a bug that recent editions of Firebase have and I've seen so many people are confused over it. Ever their own support team don't know the root of the problem. But this is the answer. I hope it helps others.

Upvotes: 0

Fancy Boy Epic
Fancy Boy Epic

Reputation: 91

I had the same issue with google-services.json not being loaded (Unity 2021.1.4f) and spent a looong time looking for answers and no suggested solutions I found worked. Finaly I tried calling Firebase.Create() manualy with manualy created AppOptions with values copied from google-services.json.

Firebase.AppOptions options = new Firebase.AppOptions();
options.ApiKey = "XXXXXXXXXXX";
options.AppId = "XXXXXXXXXXXX";
options.MessageSenderId = "XXXXXXXXXXXXX";
options.ProjectId = "my-app";
options.StorageBucket = "my-app.appspot.com";
    
var app = Firebase.FirebaseApp.Create( options );

Not an ideal solution but works till this bug gets fixed.

Upvotes: 4

Patrick Martin
Patrick Martin

Reputation: 3131

According to your log, you're missing the google-services.json file required to connect your game to your Firebase backend. There could be multiple reasons and multiple workarounds for this.

Simplest solution:

Make sure that somewhere in your Assets/ directory you have your google-services.json file. You can download that from the Firebase dashboard if you've forgotten to do so.

From this page:

Get config file for your Android app

Go to your Project settings in the Firebase console.

In the Your apps card, select the package name of the app for which you need a config file.

Click google-services.json.

Modified for Unity: Move your config file into the Assets/ directory of your game.

Make sure that you only have this most recent downloaded config file in your app.

google-services.json exists, I'm still getting the error

Make sure you have python installed and that you have an executable in your path actually named python rather than python3.

Normally, Android developers will use the play services gradle plugin to convert google-services.json into an xml (the process is detailed here). The Firebase Unity plugin still supports Unity back to 2017, which predates stable support for gradle in games (it used to support back to the 5 branch, but it's old enough that it's hard to find test machines to still run the Editor). Because of this, the Firebase Unity plugin instead runs a python script named generate_xml_from_google_services_json.py (compiled to generate_xml_from_google_services_json.exe on Windows), which generates the file Assets/Plugins/Android/FirebaseApp.androidlib/res/values/google-services.xml. This is usually what's missing when you see an error saying that you're missing a google-services.json file in the context of Unity.

This script is cross compatible with Python 3 and 2 (2 because that still ships in MacOS), but the tooling tries to execute it with python in your path. Since you're on Linux, you may want to try poking this bug: https://github.com/firebase/quickstart-unity/issues/1005 (mention @patm1987 in the comments if you think it should be opened and no one's responding -- remind me of this answer).

Neither of these are working

I'd recommend opening a bug here, but there is a workaround for all google-service.json related issues.

Note that I recommend against this as a first step, as this configuration isn't expected and may throw folks for a loop trying to help you in the future. It's also kind of fragile due to how Unity and Firebase interact.

When you call FirebaseApp.DefaultInstance or any other .DefaultInstance on a Firebase class, Firebase.Create() is implicitly lazily called if there is no FirebaseApp with the default app name. Because of a bug, you cannot override the default app once it's created (what I mean by this process being fragile) but if you make a call to .Create(AppOptions) before a default app is created (before any call to .DefaultInstance in any thread), you can provide your own app options.

You could then shove your google-services.json in the StreamingAssets/ directory and load it directly, or build an AppOptions by copy/pasting the fields out of your google-services.json.

Again, I recommend that this is your last resort rather than your first. And I'd ask that you file a bug if you have to do this.

Upvotes: 2

Related Questions