unity app crashes, when signing in with google, using firebase

Viewed 1735

I am trying to add a 'sign in with google' option to my app, using firebase. However when I click the button which calls the sign in script on my android device, the app crashes and closes completely.

I cannot run this script in the editor. so is there another way of finding out what the error is?

I am using the script found here https://www.dropbox.com/s/qss2wk4v52d48j4/GoogleSignInDemo.cs?dl=0 and have taken the web client id from my firebase project, and enabled google sign in.

I have also added the sha1 fingerprint to my firebase project and have copied the google-services.json file to my unity project. I am using Unity 2020.1.6f1 My firebase packages are version 7.0.1 and the google sign in plugin 1.0.4

Would appreciate all help on fixing this issue and getting the sign in with google option to work.

I have found that my code breaks here:

 GoogleSignIn.DefaultInstance.SignIn().ContinueWith(OnAuthenticationFinished);

This is the OnAuthenticationFinished method:

internal void OnAuthenticationFinished(Task<GoogleSignInUser> task){}

I have left it empty to make sure the problem isn't inside the method

7 Answers

I think I see your problem. OnAuthenticationFinished is likely firing in the background, and AddToInformation is mutating something in the UnityEngine (namely updating infoText.text).

If you replace ContinueWith with ContinueWithOnMainThread, it may just work. I would recommend doing this to all instances of ContinueWith in your code for now. It may be slightly less efficient (you're doing work on your game thread), but it's also much safer in Unity. Otherwise I would need to see a log to figure out what the problem is. See this blog post for more info.

Was facing same issue and ContinueWithOnMain was not working for me either so I basically defined coroutines and made them wait for the end of frame before performing other instructions

here is the code I did

FirebaseAuthHandler.instance.GetAuth().SignInWithCredentialAsync(credential).ContinueWith(authTask =>
                {
                    if (authTask.IsCanceled)
                    {
                        signInCompleted.SetCanceled();
                        Debug.Log("Task canceled");
                    }
                    else if (authTask.IsFaulted)
                    {
                        signInCompleted.SetException(authTask.Exception);
                        StartCoroutine(OnFailure(authTask.Exception));
                    }
                    else
                    {
                        signInCompleted.SetResult(authTask.Result);                            
                        StartCoroutine(OnSuccess(authTask.Result));
                    }
                });

and the coroutines are as follow

    private IEnumerator OnSuccess(FirebaseUser authTaskResult)
    {
        yield return new WaitForEndOfFrame();
        Debug.Log("OnSuccess");
        // Do stuff here
    }

    private IEnumerator OnFailure(AggregateException authTaskException)
    {
        yield return new WaitForEndOfFrame();
        // Do stuff here
    }

In my case, it was an r8/proguard issue. I've added these lines to the proguard rules and it stopped crashing:

-keep class com.google.googlesignin.** { *; }
-keep class com.google.android.gms.auth.** { *; }

Hi I was using same script and had same issue. The reason behind this problem is "AddToInformation()" is mutating something in unity engine as Patrick said.
The simplest solution for this is to comment/remove "AddToInformation()" function from the script. Its just for debug purposes and wont affect functionality.

I am beginer C# and Unity. I belive OnAuthenticationFinished run in background thread (Difference with Unity Main thread). If you update UI or print log in OnAuthenticationFinished() cause the crash.

And I fix this issue by save value in OnAuthenticationFinished() to variable. And use them in Update()

private string idToken;
private bool errorMessage;

void OnAuthenticationFinished(Task<GoogleSignInUser> task)
{
  if (task.IsFaulted) {
   err = true;
   return;
  }
  idToken = task.Result.IdToken;
    
}


private void Update()
{
    if (!String.IsNullOrEmpty(idToken))
    {
        // Handle login successfully
        idToken = null;
        return;
    }

    if (err)
    {
        // Show err
      err = false;
    }
}

      

    Or you can use Corrountine() like Digvijaysinh Gohil.

For me, I had to use Firebase.Auth.FirebaseAuth.DefaultInstance in a line of code before signing in, otherwise, it would crash every time.

_ = Firebase.Auth.FirebaseAuth.DefaultInstance;

Firebase.Auth.Credential credential = Firebase.Auth.GoogleAuthProvider.GetCredential( [...] );
Related