🦔

Flutter / Firebaseの連携時チェックリストまとめ【Android】

2021/10/02に公開

はじめまして、ますみです!

株式会社Galirage(ガリレージ)という「生成AIのシステム開発会社」で、代表をしております^^

自己紹介.png

この記事では、FlutterFire(firebase_coreなどのライブラリ)を導入する際のやることリストをまとめました。

ちなみに、うまく導入できないと以下のようなエラーが出てきます。

[core/not-initialized] Firebase has not been correctly initialized. Have you added the "google-services.json" file to the project.

1. google-services.jsonの導入

  • google-services.jsonをダウンロードし、/android/app/google-services.jsonという風に/android/app/の中に導入します。
  • iOSでは、Xcodeから直接入れることが推奨されているため、androidでもAndroid Studioから直接インポート(コピー)することを推奨します。
    google-services.png

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
}
...

4. google()の確認(/android/build.gradle

  • 以下のように/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()
    }
}
...

5. dependenciesの追加(/android/build.gradle

  • 続いて、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'
    }
}
...

6. pluginの反映(/android/app/build.gradle

  • /android/app/build.gradleapply 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"
...

7. dependenciesの追加(/android/app/build.gradle

  • 公式が推奨しているため、極力追加しましょう。
/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'
}

8. multiDexの反映(/android/app/build.gradle

  • 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上の「コンソールに進む」ボタンを押すところまで実行しましょう。
  • 少しだけローダーが回る時間があるので、何かしら裏側で処理が回っていそうですね。
    firebase_console.png

10. Android Studioの再起動

  • 人それぞれの環境にも寄りますが、IDEなどを再起動すると読み込みが再度行われるため、これでエラー/バグが直ることがあります。
  • Sync nowを押しましょう」というような推奨もされています(私は、こういった処理をしなくてもflutter runしたら自動的にsyncされました)。

最後に

最後まで読んでくださり、ありがとうございました!
この記事を通して、少しでもあなたの学びに役立てば幸いです!

おまけ①:生成AIエンジニア塾

より専門的な「生成AIエンジニア人材」を目指しませんか?

そんな方々に向けて、「生成AIエンジニア塾」というプログラムを始めました🎉

最終的なゴールとして、『エンタープライズ向けの生成AIシステムを構築するためのスキルを習得し、大手案件で活躍できる人材』を目標とします。

また、一人一人にしっかりと向き合って、メンタリングをできるようにするため、現在メンバーの人数制限をしております。本気度やスキルレベルの高い人から、順番にご案内しております。

▼ 登録はこちらから ▼
https://bit.ly/generative_ai_engineer_school_by_zenn

おまけ②:AI Newsletter for Biz

最新のAIニュースの情報を収集しませんか?

AI Newsltter for Bizは、ビジネスパーソン向けに「AIニュース」を定期配信する完全無料のニュースレターです📩

一人でも多くの方にとって、「AI人材としてのスキルアップ」につながれば幸いです^^

また、現在、登録者限定で「明日から使える 無料AIサービス3選」のPDFを配布中です 🎁
※ ご登録完了のメールに、PDFリンクを添付いたします。

期間限定のプレゼントとなりますので、ぜひ、お早めにご登録ください!

▼ 登録はこちらから ▼
https://bit.ly/ai_newsletter_for_biz_zenn

参考文献

https://firebase.flutter.dev/docs/installation/android/

https://stackoverflow.com/questions/63804012/flutter-firebase-and-android-issue-unable-to-initialise-cannot-find-google-se

Discussion