0
0

More than 3 years have passed since last update.

Unity 2020で作ったAndroidアプリからFireBaseに繋ぐ時の苦労したからまとめといた

Posted at

この記事で解決できること

Unityでアンドロイドアプリを作成していて、Google Cloud Platformに
接続する時に何故かUnity2020では送信できなくって困った。
その原因と解決策をまとめているので、一助になれば幸いっす。

事象

環境

MacBook Pro (13-inch, 2019, Two Thunderbolt 3 ports)
OS:BigSur 11.2.3
Unity
version:2020.3.7f1
target Platform:Android
target SDK:22
FireBase
version 6.1.1

問題が起こったストーリー

今まで、Unity2019(1.5f)を中心に開発をしていたが、新規アプリ開発するにあたり、Unity2020(3.7f)をPJとして採用し、開発をスタート。
アプリ開発終盤に、Google Analyticsで利用ログを簡単に分析したいとの要件に対応すべく、他PJと同じように実装した。
1. FireBaseSDKとGoogle-Service.jsonをDLしてきて
2. Importして
3. 必要な実装をして、
4. Buildして
5. 実機に乗せたら
6. LogがFireBaseに飛ばない

logcatしてみるとこんなエラーを吐いていた。

Firebase App initializing app jp.co.appname (default 1).
strlen(app_options->app_id()) && strlen(app_options->api_key())
Failed to set AppOption: ApiKey must be set.
Could not initialize Firebase App Options: ApplicationId must be set.
new_app
InitializationException:  Firebase app creation failed.
   at Firebase.FirebaseApp.CreateAndTrack (Firebase.FirebaseApp+CreateDelegate createDelegate, Firebase.FirebaseApp existingProxy) [0x000e3] in <5ef67111bf8740dfbdce3ea0f81c0968>:0
   at Firebase.FirebaseApp.Create () [0x00027] in <5ef67111bf8740dfbdce3ea0f81c0968>:0
   at Firebase.FirebaseApp.get_DefaultInstance () [0x00017] in <5ef67111bf8740dfbdce3ea0f81c0968>:0
   at Firebase.Analytics.FirebaseAnalytics..cctor () [0x00000] in <9dfcce3de97b43bcb1b4b7521e9b1f07>:0
    Rethrow as TypeInitializationException: The type initializer for 'Firebase.Analytics.FirebaseAnalytics' threw an exception.```

errorを読むに
・ApplicationIdがセットされていない
・故にFireBase appが初期設定ができない
とのこと。これは困った。

困ったので調べたこと

ひとまず、Firebase InitializationException unityとかで
ggってみたが、意外と記事が出てこない(そりゃそうさ、最新のUnityのversionを使っているんだから)

調査を進めていく中で、Google-Service.jsonを元に生成したGoogle-Service.xmlが読み込まれていないことに気づく。
今度は、Build時にちゃんとXMLを読み込んでもらえるようにgradleに記載。

mainTemplate.gradle
android {
    sourceSets {
        main {
            def unityProjectPath = $/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/")
            res.srcDirs += (unityProjectPath +  '/Assets/Plugins/Android/Firebase/res/values/google-services.xml')
        }
    }
}

これを入れたあと、普通にBuildしたら、無事FirebaseからLogが確認できました。
流行り物に飛びつくのはやめよう。と思いましたとさ。

参考記事

公式のチュートリアルの中にIssueがたってました
https://github.com/firebase/quickstart-unity/issues/677

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0