🦔
Flutter / Firebaseの連携時チェックリストまとめ【Android】
はじめまして、ますみです!
株式会社Galirage(ガリレージ)という「生成AIのシステム開発会社」で、代表をしております^^
この記事では、FlutterFire(firebase_core
などのライブラリ)を導入する際のやることリストをまとめました。
ちなみに、うまく導入できないと以下のようなエラーが出てきます。
[core/not-initialized] Firebase has not been correctly initialized. Have you added the "google-services.json" file to the project.
google-services.json
の導入
1. -
google-services.json
をダウンロードし、/android/app/google-services.json
という風に/android/app/
の中に導入します。 - iOSでは、Xcodeから直接入れることが推奨されているため、androidでもAndroid Studioから直接インポート(コピー)することを推奨します。
2. bundle idの一致確認
- bundle idが一致していることを確認しましょう。
- 具体的には、「flutter project内でのbundle id」と「firebaseで設定したbundle id」が一致している必要があります。
- 余談ですが、bundle idの変更はそこそこ大変なので(bundle idの変更方法)、個人的には、
flutter create --org com.umimori
というようにcompany idを指定してプロジェクト作成することを推奨します。 - 具体的な確認ポイントは以下の通りです。以下の2箇所が一致していない場合は、flutter側かfirebase側のbundle idを変更しましょう。
/android/app/google-services.json
...
"client": [
{
"client_info": {
"mobilesdk_app_id": "xxxxx",
"android_client_info": {
+ "package_name": "com.umimori.sample"
}
},
...
/android/app/build.gradle
...
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
+ applicationId "com.umimori.sample"
minSdkVersion 21
targetSdkVersion 30
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
...
/android/build.gradle
)
4. google()の確認(- 以下のように
/android/build.gradle
のファイルにgoogle()
が明記されていることを確認しましょう。
/android/build.gradle
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
+ google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.10'
}
}
allprojects {
repositories {
+ google()
mavenCentral()
}
}
...
/android/build.gradle
)
5. dependenciesの追加(- 続いて、
classpath 'com.google.gms:google-services:4.3.10'
を/android/build.gradle
に追加しましょう。 - ここのバージョンはFirebaseの導入時期によって変わるため、適宜最新のものを確認しましょう(2021年10月2日時点では、
4.3.10
が最新)。
/android/build.gradle
buildscript {
...
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
+ classpath 'com.google.gms:google-services:4.3.10'
}
}
...
/android/app/build.gradle
)
6. pluginの反映(-
/android/app/build.gradle
にapply plugin: 'com.google.gms.google-services'
を追加します。
/android/app/build.gradle
...
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
+ apply plugin: 'com.google.gms.google-services'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
...
/android/app/build.gradle
)
7. dependenciesの追加(- 公式が推奨しているため、極力追加しましょう。
/android/app/build.gradle
...
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
+ implementation platform('com.google.firebase:firebase-bom:28.4.1')
+ implementation 'com.google.firebase:firebase-analytics'
}
/android/app/build.gradle
)
8. multiDexの反映(-
minSdkVersion
が20以下の場合は、multiDexの反映も行う必要があります。
/android/app/build.gradle
...
android {
...
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.umimori.sample"
minSdkVersion 16
targetSdkVersion 28
+ multiDexEnabled true
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
}
...
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation platform('com.google.firebase:firebase-bom:28.4.1')
implementation 'com.google.firebase:firebase-analytics'
+ implementation 'com.android.support:multidex:1.0.3'
}
9. Firebaseの「コンソールに進む」
- 適度に慣れてくると
google-services.json
をダウンロードして、満足している方も多そうですね。 - Firebase上の「コンソールに進む」ボタンを押すところまで実行しましょう。
- 少しだけローダーが回る時間があるので、何かしら裏側で処理が回っていそうですね。
10. Android Studioの再起動
- 人それぞれの環境にも寄りますが、IDEなどを再起動すると読み込みが再度行われるため、これでエラー/バグが直ることがあります。
- 「
Sync now
を押しましょう」というような推奨もされています(私は、こういった処理をしなくてもflutter run
したら自動的にsyncされました)。
最後に
最後まで読んでくださり、ありがとうございました!
この記事を通して、少しでもあなたの学びに役立てば幸いです!
おまけ①:生成AIエンジニア塾
より専門的な「生成AIエンジニア人材」を目指しませんか?
そんな方々に向けて、「生成AIエンジニア塾」というプログラムを始めました🎉
最終的なゴールとして、『エンタープライズ向けの生成AIシステムを構築するためのスキルを習得し、大手案件で活躍できる人材』を目標とします。
また、一人一人にしっかりと向き合って、メンタリングをできるようにするため、現在メンバーの人数制限をしております。本気度やスキルレベルの高い人から、順番にご案内しております。
▼ 登録はこちらから ▼
おまけ②:AI Newsletter for Biz
最新のAIニュースの情報を収集しませんか?
AI Newsltter for Bizは、ビジネスパーソン向けに「AIニュース」を定期配信する完全無料のニュースレターです📩
一人でも多くの方にとって、「AI人材としてのスキルアップ」につながれば幸いです^^
また、現在、登録者限定で「明日から使える 無料AIサービス3選」のPDFを配布中です 🎁
※ ご登録完了のメールに、PDFリンクを添付いたします。
期間限定のプレゼントとなりますので、ぜひ、お早めにご登録ください!
▼ 登録はこちらから ▼
参考文献
Discussion