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

How to initialize the app

Open IvanSimovic opened this issue 2 years ago • 4 comments

How do I initialize the app? I get this error message: Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.bambuser.onetoone.dev. Make sure to call FirebaseApp.initializeApp(Context) first.

the initializeApp function does not seem to be available. Also, context is not available in the SDK either. Dependencies I use are: implementation("dev.gitlive:firebase-auth:1.8.0"), implementation("dev.gitlive:firebase-firestore:1.8.0"), implementation("dev.gitlive:firebase-functions:1.8.0")

How the code looks with normal firebase:

val builder = FirebaseOptions.Builder()
            .setApplicationId(regionData.id)
            .setProjectId(regionData.projectId)
            .setApiKey(regionData.apiKey)
            .setStorageBucket(regionData.storageBucket)
        if (regionData.databaseUrl.isNotEmpty())
            builder.setDatabaseUrl(regionData.databaseUrl)

        try {
            if (name == null)
                FirebaseApp.initializeApp(context, builder.build())
            else
                FirebaseApp.initializeApp(context, builder.build(), name)
        } catch (e: Exception) {

        }

Additional questions: Are there any example projects using this library? Can I have two firebase apps at the same time, one in my core app and one in the KMP library, both authenticated to same credentials?

IvanSimovic avatar Aug 28 '23 13:08 IvanSimovic

What platform/OS is your project running on? My Experience the first time was very tricky with this lib for integrate it on the Android Part and IOS Part

Dardev12 avatar Aug 29 '23 01:08 Dardev12

I am developing on macOs. Building the KMP library for android and iOS

IvanSimovic avatar Aug 29 '23 07:08 IvanSimovic

Hi, I create a medium article about this https://medium.com/@carlosgub/how-to-implement-firebase-firestore-in-kotlin-multiplatform-mobile-with-compose-multiplatform-32b66cdba9f7 , you need to have something like this in your ios file

import SwiftUI
import Firebase // Add this line

@main
struct iOSApp: App {
  // Add this init with the FirebaseApp.configure()
  init(){
    FirebaseApp.configure()
  }
    
  var body: some Scene {
    WindowGroup {
      ContentView()
    }
  }
}

carlosgub avatar Sep 22 '23 04:09 carlosgub

How does one initialize the app on a JVM target?

I have iOS + Android integration working, but looking at the instructions here, https://github.com/GitLiveApp/firebase-java-sdk#initializing-the-sdk, it isn't clear how to initialize the app on JVM targets.

Edit:

Okay, I now see the part in the above sdk: we include a minimal Android emulation layer in the SDK only implementing the functionality used by the Firebase libraries at runtime. So I am just using the 'emulated' context.

    val options = FirebaseOptions.Builder()
        .setProjectId("my-firebase-project")
        .setApplicationId("appId")
        .setApiKey("apiKey")
        // setDatabaseURL(...)
        // setStorageBucket(...)
        .build()

    FirebaseApp.initializeApp(Context(), options)

j-roskopf avatar Nov 15 '23 20:11 j-roskopf

Closing as it seems the issues are resolved

nbransby avatar Apr 10 '24 05:04 nbransby