Firebaseを使ったアプリを動かした時に、
Default FirebaseApp is not initialized in this process net.minpro.textrecognitionnocamera. Make sure to call FirebaseApp.initializeApp(Context) first.
というエラーが出て、アプリが落ちてしまう場合があります。
この時に、
Make sure to call FirebaseApp.initializeApp(Context) first.
とあるので、MainActivityあるいはMyApplicationクラスでFirebaseApp#initializeAppメソッドを呼び出せば解決するのかと思いきや、そうはイカの塩辛という話です。
(参考にさせて頂いたサイト)

We're seeing a few exceptions with the message Default FirebaseApp is not initialized in this process com.example.app. Make sure to call FirebaseApp.initializeApp(Context) first. in our Android app...
もくじ
Firebaseをプロジェクトに追加するときの初期設定がマズいから起こったエラーだった
結論から言うと、このエラーはFirebaseApp#initializeAppメソッドが必要だからではなく、そもそもFirebaseをプロジェクトに追加するときの初期設定がマズかったことによって起こるエラーなのだそうです。
Firebaseのプロジェクトへの追加は、Android Studioからアシスタント機能を使ってやることもできます(Tools ⇒ Firebase)。
この場合は、このエラーが起こる可能性は低いと考えられます。
一方、手動で追加するやり方もあって、今回のエラーはこの手順に沿っていないことによって発生するようです。
ですので、この手順に沿って行われているかを確認することが、解決法になります。
Firebase SDKの手動での追加が正しく行われているか確認する
手順は、先のリンクの通りなのですが、
1.プロジェクトレベルの build.gradle
ファイルの設定
buildscript { // ... dependencies { // ... classpath 'com.google.gms:google-services:4.1.0' // google-services プラグインの追加 } } allprojects { // ... repositories { // ... google() // Google's Mavenレポジトリの追加 } }
2.アプリ(モジュールレベル)のbuild.gradle
ファイルの設定
apply plugin: 'com.android.application' android { // ... } dependencies { // ... implementation 'com.google.firebase:firebase-core:16.0.4' //Firebaseコアのライブラリ追加 // Getting a "Could not find" error? Make sure you have // added the Google maven respository to your root build.gradle } // google servicesのプラグインの追加(ファイルの末尾にすること!!) apply plugin: 'com.google.gms.google-services'
3.使いたいFIrebaseの機能の依存関係追加
さまざまな Firebase 機能で使用できるライブラリは次のとおりです。
Gradle の依存関係行 | サービス |
---|---|
com.google.firebase:firebase-core:16.0.4 | アナリティクス |
com.google.firebase:firebase-database:16.0.3 | Realtime Database |
com.google.firebase:firebase-firestore:17.1.1 | Cloud Firestore |
com.google.firebase:firebase-storage:16.0.3 | Storage |
com.crashlytics.sdk.android:crashlytics:2.9.5 | Crashlytics |
com.google.firebase:firebase-auth:16.0.4 | Authentication |
com.google.firebase:firebase-messaging:17.3.3 | Cloud Messaging |
com.google.firebase:firebase-config:16.0.1 | Remote Config |
com.google.firebase:firebase-invites:16.0.4 | Invites と Dynamic Links |
com.google.firebase:firebase-ads:16.0.1 | AdMob |
com.google.firebase:firebase-appindexing:16.0.2 | App Indexing |
com.google.firebase:firebase-perf:16.1.2 | Performance Monitoring |
com.google.firebase:firebase-functions:16.1.1 | Cloud Functions for Firebase Client SDK |
com.google.firebase:firebase-ml-vision:17.0.1 | ML Kit(Vision) |
com.google.firebase:firebase-ml-model-interpreter:16.2.2 | ML Kit(カスタムモデル) |
(詳しくは下の動画で解説していますのでよろしければご覧ください)