Search code examples
firebaseunity-game-enginegoogle-signin

Unity/Firebase Google Sign In Force Close On Android


So, I tried making a Google Sign In Option on my Unity Apps by mixing it with Firebase. So, i import the google signin plugin v1.04 and write the code. While building it to *.apk file nothing error show up on the console. But, when i try to login it force close after i chose which account i'm gonna use to login. Here's my full code:

using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Google;
using UnityEngine;
using Firebase.Auth;

public class googleAuth : MonoBehaviour
{
    [SerializeField] string webClientID = "190747548795-haa9ej8fkg584c1r5nkhu7u6ol50tmcf.apps.googleusercontent.com";
    GoogleSignInConfiguration configuration;
    private void Awake() {
        configuration = new GoogleSignInConfiguration{
        WebClientId = webClientID,
            RequestIdToken = true
        };
    }

    public void loginGoogle(){
      GoogleSignIn.Configuration = configuration;
      GoogleSignIn.Configuration.UseGameSignIn = false;
      GoogleSignIn.Configuration.RequestIdToken = true;
      GoogleSignIn.DefaultInstance.SignIn().ContinueWith(OnAuthenticationFinished);
    }
    void OnAuthenticationFinished(Task<GoogleSignInUser> task){
        if(task.IsCanceled)
            print("error");
        if(task.IsFaulted)
            print("error");
        if(task.IsCompleted){
            googleFirebase(task.Result.IdToken);
        }
    }
    void googleFirebase(string token){
        Credential credential = Firebase.Auth.GoogleAuthProvider.GetCredential(token,null);
        FirebaseAuth.DefaultInstance.SignInWithCredentialAsync(credential);
    }
}

Solution

  • It means you are missing files in the Plugins/Android/ folder.

    1. Create an APP on Firebase console.
    2. Authentication-> Sign-In Method -> Google Enable.
    3. Download the "google-services.json" file.
    4. Put in Assets/ folder then you can Import Plugin.
    5. After importing Jar resolver will resolve the required files and create Folder for firebase in Plugins/Android.
    6. Then you need to check your bundle ID in Build Settings and run the application. I think you need to check step 5 for your issue based on code you have correctly created the app.