Zenn
Open11

複数Flavorに対応したFlutterにFirebaseを導入する

manabiyamanabiya

iOSシミュレータでアプリを実行すると以下のエラーとなる

Error (Xcode): Unable to load contents of file list: '/Target Support Files/Pods-Runner/Pods-Runner-frameworks-Debug-development-input-files.xcfilelist'

色々した挙句、Flavor設定後かつFirebaseの設定をしていないコミットまで戻り以下を実行
https://github.com/AngeloAvv/flutter_flavorizr/blob/master/doc%2Ftroubleshooting%2Funable-to-load-contents-of-file-list%2FREADME.md
PodfileのFlavor設定、iosのバージョン指定をもとに戻して実行。
上記のエラーは消えたが、
Waiting for VM Service port to be available...から動かなくなる

manabiyamanabiya

また同様のエラーが発生し、上記では解消できなかった。

cd ios
pod install

で以下のエラーが出たので

[!] CocoaPods did not set the base configuration of your project because your project already has a custom config set.

このエラーから下記のIssueに辿り着き、ios/Flutter/Debug.xcconfigios/Flutter/Release.xcconfigの内容を書き換えることで問題が解消した。

例)ios/Flutter/Debug.xcconfig

- #include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
+ #include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug-development.xcconfig"
+ #include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug-production.xcconfig"
#include "Generated.xcconfig"

https://github.com/flutter/flutter/issues/66222#issuecomment-718040317

manabiyamanabiya

iOSシミュレータの再起動で解消。

Flavor対応のため作成したios/configをXcodeにドラッグ&ドロップして、アプリを実行すると別のエラーが発生。

Error (Xcode): Multiple commands produce '.../build/ios/Debug-development-iphonesimulator/Runner.app/GoogleService-Info.plist'

manabiyamanabiya

⇧ config追加時はデフォルト設定。xcodeに追加するとbuildphaseにbundle対象のファイルとして追加されるようだ。
flutterfireで構成しているので、FlutterFire: "flutterfire bundle-service-file"でバンドルするので削除が不安だったが問題なさそうだ

manabiyamanabiya

Google Sign-Inを実装する

Supabaseをバックエンドとしているため、以下の記事を参考に進める
https://supabase.com/docs/guides/auth/social-login/auth-google?queryGroups=platform&platform=flutter

  1. Firebaseのログイン方法にあるGoogleプロバイダを有効にする。
    • Androidは、証明書のフィンガープリントを追加する。注意点として、リリースしたアプリの署名はPlay署名になるので、別途フィンガープリントの登録が必要。

Androidフィンガープリント取得方法
https://developers.google.com/android/guides/client-auth?hl=ja#using_gradles_signing_report

manabiyamanabiya

上記設定後、Flutterfireによる設定ファイルの更新を行おうとすると以下のエラーが発生。

`PBXGroup` attempted to initialize an object with unknown ISA `PBXFileSystemSynchronizedRootGroup` from attributes

configをXcodeに追加する際に、Folderとして追加したのが問題だったようだ。
Groupに変換したところ上記エラーは解消した
https://github.com/CocoaPods/CocoaPods/issues/12456#issuecomment-2402736983

manabiyamanabiya
  1. iOSは設定ファイルを更新する

https://pub.dev/packages/google_sign_in_ios#ios-integration

手順6のCFBundleURLTypesについて。
以前作成したアプリでは、arrayに開発、本番のを入れてしまっていたが、ユーザー定義変数を利用するのが良いかもしれない。今回はこちらを採用する。

https://github.com/flutter/flutter/issues/24614#issuecomment-581769842

スキップした手順(GIDClientID, GIDServerClientID)についてもユーザー定義変数を利用することにした。
Dartのファイル内でiOSだけの対応となる以下の実装をすることを避けるため。

final GoogleSignIn googleSignIn = GoogleSignIn(
  // The OAuth client id of your app. This is required.
  clientId: 'Your Client ID',
  // If you need to authenticate to a backend server, specify its OAuth client. This is optional.
  serverClientId: 'Your Server ID',
);

追記、そもそも6だけで良いので4, 5の手順はスキップする

manabiyamanabiya

Google Cloudのプロジェクトに生成されているWeb Client IDをSupabaseのAuth ProviderでGoogleを有効にする際にClinet IDsに登録する。
iOSも利用するのでSkip nonce checkを有効にする

https://youtu.be/utMg6fVmX0U

コードでもwebclientidとiosclientidをセットしないとエラーになる

manabiyamanabiya

Androidのフレーバー作成で、マニフェストに以下のように動的な値を設定していたが、

    <application
-        android:label="@string/app_name"
+       android:label="${appLabel}"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">

のようにしても良かった。
後から見返したときに、リソースファイルを探してしまったがないどこで定義されているのかと迷ってしまった。
ビルド変数として定義することで、ビルドファイルで定義されているとすぐわかるから良いか

https://developer.android.com/build/manage-manifests?hl=ja#inject_build_variables_into_the_manifest

ログインするとコメントできます