Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Unity Android Firebase Auth can't find google-services.json

Discussion in 'Scripting' started by ZitounesMinapps, Jul 2, 2023.

  1. ZitounesMinapps

    ZitounesMinapps

    Joined:
    Jul 31, 2019
    Posts:
    3
    I am trying to setup a Google sign-in auth in Unity 2022.3.1f1 for Android. I already have a firebase database set up and running fine.

    I installed these packages :
    • FirebaseAnalytics
    • FirebaseAuth
    • FirebaseDatabase
    from here : https://firebase.google.com/docs/auth/unity/google-signin and the Google Sign-In plugin from here : https://github.com/googlesamples/google-signin-unity/releases (1.0.4), so the latest versions of the packages.

    Questions : How can I get the google auth working ? Why isn't my google-services.json file found on my phone in Assets/StreamingAssets, Assets/, ... Why is the firebase (realtime) database working without the google-services.json but not the Auth ?

    Problems and what i have tried :

    My 1st problem was that the google-services.json (placed in the Assets folder) wasn't found in the editor or phone. I copied it and placed it in Assets/StreamingAssets, and was then found by the editor (making the firebase database work).

    My problem now is that the google-services.json is found in the editor but not on my phone. This is the log error :

    Code (CSharp):
    1. 07-01 11:49:38.349: E/Unity(21303): 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.
    So I explicitly specified the options like this :

    Code (CSharp):
    1. Firebase.AppOptions options = new Firebase.AppOptions();
    2.                 options.ApiKey = "SameApiKeyInGoogleServicesJson";
    3.                 options.AppId = "SameAppIdyInGoogleServicesJson";
    4.                 options.ProjectId = "SameProjectIdInGoogleServicesJson";
    5.                 options.StorageBucket = "SameStorageBucketInGoogleServicesJson";
    6.                 options.DatabaseUrl = new System.Uri("SameDatabaseUrlInGoogleServicesJson");
    7.  
    8.                 app = Firebase.FirebaseApp.Create(options);
    The database now works on my phone, the Sign-In page pops up but after choosing an account I get still get the error and more.

    The log :

    Code (CSharp):
    1. 07-01 12:19:41.006: I/Unity(28431): Google checked dependencies
    2. 07-01 12:19:41.006: I/Unity(28431): LoginManager:<Start>b__7_0(Task`1)
    3. 07-01 12:19:41.006: I/Unity(28431): System.Threading.Tasks.Task:Execute()
    4. 07-01 12:19:41.006: I/Unity(28431): System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback, Object, Boolean)
    5. 07-01 12:19:41.006: I/Unity(28431): System.Threading.Tasks.Task:ExecuteWithThreadLocal(Task&)
    6. 07-01 12:19:41.006: I/Unity(28431): System.Threading.Tasks.Task:ExecuteEntry(Boolean)
    7. 07-01 12:19:41.006: I/Unity(28431): System.Threading.ThreadPoolWorkQueue:Dispatch()
    8. 07-01 12:19:41.006: I/Unity(28431): Loading default instances
    9. 07-01 12:19:41.006: I/Unity(28431): LoginManager:<Start>b__7_0(Task`1)
    10. 07-01 12:19:41.006: I/Unity(28431): System.Threading.Tasks.Task:Execute()
    11. 07-01 12:19:41.006: I/Unity(28431): System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback, Object, Boolean)
    12. 07-01 12:19:41.006: I/Unity(28431): System.Threading.Tasks.Task:ExecuteWithThreadLocal(Task&)
    13. 07-01 12:19:41.006: I/Unity(28431): System.Threading.Tasks.Task:ExecuteEntry(Boolean)
    14. 07-01 12:19:41.006: I/Unity(28431): System.Threading.ThreadPoolWorkQueue:Dispatch()
    15. 07-01 12:19:41.109: E/Unity(28431): 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.
    16. 07-01 12:19:41.109: E/Unity(28431): Firebase.AppUtilPINVOKE:PollCallbacks()
    17. 07-01 12:19:41.109: E/Unity(28431): Firebase.AppUtil:PollCallbacks()
    18. 07-01 12:19:41.109: E/Unity(28431): Firebase.Platform.FirebaseHandler:Update()
    19. 07-01 12:19:41.186: I/Unity(28431): Loaded default instances
    20. 07-01 12:19:41.186: I/Unity(28431): LoginManager:<Start>b__7_0(Task`1)
    21. 07-01 12:19:41.186: I/Unity(28431): System.Threading.Tasks.Task:Execute()
    22. 07-01 12:19:41.186: I/Unity(28431): System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback, Object, Boolean)
    23. 07-01 12:19:41.186: I/Unity(28431): System.Threading.Tasks.Task:ExecuteWithThreadLocal(Task&)
    24. 07-01 12:19:41.186: I/Unity(28431): System.Threading.Tasks.Task:ExecuteEntry(Boolean)
    25. 07-01 12:19:41.186: I/Unity(28431): System.Threading.ThreadPoolWorkQueue:Dispatch()
    26. 07-01 12:19:41.220: I/Unity(28431): SAVED TO DATABASE
    27. 07-01 12:19:41.220: I/Unity(28431): LoginManager:<Start>b__7_0(Task`1)
    28. 07-01 12:19:41.220: I/Unity(28431): System.Threading.Tasks.Task:Execute()
    29. 07-01 12:19:41.220: I/Unity(28431): System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback, Object, Boolean)
    30. 07-01 12:19:41.220: I/Unity(28431): System.Threading.Tasks.Task:ExecuteWithThreadLocal(Task&)
    31. 07-01 12:19:41.220: I/Unity(28431): System.Threading.Tasks.Task:ExecuteEntry(Boolean)
    32. 07-01 12:19:41.220: I/Unity(28431): System.Threading.ThreadPoolWorkQueue:Dispatch()
    33. 07-01 12:19:41.221: I/Unity(28431): Signing in google
    34. 07-01 12:19:41.221: I/Unity(28431): LoginManager:GoogleSignInClick()
    35. 07-01 12:19:41.221: I/Unity(28431): System.Threading.Tasks.Task:Execute()
    36. 07-01 12:19:41.221: I/Unity(28431): System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback, Object, Boolean)
    37. 07-01 12:19:41.221: I/Unity(28431): System.Threading.Tasks.Task:ExecuteWithThreadLocal(Task&)
    38. 07-01 12:19:41.221: I/Unity(28431): System.Threading.Tasks.Task:ExecuteEntry(Boolean)
    39. 07-01 12:19:41.221: I/Unity(28431): System.Threading.ThreadPoolWorkQueue:Dispatch()
    40. 07-01 12:19:49.716: E/Unity(28431): g_methods_cached
    41. 07-01 12:19:49.716: E/Unity(28431): Firebase.AppUtilPINVOKE:PollCallbacks()
    42. 07-01 12:19:49.716: E/Unity(28431): Firebase.AppUtil:PollCallbacks()
    43. 07-01 12:19:49.716: E/Unity(28431): Firebase.Platform.FirebaseHandler:Update()
    First error :

    Code (CSharp):
    1. 07-01 12:19:41.109: E/Unity(28431): 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.
    2. 07-01 12:19:41.109: E/Unity(28431): Firebase.AppUtilPINVOKE:PollCallbacks()
    3. 07-01 12:19:41.109: E/Unity(28431): Firebase.AppUtil:PollCallbacks()
    4. 07-01 12:19:41.109: E/Unity(28431): Firebase.Platform.FirebaseHandler:Update()
    Second error :

    Code (CSharp):
    1. 07-01 12:19:49.716: E/Unity(28431): g_methods_cached
    2. 07-01 12:19:49.716: E/Unity(28431): Firebase.AppUtilPINVOKE:PollCallbacks()
    3. 07-01 12:19:49.716: E/Unity(28431): Firebase.AppUtil:PollCallbacks()
    4. 07-01 12:19:49.716: E/Unity(28431): Firebase.Platform.FirebaseHandler:Update()
    This is the code :

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using System.Threading.Tasks;
    4. using TMPro;
    5. using Firebase;
    6. using Google;
    7. using Firebase.Extensions;
    8. using Firebase.Database;
    9. using Firebase.Auth;
    10. using System;
    11.  
    12. public class LoginManager : MonoBehaviour
    13. {
    14.  
    15.     public TextMeshProUGUI text;
    16.  
    17.     FirebaseApp app;
    18.     Firebase.Auth.FirebaseAuth auth;
    19.     private GoogleSignInConfiguration configuration;
    20.     Firebase.Auth.FirebaseUser user;
    21.  
    22.     Statistics stats;
    23.     DatabaseReference mDatabaseRef;
    24.  
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.         configuration = new GoogleSignInConfiguration();
    30.         configuration.WebClientId = "SameInGoogleServicesJson";
    31.         configuration.RequestIdToken = true;
    32.         configuration.RequestEmail = true;
    33.         configuration.RequestProfile = true;
    34.  
    35.         FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
    36.             var dependencyStatus = task.Result;
    37.             if (dependencyStatus == Firebase.DependencyStatus.Available)
    38.             {
    39.                 // Create and hold a reference to your FirebaseApp,
    40.                 // where app is a Firebase.FirebaseApp property of your application class.
    41.                 Debug.Log("Google checked dependencies");
    42.                 Debug.Log("Loading default instances");
    43.  
    44.                 Firebase.AppOptions options = new Firebase.AppOptions();
    45.                 options.ApiKey = "SameApiKeyInGoogleServicesJson";
    46.                 options.AppId = "SameAppIdyInGoogleServicesJson";
    47.                 options.ProjectId = "SameProjectIdInGoogleServicesJson";
    48.                 options.StorageBucket = "SameStorageBucketInGoogleServicesJson";
    49.                 options.DatabaseUrl = new System.Uri("SameDatabaseUrlInGoogleServicesJson");
    50.  
    51.                 app = Firebase.FirebaseApp.Create(options);
    52.                 Debug.Log("Loaded default instances");
    53.  
    54.                 //TestDatabase
    55.                 mDatabaseRef = FirebaseDatabase.DefaultInstance.RootReference;
    56.                 writeNewStats("userId");
    57.  
    58.                 //Login
    59.                 GoogleSignInClick();
    60.  
    61.             }
    62.             else
    63.             {
    64.                 UnityEngine.Debug.LogError(System.String.Format(
    65.                   "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
    66.                 // Firebase Unity SDK is not safe to use here.
    67.             }
    68.         });
    69.  
    70.     }
    71.  
    72.     void GoogleSignInClick()
    73.     {
    74.         Debug.Log("Signing in google");
    75.  
    76.         GoogleSignIn.Configuration = configuration;
    77.         GoogleSignIn.Configuration.UseGameSignIn = false;
    78.         GoogleSignIn.Configuration.RequestIdToken = true;
    79.         GoogleSignIn.Configuration.RequestEmail = true;
    80.  
    81.         GoogleSignIn.DefaultInstance.SignIn().ContinueWith(OnGoogleAuthenticatedFinished);
    82.     }
    83.  
    84.     void OnGoogleAuthenticatedFinished(Task<GoogleSignInUser> task)
    85.     {
    86.  
    87.         if (task.IsFaulted)
    88.         {
    89.             Debug.LogError("Google Sign In : Fault");
    90.             Debug.Log(task.Exception.ToString());
    91.         }
    92.         else if (task.IsCanceled)
    93.         {
    94.             Debug.LogError("Google Sign In : Canceled");
    95.         }
    96.         else
    97.         {
    98.             Firebase.Auth.Credential credential = Firebase.Auth.GoogleAuthProvider.GetCredential(task.Result.IdToken, null);
    99.             Debug.LogError("Create credentials");
    100.  
    101.             auth.SignInWithCredentialAsync(credential).ContinueWithOnMainThread(task =>
    102.             {
    103.  
    104.                 if (task.IsCanceled)
    105.                 {
    106.                     Debug.LogError("SignInWithCredential was canceled");
    107.                     return;
    108.                 }
    109.  
    110.                 if (task.IsFaulted)
    111.                 {
    112.                     Debug.LogError("SignInWithCredential encountered an error " + task.Exception);
    113.                     return;
    114.                 }
    115.  
    116.                 user = auth.CurrentUser;
    117.                 Debug.Log("User name : " + user.DisplayName);
    118.                 Debug.Log("User email : " + user.Email);
    119.             });
    120.         }
    121.  
    122.     }
    123.  
    124.     private void writeNewStats(string userId)
    125.     {
    126.         stats = new Statistics();
    127.         string json = JsonUtility.ToJson(stats);
    128.  
    129.         mDatabaseRef.Child("users").Child(userId).SetRawJsonValueAsync(json);
    130.  
    131.         Debug.Log("SAVED TO DATABASE");
    132.     }
    133. }
    I tried renaming folders with .androidlib, changing the google-services.json location, deleting the packages and redownloading them making sure they don't overite other packages, implementing the code differently, but the best I found is what I have here.

    I hope I have given enough information, thanks for the future help !
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    While you provided a lot of info, the only thing I can tell you is that Google file should be in the assets folder. That's where I've always put it and not had an issue yet. Try re-downloading it if you haven't already. Make sure your authentication part is setup properly on Firebase as well. There are a few things you have to turn on if I remember.
     
  3. ZitounesMinapps

    ZitounesMinapps

    Joined:
    Jul 31, 2019
    Posts:
    3
    Hi, thanks for the reply, I redownloaded it, checked the ids, and key, nothing changed, and Google Sign-In seems to be setup as it should in firebase..
     
  4. ZitounesMinapps

    ZitounesMinapps

    Joined:
    Jul 31, 2019
    Posts:
    3
    Ok so I've managed to make it work but not with the latest updates. I exported an older project's packages
    (the folders :
    • Editor Default Resources
    • ExternalDependencyManager
    • Firebase
    • GoogleSignIn
    • Parse
    • Plugins
    )

    which used the v9.2.0 SDK (auth, app, database, analytics) and the same GoogleSign-In package v1.0.4 with the 2021.3.6f1 version of Unity.

    I put my google-services.json in StreamingAssets/ (But it was also found in Assets/) and changed manually the \Assets\Plugins\Android\FirebaseApp.androidlib\res\values\google-services.xml with the corresponding google-services.json values.

    I don't know what the problem was with the newer versions but this seems to work.
     
  5. erik196

    erik196

    Joined:
    Oct 12, 2020
    Posts:
    1
    Hi ZitounesMinapps,

    I'm facing this issue also. I'm using Unity 2021.3.18f1 and firebase SDK 11.3.0. Working fine in Editor but failed to work when build on Android phone, seems like related with google-services.json.

    You said you changed values in google-services.xml with corresponding in google-services.json, since I add SHA certificate fingerprints in Firebase Console, it didn't appear on google-services.xml.

    Did you add SHA certificate fingerprints in your Firebase Console?

    Thanks, Regards