To make `Firebase Crashlytics` work in a up to dat...
# multiplatform
m
To make
Firebase Crashlytics
work in a up to date KMP project, I had to do some manual changes. The
google-services.json
is not picked up in the new project structure. Sharing here if someone needs it, I’m now not relying on the automatic init of the SDK, but do it manually:
Copy code
fun initializeFirebase(context: Context) {
    // Load google-services.json from the custom directory
    val inputStream: InputStream = context.assets.open("config/google-services.json")
    val json = inputStream.bufferedReader().use { it.readText() }

    val jsonObject = JSONObject(json)

    val options = FirebaseOptions.Builder()
        .setProjectId(jsonObject.getString("project_id"))
        .setApplicationId(jsonObject.getString("mobilesdk_app_id"))
        .setApiKey(jsonObject.getString("current_key"))
        .setGcmSenderId(jsonObject.getString("project_number"))
        .setStorageBucket(jsonObject.getString("storage_bucket"))
        .build()

    FirebaseApp.initializeApp(context, options)
}
That was the base code ChatGPT gave me, I hardvoded the keys for now, not using the JSON loading. But base concept stays the same. Works for me.
thank you color 3
h
@Max if in package commonMain then where do get context? since multiplatform do not have context
m
Firebase.initialize(
applicationContext,
options = FirebaseOptions(
applicationId = "Add your firebase android appId here",
apiKey = "Add your firebase project api key here",
projectId = "Add your firebase projectId here"
)
)
m
@hafiz you need to do it separately per platform, my above code is for Android only - so the code goes in the MainApplication class which you registered in the manifest, there you got a context
m
349 Views