Search Unity

Firebase Auth error on Android (worknig fine on Editor)

Discussion in 'Scripting' started by rubcc95, Sep 22, 2020.

  1. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Just
    I'm making a firebase made app with unity, just yesterday I tried to build it for first time.
    Everything works fine while testing on unity editor but when I build it (with no errors) Firebase.Auth.FirebaseAuth.DefaultInstance drop an error... an not really a specific exception, it drops exactly "Firebase app creation failed".

    I was using unity 2020.1.0f1 and Firebase Unity SDK 6.14.1

    I've tried to resolve packages, force resolve packages, rebuild the project... nothing work. After that I also tried upgrading; 1st Unity version to lastest one (2020.1.6f1) and after Firebase Unity SDK to 6.15.2. This didn't work too.

    Seems like this guys had the same issue:
    https://github.com/firebase/quickstart-unity/issues/709
    https://github.com/firebase/quickstart-unity/issues/229

    After following their threads I wasn't able to fix it. I'm testing today FirebaseAuth quickStart example but still there's the same issue. It works fine on editor, it builds fine... but it doesn't work also at phone.

    Any ideas about how to fix it? I've so pleased because at this point I'm completely lost, if you need any documentation/logcat in order to understand whats happening just ask for it, I'll be next to this thread all the day cause I've so much work at my pc today, so probably you'll get an answer fast.

    Thanks in advice.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Editor and mobile are completely different in their behavior.

    It would be helpful to see your code, but the basics I can share is make sure you have the Firebase json file in the project and you need to call

    Firebase.Firebaseapp.Create()

    in Awake at least to connect to the default Firebase project.

    Since it's saying it failed it just sounds like either you have the wrong json or missing it or it wasn't able to connect to the project on Firebase itself. Do you have everything set up correctly?

    But, show some code, copy the exact error and I might be able to point you in the right direction. And yeah, do use the latest Firebase sdk which it sounds like you already updated to. One of the previous versions had a pretty nasty bug in it.
     
    rubcc95 likes this.
  3. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Thank you so much. Just give me a few mins cause I'm not using the Create method anywhere and I want to test it, and I'm sharing the code.
     
  4. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    This is the InitializeFirebase function I call at awake:

    Code (CSharp):
    1.     void InitializeFirebase()
    2.     {
    3.         ***auth = FirebaseAuth.DefaultInstance;***
    4.         auth.StateChanged += AuthStateChanged;
    5.         AuthStateChanged(this, null);
    6.     }
    Well, exactly I call this to ensure net is right and check if firebase is ok... and CheckDependencies calls InitializeFirebase

    Code (CSharp):
    1.     void CheckDependencies()
    2.     {
    3.         Debug.Log("Start FireBase");
    4.         try
    5.         {
    6.             dependencyStatus = FirebaseApp.CheckDependencies();
    7.         }
    8.         catch (Exception e)
    9.         {
    10.             DLog.Instance.Print(e.Message);
    11.         }
    12.  
    13.         if (dependencyStatus != DependencyStatus.Available)
    14.         {
    15.             FirebaseApp.FixDependenciesAsync().ContinueWith(task =>
    16.             {
    17.                 dependencyStatus = FirebaseApp.CheckDependencies();
    18.                 if (dependencyStatus == DependencyStatus.Available)
    19.                 {
    20.                     InitializeFirebase();
    21.                 }
    22.                 else Debug.LogError("Could not resolve all Firebase dependencies: " + dependencyStatus);
    23.             });
    24.         }
    25.         else
    26.         {
    27.             InitializeFirebase();
    28.         }
    29.     }
    But in order to simplify and test faster I removed it assuming Firebase is ok and my net too. To call directly InitializeFirebase() works on editor.

    On android, code breaks at the line between ****** throwing the exception 'Firebase app creation failed'.
    If I catch this error, next line breaks cause auth is not an instance of an object...

    Btw if i Call Firebase.Firebaseapp.Create() before auth = FirebaseAuth.DefaultInstance it drops exactly the same error 'Firebase app creation failed' on Android, working still fine during editor mode.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Ok, I have two apps using Firebase differently, so they have a couple differences.

    In my splash screen I'm essentially doing...

    Code (CSharp):
    1.         var dependencyResult = await FirebaseApp.CheckAndFixDependenciesAsync();
    2.         if(dependencyResult == DependencyStatus.Available)
    3.         {
    4.             app = FirebaseApp.DefaultInstance;
    5.             OnFirebaseInitialized.Invoke(); //This simply loads the next scene.
    6.         }
    7.  
    Once on the next scene, I do this in Start

    Code (CSharp):
    1.         auth = FirebaseAuth.DefaultInstance;
    2.         auth.StateChanged += AuthStateChanged;
    3.  
    We're very similar, with just the slight difference being the dependency code.
    Go ahead and remove the Create call. I'm using that in a second app that accesses the first apps firestore, so it serves a different purpose. I thought I recalled using it in the main app as well.
     
    rubcc95 likes this.
  6. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Well, ty for your help Brathnann.
    But it seems like I'm still stuck since we do similar things, but mine one is not working :(
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    rubcc95 likes this.
  8. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    I saw both links before, btw my issue is the same than github 709 guy, i followed all they said before but didn't work...
    Are you using Dotnet3 or Dotnet4?
    Im at Dotnet4 now, just gonna swap to Dotnet3 as my last try, I hope it works.
    Ty for your time Brathnann
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I'm using .net 4

    I'm in unity 2019.4.8f currently on this project.
    I have the following packages installed.
    -Cloud Firestore
    -Cloud Functions
    -Firebase app (core)
    -Firebase authentication

    Hope that helps.
     
    rubcc95 likes this.
  10. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Uuuu and what package is Firebase app (core) ? I never installed it and enything called core seems to be important.
    In my zip I just have:
    -Analytics
    -Auth
    -Crashlytics
    -Database
    -DynamicLinks
    -Firestore
    -Functions
    -InstanceId
    -Invites
    -Messaging
    -RemoteConfig
    -Storage

    And I'm currently using only FirebaseAuth and FirebaseDatabase
     
  11. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Core as described in the package manager description "Core library every Firebase package depends on". So I'm guessing you will need it.

    You said zip, so I'm assuming you didn't install Firebase through the Unity package manager?

    https://firebase.google.com/docs/unity/setup

    I would look at the Unity Package manager setup section.

    upload_2020-9-23_10-51-39.png
     
    rubcc95 likes this.
  12. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Lol i have noting. I just clicked the .unitypackage file and Unity did the rest.
     
  13. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Sounds like you installed Firebase the old way. I remember doing that a long time ago. I would look into upgrading to the new method with the Package Manager. I'm guessing yours isn't working because of changes they did to their sdk.
     
    rubcc95 likes this.
  14. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    I just followed this vid for install it

    I'm not able to found how to install it thourgh package manager...
    Sry, I'm feeling so noob :(
     
  15. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    rubcc95 likes this.
  16. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Step 4 talks about google-services.json, I had it ready at assets folder since the start.
    Anyway.... It's solved. Nothing about how it was installed. In Unity 2020, all Android resources must be in directories with a .androidlib suffix.
    I just had to rename a folder:
    Assests/Plugins/Android/Firebase to Assests/Plugins/Android/Firebase.androidlib

    I also had to remove at AndroidManifest.xml at this folder the minsdk version.

    Code (xml):
    1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    2.           package="com.google.firebase.unity"
    3.           android:versionCode="1"
    4.           android:versionName="1.0">
    5.     <uses-sdk android:minSdkVersion="14"/> <!--- remove this line!-->
    6. </manifest>
    And at end it works! Thanks you for your patience Brathnann! Best wishes.
     
    UNSEENWorks likes this.
  17. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    upload_2020-9-23_15-12-5.png

    Step 4...using Unity Package manager.

    Also, renaming the folder was in one of the previous links I provided you, which you said you had read, so I assumed you had already done that...lol.

    Glad you have it working though. Good luck.
     
  18. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Oh, I'm spanish and the link you provided opens at my navigator as the spanish version. It seems like is not just a translate of the english page, my 4th point talk about google-services.json and googleServices-Info.plist for iOS. And I read all the link btw, but nothing about the rename folder.
     
  19. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Ah, it might have been another translation error.

    upload_2020-9-23_19-42-46.png

    This was in the link about missing Firebase config file.
     
  20. PedroGomas

    PedroGomas

    Joined:
    May 11, 2017
    Posts:
    3
    Hi , solved my problem with a simple thing, instead use a variable "DatabaseReference reference" and next equals FirebaseDatabase.DefaultInstance.RootReference, use directly in the code to save data like this - >


    FirebaseDatabase.DefaultInstance.RootReference.Child("Users").Child("user1").Child("email").SetValueAsync(emailRegisterField.text.ToString()).ContinueWith(task =>
    {
    if (task.IsCompleted)
    {
    Debug.Log("SUCESSO");

    }
    else
    {
    Debug.Log("já foste");
    }
    }