Post

KMP (Kotlin Multiplatform) Firebase setup

Intro

Nearly every mobile application nowadays utilizes Firebase in some capacity, whether it’s implementing CRUD operations through their NoSQL database, analyzing A/B tests, or detecting crashlytics. It has become a crucial component for launching your app successfully.

Unfortunately, when it comes to Kotlin Multiplatform (KMP), Firebase lacks official support, as highlighted in this UserVoice thread. However, GiveLiveApp has taken on much of the heavy lifting with their SDK wrapper.

This blogpost goes in hand with using this SDK instead of reinventing the wheel, you’re free to use expect/actual anytime.

Steps

Create Firebase project

Navigate to the Firebase console and click “Add project”

It should look like this package structure

Choose a name for your Firebase project and click “Continue”.

package structure

In the following step, you’ll be prompted to enable analytics. This is optional; you can choose based on your preference.

package structure

Wait until the project is created

package structure

Add configuration for Android

Now, add the configuration parameters for your Android app.

package structure

After clicking on the Android icon, you’ll be taken to a registration screen for your app. Ensure that you provide the correct package name and then click “Register.”

package structure

On the next step, make sure to download the generated google-services.json file.

Proceed to add it to your :composeApp or wherever your entry point to your Android app is within your KMP structured project.

package structure

Add configuration for iOS

The process for iOS configuration is similar to Android. Begin by adding your Apple target.

package structure

Ensure that your BUNDLE_ID, which can be found in your Config.xconfig, is correctly added in the configuration file.

package structure

As a naming convention, add the suffix “iOS” to your project nickname for clarity. This helps distinguish between different projects, I did not add it to the Android one on purpose as this is slight inconvenience that everyone does, not necessarily a must change.

package structure

In the next step, download the generated GoogleService-Info.plist file.

Right-click on the iosApp.xcodeproj file in your iosApp folder and open it with Xcode.

package structure

Copy the GoogleService-Info.plist to the iosApp folder.

package structure

A screen will appear, click “Finish.”

package structure

You can change the reference path if needed.

package structure

In Xcode, navigate to File > Add package dependencies.

package structure

Make sure to add the Firebase dependency from https://github.com/firebase/firebase-ios-sdk.

package structure

Choose the Firebase components you want to use from the repository. In this example, we’ll use analytics and crashlytics. Then, click “Add Packages.”

package structure

Due to an issue where the Frameworks, Libraries, and Embedded Content section is missing when creating a KMP project, you may need to add it manually from the Build phases tab in Xcode and restart Xcode a few times.

Ensure to add all necessary components from Firebase that you’ll be using in your project.

package structure

That’s all for the setup process. It’s a bit lengthy, but as of the time of writing this article, this is the current setup procedure.

Writing some code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[versions]
#Firebase android
firebase-android-bom = "32.8.1"
#Gradle
gradlePlugins-crashlytics = "2.9.9"
gradlePlugins-google-services = "4.4.1"
#Gitlive
firebase-gitlive-sdk = "1.12.0"

[libraries]
#Firebase
firebase-android-bom = { module = "com.google.firebase:firebase-bom", version.ref = "firebase-android-bom" }
firebase-android-crashlytics-ktx = { module = "com.google.firebase:firebase-crashlytics" }
#Gitlive
gitlive-firebase-kotlin-crashlytics = { module = "dev.gitlive:firebase-crashlytics", version.ref = "firebase-gitlive-sdk" }

[plugins]
crashlytics = { id = "com.google.firebase.crashlytics", version.ref = "gradlePlugins-crashlytics" }
google-services = { id = "com.google.gms.google-services", version.ref = "gradlePlugins-google-services" }

In your project’s build.gradle.kts, include the following plugins:

1
2
3
4
5
plugins {
    ...
    alias(libs.plugins.google.services) apply false
    alias(libs.plugins.crashlytics) apply false
}

In your :composeApp or Android app entry point, be sure to include the following plugins:

package structure

Ensure that your :shared module’s build.gradle.kts contains the necessary setup.

1
2
3
4
5
sourceSets {
        commonMain {
            api(libs.gitlive.firebase.kotlin.crashlytics)
        }
}   

Testing the implementation

Once your setup is complete, you can run the Android app to verify the implementation.

package structure

Additionally, you can configure settings such as disabling crashlytics collection on Android. Further details on configuration options are available in other articles. Below, I’ll demonstrate how to configure this on iOS.

On the iOS side, you can configure the collection settings as follows. Start by adding the initialization point to your iOSApp.swift:

package structure

Then, within your MainViewController.kt file, you can define your Kotlin logic to enable or disable collection in debug mode:

package structure

When you run the app through Xcode, you’ll notice that Firebase has been successfully initialized:

package structure

Conclusion

Integrating Firebase into your Kotlin Multiplatform project facilitates sharing some platform benefits seamlessly. However, it’s worth noting that Firebase lacks support for Desktop, including but not limited to Crashlytics. This limitation may affect your project’s scope.

Stay hydrated in the early summer heat! Don’t forget to drink water. Thanks for reading, stay cool!

Until the next article!

This post is licensed under CC BY 4.0 by the author.