问题
I have a problem with Firebase in my Unity project. Firebase SDK was imported in the project, builded, no errors during this process.
SHA-1 key was generated with a keytool and added to Firebase project in the console.
google-services.json was also added to the Assets folder.
Simple script to initialize Firebase:
DependencyStatus dependencyStatus = DependencyStatus.UnavailableOther;
// Use this for initialization
void Start ()
{
Debug.Log ("Start FireBase");
dependencyStatus = FirebaseApp.CheckDependencies();
if (dependencyStatus != DependencyStatus.Available)
{
FirebaseApp.FixDependenciesAsync().ContinueWith(task =>
{
dependencyStatus = FirebaseApp.CheckDependencies();
if (dependencyStatus == DependencyStatus.Available)
{
InitializeFirebase();
}
else
{
Debug.LogError("Could not resolve all Firebase dependencies: " + dependencyStatus);
}
});
}
else
{
InitializeFirebase();
}
}
void InitializeFirebase()
{
FirebaseAnalytic.Instance().setAnalyticsCollectionEnabled(true);
FirebaseAnalytic.Instance().setUserProperty(FirebaseAnalytics.UserPropertySignUpMethod, "Google");
FirebaseAnalytic.Instance().setUserId(SystemInfo.deviceUniqueIdentifier);
FirebaseAnalytic.Instance().logEvent("LogIn", FirebaseAnalytics.EventLogin);
Debug.Log ("FirebaseAnalytics Logined");
}
So app builds and runs without crashes. But through adb logcat -s Unity
I can see the following:
I/Unity (27030): Start FireBase
I/Unity (27030): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
I/Unity (27030):
I/Unity (27030): Firebase App initializing app com.boldstatementproductions.mcpro (default 1).
I/Unity (27030): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
I/Unity (27030):
W/Unity (27030): Callback module already shut down
W/Unity (27030): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
E/Unity (27030): java_app
E/Unity (27030): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
I/Unity (27030): Firebase App initializing app com.boldstatementproductions.mcpro (default 1).
I/Unity (27030): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
W/Unity (27030): Callback module already shut down
W/Unity (27030): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
E/Unity (27030): java_app
E/Unity (27030): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
I/Unity (27030): InitializationException: Failed to initialize the default Firebase App.
I/Unity (27030): at Firebase.FirebaseApp.CreateAndTrack (Firebase.CreateDelegate createDelegate) [0x00000] in <filename unknown>:0
I/Unity (27030): at Firebase.FirebaseApp.Create () [0x00000] in <filename unknown>:0
I/Unity (27030): at Firebase.FirebaseApp.get_DefaultInstance () [0x00000] in <filename unknown>:0
I/Unity (27030): at Firebase.Analytics.FirebaseAnalytics..cctor () [0x00000] in <filename unknown>:0
I/Unity (27030): Rethrow as TypeInitializationException: An exception was thrown by the type initializer for Firebase.Analytics.FirebaseAnalytics
I/Unity (27030): at FirebaseDependencyResolver.InitializeFirebase () [0x00000] in <filename unknown>:0
I/Unity (27030): at FirebaseDependencyResolver.Start () [0x00000] in <filename unknown>:0
I/Unity (27030): (Filename: Line: -1)
Googling any of those messages didn't help very much. What am I missing? I followed Firebase setup tutorial for Unity step-by-step. This error terrorised me for a week already!
回答1:
So, after 1245321653214th attempt to reimport Firebase and to resolve this issue anyhow we discovered that previously the other plugin, Admob, overwrote some libraries that Firebase uses.
We removed all aars and jars both Firebase and Admob may use and firstly reimported Firebase and then, selectively, Admob. The main rule is not to allow Admob overwrite any files Firebase uses, for example "play-services-blah-blah.aar".
Main piece of advice in this post is not to hurry with importing plugins: import one, configure, build, check if it works (10 times), commit, import the seconds one and so on.. This way you'll clearly see if 3rd parties have confilcts between them.
If you are already in the mess, you should better delete all 3rd parties and start from scratch.
So, be VEEERY careful with files similar to those on the image below.
Hope this helps people to prevent problems we had.
回答2:
Maybe my feedback can help you too, I had this same error for ages (which was blocking Firebase Remote config from working) until I figured I had both Firebase Analytics and Remote Config initializing on the same frame using the async call, and that seem to be the issue.
Once I replaced both initializations by the sync version:
DependencyStatus dependencyStatus = FirebaseApp.CheckDependencies();
if (dependencyStatus == DependencyStatus.Available)
InitializeFirebase();
else Debug.LogError("Remote Config Could not resolve all Firebase dependencies: " + dependencyStatus);
it all worked...
回答3:
Resolved - I was getting the same error on initialisation, in Unity. Key difference here was, I was using my own gradle for build (mainTemplate.gradle), and the values inside Plugins/Android/Firebase/res/values.xml was not getting merged with final values.xml
Solution: Edited my gradle to include Firebase/res folder also to the res folders - Added following lines in mainTemplate.gradle under android block
sourceSets {
main {
res.srcDirs += 'Firebase/res'
}
}
Debugging Steps: 1. Verified google-services.xml inside Plugins folder 2. After building, verified the values inside Temp/ folder in Unity project 3. Found, the values inside Temp/Firebase is not in the main generated values.xml 4. Added line in mainTemplate.gradle to include Firebaes/res
回答4:
I was having the same issues as described in my comment above.
Have a fully working project with unity 2018.3,8f1 and firebase 5.4.4. Upgraded unity to 2019.2.6f1 which forced me to also upgrade firebase. I upgraded without too many issues, but then get this same error. Tried all of the solutions here, but get no results. I assume this is due to some conflict with other plugins as well, but can't figure out which ones. (also firebase was the last one installed, so I would expect it to overwrite any other issues)
I only had these modules installed: FirebaseAuth, FirebaseMessaging, FirebaseRemoteConfig, FirebaseAnalytics.
I then installed the FirebaseCrashlytics module, and it fixed my issue.
来源:https://stackoverflow.com/questions/43677353/initializationexception-in-unity-firebase