GitLiveApp / firebase-kotlin-sdk

A Kotlin-first SDK for Firebase
https://gitliveapp.github.io/firebase-kotlin-sdk/
Apache License 2.0
1.17k stars 155 forks source link

How to initialize the app #415

Closed IvanSimovic closed 7 months ago

IvanSimovic commented 1 year ago

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?

Dardev12 commented 1 year ago

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

IvanSimovic commented 1 year ago

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

carlosgub commented 1 year ago

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()
    }
  }
}
j-roskopf commented 1 year ago

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)
nbransby commented 7 months ago

Closing as it seems the issues are resolved