Unity Firebase 中的 InitializationException

InitializationException in Unity Firebase

我的 Unity 项目中的 Firebase 有问题。项目中导入了Firebase SDK,构建,过程中没有报错。

SHA-1 密钥是使用密钥工具生成的,并添加到控制台中的 Firebase 项目中。

google-services.json 也被添加到资产文件夹中。

初始化 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");
}

所以应用程序可以构建和运行而不会崩溃。但是通过 adb logcat -s Unity 我可以看到以下内容:

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)

谷歌搜索任何这些消息都没有太大帮助。我错过了什么? 我逐步遵循了 Unity 的 Firebase 设置教程。这个错误已经吓了我一个星期了!

因此,在第 1245321653214 次尝试重新导入 Firebase 并解决此问题后,我们发现之前的另一个插件 Admob 覆盖了 Firebase 使用的一些库。

我们删除了 Firebase 和 Admob 可能使用的所有 aar 和 jars,并首先重新导入 Firebase,然后有选择地导入 Admob。 主要规则 不允许 Admob 覆盖 Firebase 使用的任何文件,例如 "play-services-blah-blah.aar"。

此 post 中的主要建议是不要急于导入插件:导入一个,配置,构建,检查它是否工作(10 次),提交,导入第二个等等。 . 这样你就可以清楚地看到3rd parties之间是否有confilcts。

如果你已经陷入困境,你最好删除所有 3rd 方并从头开始。

因此,请务必小心处理与下图类似的文件。

希望这有助于人们避免我们遇到的问题。

也许我的反馈对您也有帮助,我多年来一直遇到同样的错误(这会阻止 Firebase 远程配置工作),直到我发现我使用异步调用在同一帧上同时初始化了 Firebase Analytics 和远程配置,这似乎是问题所在。

一旦我用同步版本替换了两个初始化:

DependencyStatus dependencyStatus = FirebaseApp.CheckDependencies();
    if (dependencyStatus == DependencyStatus.Available)
        InitializeFirebase();
    else Debug.LogError("Remote Config Could not resolve all Firebase dependencies: " + dependencyStatus);

一切正常...

已解决 - 在 Unity 中,我在初始化时遇到了同样的错误。这里的主要区别是,我使用自己的 gradle 进行构建 (mainTemplate.gradle),而 Plugins/Android/Firebase/res/values.xml 中的值没有与 final values.xml

合并

解决方法: 编辑了我的 gradle 以将 Firebase/res 文件夹也包含到 res 文件夹中 - 在 android 块

下的 mainTemplate.gradle 中添加了以下行
    sourceSets {
        main {
                res.srcDirs += 'Firebase/res'
        }
}

调试步骤: 1. 已在 Plugins 文件夹中验证 google-services.xml 2. 构建完成后,验证Unity项目中Temp/文件夹内的值 3.发现,Temp/Firebase里面的值不在主生成的values.xml里面 4. 在 mainTemplate.gradle 中添加行以包含 Firebaes/res

我遇到了与上面评论中所述相同的问题。

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)

我只安装了这些模块:FirebaseAuth、FirebaseMessaging、FirebaseRemoteConfig、FirebaseAnalytics。

然后我安装了 FirebaseCrashlytics 模块,它解决了我的问题。