firebase-kotlin-sdk icon indicating copy to clipboard operation
firebase-kotlin-sdk copied to clipboard

How i need initialize FirebaseApp?

Open v1rus-dev opened this issue 2 years ago • 8 comments

How i need initialize FirebaseApp for use Firebase.auth? Now I am getting an error: Default FirebaseApp is not initialized in this process by.iba.relocation.android. Make sure to call FirebaseApp.initializeApp(Context) first. when i'm use Firebase.auth(Firebase.app)

v1rus-dev avatar May 29 '22 05:05 v1rus-dev

You need to call

import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.FirebaseOptions
import dev.gitlive.firebase.firestore.firestore
import dev.gitlive.firebase.initialize

        val app = Firebase.initialize(
            options = FirebaseOptions(
                applicationId = "",
                apiKey = "",
                databaseUrl = "",
                gaTrackingId = "",
                storageBucket = "",
                projectId = "",
                gcmSenderId = "",
                authDomain = "",
            )
        )
        val store = Firebase.firestore(app)

MinmoTech avatar Jun 07 '22 17:06 MinmoTech

My android app is crashing if I initialize it like this, complaining that context is null, any idea ?

gergirod avatar Sep 08 '22 10:09 gergirod

@gergirod I don't think we need to configure FirebaseOptions in the case of Android. We can just pass it a context and it should be good to go. That's because the Android project should already have the google-services.json acting as its FirebaseOptions.

shubhamsinghshubham777 avatar Jan 03 '23 04:01 shubhamsinghshubham777

It would be great to have some guide. Meanwhile, what worked for me:

  1. For android a. Add android app using firebase console. Note, my android module has package name com.example.app. And android part of shared module (I use firebase inside that shared module) has name com.example.app.shared. So I had to use com.example.app.shared package name when adding (which was not very intuitive). b. Firebase suggests you to download google-services.json file, I put it inside shared module (in the same dir with shared module build.gradle.kts file). c. After doing that, launching app was able to initialise from file. So I was able to get db reference using simple val db = Firebase.firestore
  2. For ios: a. Add ios app using firebase console. Note, in that one I had to use package name of ios module, not shared (com.example.app in my case) b. As in instructions from console in xcode -> file -> add packages -> https://github.com/firebase/firebase-ios-sdk -> Choose required services from the list (for me it was only firestore) c. In main swift code of ios module I added import FirebaseCore and FirebaseApp.configure() in func application d. File from firebase console (GoogleService-Info.plist) put into the same folder with that main swift file (also there was Info.plist in that dir). e. And the most undocumented step: we need to make sure xcode sees that file. In my case I had to manually add it by left click on project dir (the same we place file in) -> add files to "app" -> add GoogleService-Info.plist After following actions a got firebase working on android and ios.

Disclaimer: I had no idea what I was doing, just randomly applying solutions from the internet to fix errors appearing. Probably my solution is not correct, but it works. I hope in future there will be some guide for setting up firebase multiplatform.

UPD: 3. Figured it out for JS: a. Add web app using Firebase console b. Instead of following advices from Firebase console, follow solution of @MinmoTech: without any index.html file modification or adding extra dependencies, I added to main function of main.js.kt (before everything else) Firebase.initialize(FirebaseOptions(...)) with credentials created by Firebase console

dimitree54 avatar Feb 04 '23 21:02 dimitree54

@dimitree54 Would you be able to share your "shared" module's build.gradle.kts? I'm having the same issue and I suspect I'm importing the libraries incorrectly in the gradle.

I have added the implementation("dev.gitlive:firebase-firestore:1.8.0") to commonMain dependencies. I'm unsure of how to import the google-services plugin correctly in Kotlin Gradle DSL

javierlcontreras avatar Jun 14 '23 15:06 javierlcontreras

Hello @javierlcontreras, Maybe you can find something useful in my abandoned project https://github.com/dimitree54/buboc_ui (also follow submodule cuboc_core, some firebase setup done there). But I do not develop that project for some time already, so could not comment that code.

dimitree54 avatar Jun 14 '23 17:06 dimitree54

Hello @javierlcontreras, Please add id("com.google.gms.google-services").version("4.3.14").apply(false) in the inside the plugins of build.gradle.kts(Module :Project) and add id("com.google.gms.google-services") in the plugins {} of build.gradle.kts(Module :androidApp).

Note: Don't forget to add google-services.json in the android app

rsuman132 avatar Jul 01 '23 12:07 rsuman132

So thanks to @dimitree54 's steps I managed to get my app initialized, and Firebase Auth and Firestore are working on both Android and IOS. I have a secondary issue now, though: I added Google One Tap Sign in, which uses the com.google.android.gms:play-services-auth library, as an Android-specific feature. It's executing the service call right, but it's getting an com.google.android.gms.common.api.ApiException: 10 exception, which is usually an indication that the app's SHA-1 fingerprint isn't set up right on Firebase/present in the google-services.json file (double-checked, it's there in both). Has anybody else encountered that/managed to get it working?

A side note I only have the google-services.json file in the shared module, when I tried adding it to the androidApp module

Execution failed for task ':androidApp:processDebugGoogleServices'.

No matching client found for package name 'com.example.app.MyApplication'

The package name in the google-services.json file is just "package_name": "com.example.app", which is right, and the path to my application class is com.example.app.MyApplication. It's not doing this when the google-services file is just in shared, only when I add it to androidApp, but still it's really odd, if anyone can tell me why it's doing that I'd appreciate it

DavinRot avatar Sep 10 '23 18:09 DavinRot