Open10

最新のflutterfire_cliを使ってみる

noboru_inoboru_i

sample-flutterfire-20220709 というFirebase projectを作成してみる。
GAも紐付けておく。

noboru_inoboru_i

Flutter projectを作成する。

flutter create --project-name sample_flutterfire_20220709 --org dev.flutter.my_app --template=app --platforms=ios,android .
noboru_inoboru_i

環境情報

> flutter --version
Flutter 3.0.4 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 85684f9300 (8 days ago)2022-06-30 13:22:47 -0700
Engine • revision 6ba2af10bb
Tools • Dart 2.17.5 • DevTools 2.12.2
> firebase --version
11.2.1
> flutterfire --version
0.2.2+2
noboru_inoboru_i

https://firebase.google.com/docs/flutter/setup?platform=ios#initialize-firebase
に従ってすすめる。

flutter pub add firebase_core

によって、firebase_coreがpubspec.ymlに追加される。

flutterfire configure を実行

flutterfire configure --project sample-flutterfire-20220709 \
--platforms android,ios \
--android-app-id dev.flutter.my_app.sample_flutterfire_20220709 \
--ios-bundle-id dev.flutter.myapp.sampleFlutterfire20220709 \
--out lib/gen/firebase_options.dart

Firebase project上に、AndroidとiOSのアプリが作成される。

lib/main.dart を以下のように書き換える。

diff --git a/lib/main.dart b/lib/main.dart
index 202509b..12c1432 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -1,6 +1,13 @@
 import 'package:flutter/material.dart';
+import 'package:firebase_core/firebase_core.dart';
+import 'package:sample_flutterfire_20220709/gen/firebase_options.dart';
+
+Future<void> main() async {
+  WidgetsFlutterBinding.ensureInitialized();
+  await Firebase.initializeApp(
+    options: DefaultFirebaseOptions.currentPlatform,
+  );
 
-void main() {
   runApp(const MyApp());
 }

VSCode上より、main.dartを実行すると、iOS向けのファイルがさらに作成される。
https://github.com/noboru-i/sample_flutterfire/commit/0f91ba84642e8d5d2f9f5244305cca6966f51fb0

noboru_inoboru_i

本編

https://github.com/noboru-i/sample_flutterfire/commit/85fd2313c8a81314ee7bcd90d108f858fe0ec4bc
これが、 flutterfire configure を実行した際に、自動で変更された差分。

ファイルとしては、以下の通り。

M     android/app/build.gradle
A     android/app/google-services.json
M     android/build.gradle
A     ios/firebase_app_id_file.json
A     lib/gen/firebase_options.dart

また、iOSアプリをビルドした際には、以下の変更が発生した。
https://github.com/noboru-i/sample_flutterfire/commit/0f91ba84642e8d5d2f9f5244305cca6966f51fb0

M     ios/Flutter/Debug.xcconfig
M     ios/Flutter/Release.xcconfig
A     ios/Podfile
A     ios/Podfile.lock
M     ios/Runner.xcodeproj/project.pbxproj
M     ios/Runner.xcworkspace/contents.xcworkspacedata
noboru_inoboru_i

Androidアプリprojectに、普通にFirebaseを組み込む際に必要なファイル配置・変更と同じことが行われていた。
https://firebase.google.com/docs/android/setup#add-config-file

android/app/build.gradle

apply plugin: 'com.google.gms.google-services' が追加されている。

android/build.gradle

classpath 'com.google.gms:google-services:4.3.10' が追加されている。

android/app/google-services.json

app_idなどが記載されているもの。
通常、Web上のFirebase consoleよりダウンロードする。

noboru_inoboru_i

iOSの方は、一般的な手順は以下のあたり。
https://firebase.google.com/docs/ios/setup#add-config-file
https://firebase.google.com/docs/ios/setup#add-sdks

GoogleService-Info.plist を配置して、Swift Package Managerを利用して FirebaseAnalytics などを導入する。

flutterfire configure コマンドでは、ぜんぜん違う形になった。

ios/firebase_app_id_file.json

GoogleService-Info.plist の代わりになりそうなものが配置されていた。
GoogleService-Info.plist とは異なり、Firebase console上からはダウンロードできないように見える。( GoogleService-Info.plist のダウンロードリンクしか存在しない)

https://github.com/firebase/flutterfire/pull/8157/files#diff-7de7813fe5830b238e9b0f3802251c62a63837b1bfb2360d843ddd0f49780148R42-R43
このあたりを見ると、Crashlyticsにてsymbolをアップロードするのに使われていそう?
(ファイルとしては、 packages/firebase_crashlytics/firebase_crashlytics/ios/crashlytics_add_upload_symbols で利用されている)

2022/07/09現在のmasterブランチを見ても、それ以外で firebase_app_id_file.json を使っている部分は存在してなさそう。
(GitHubの firebase/flutterfire 上で . を押してVSCodeを起動、 firebase_app_id_file.json で検索してヒットしたのがそれだけだった。)

なので、一旦 firebase_app_id_file.json を削除してしまっても普通にiOSアプリとして起動できた。(その後、もとに戻した)

noboru_inoboru_i

試しに、以下のpluginを入れてみる。

  • firebase_analytics
  • firebase_crashlytics
  • firebase_performance
flutter pub add firebase_analytics firebase_crashlytics firebase_performance

この時点では、 pubspec.yamlpubspec.lock の変更のみ。


flutterfire configure を再実行する。(初回と同じオプションを付与して実行した)

android側の各 build.gradle に、それらのpluginが追加された。
https://github.com/noboru-i/sample_flutterfire/commit/497a50db760dc0249a13c6732c0e0661738b0422


iOSアプリをビルドしてみると、以下のファイルも変化する。
が、エラーが発生したので、Podfileを書き換える。

diff --git a/ios/Podfile b/ios/Podfile
index 1e8c3c9..0b62d79 100644
--- a/ios/Podfile
+++ b/ios/Podfile
@@ -1,5 +1,5 @@
 # Uncomment this line to define a global platform for your project
-# platform :ios, '9.0'
+platform :ios, '14.0'
 
 # CocoaPods analytics sends network stats synchronously affecting flutter build latency.
 ENV['COCOAPODS_DISABLE_STATS'] = 'true'

再度iOSアプリをビルドしてみると、 Podfile.lock に、各ライブラリが追加された。


Androidアプリをビルドしてみると、以下のエラー。

	uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:firebase_analytics] /Users/.../sample_flutterfire/build/firebase_analytics/intermediates/merged_manifest/debug/AndroidManifest.xml as the library might be using APIs not available in 16
	Suggestion: use a compatible library with a minSdk of at most 16,
		or increase this project's minSdk version to at least 19,
		or use tools:overrideLibrary="io.flutter.plugins.firebase.analytics" to force usage (may lead to runtime failures)

16だとAndroid 4.1なので、てきとーに26(Android 8.0)に変更。

diff --git a/android/app/build.gradle b/android/app/build.gradle
index 7cc14a5..dc2b211 100644
--- a/android/app/build.gradle
+++ b/android/app/build.gradle
@@ -52,7 +52,7 @@ android {
         applicationId "dev.flutter.my_app.sample_flutterfire_20220709"
         // You can update the following values to match your application needs.
         // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
-        minSdkVersion flutter.minSdkVersion
+        minSdkVersion 26
         targetSdkVersion flutter.targetSdkVersion
         versionCode flutterVersionCode.toInteger()
         versionName flutterVersionName

ビルドは成功し、ファイルの変更は無かった。

noboru_inoboru_i

memo
https://qiita.com/noboru_i/items/f3add5f556593f0ea15b
Crashlyticsインストール後、iOSアプリのビルド時に "[firebase_crashlytics] Crashlytics Upload Symbols" というBuild Phaseが追加されるはず。


とりあえず、 firebase_app_id_file.json の利用箇所としては、現状はCrashlytics周りのみで、
上記で追加される "[firebase_crashlytics] Crashlytics Upload Symbols" でのみ利用されていることがわかった。

--dart-define などで環境ごと(dev/stg/prod)のアプリを作成したく、環境ごとにFirebase projectを分けている場合には、上記のBuild Phaseの前に、環境ごとの firebase_app_id_file.json を再配置するBduild Phaseを追加するのが良さそう。