Real-time code collaboration inside any IDE
🔓 Request Early Access
The Firebase Kotlin SDK is a Kotlin-first SDK for Firebase. It's API is similar to the Firebase Android SDK Kotlin Extensions but also supports multiplatform projects, enabling you to use Firebase directly from your common source targeting iOS, Android or JS.
The following libraries are available for the various Firebase products.
Is the Firebase library or API you need missing? Create an issue to request additional API coverage or be awesome and submit a PR
Unlike the Kotlin Extensions for the Firebase Android SDK this project does not extend a Java based SDK so we get the full power of Kotlin including coroutines and serialization!
Asynchronous operations that return a single or no value are represented by suspending functions in the SDK instead of callbacks, listeners or OS specific types such as Task, for example:
suspend fun signInWithCustomToken(token: String): AuthResult
It is important to remember that unlike a callback based API, wating for suspending functions to complete is implicit and so if you don't want to wait for the result you can launch
a new coroutine:
//TODO don't use GlobalScope
GlobalScope.launch {
Firebase.auth.signOut()
}
Asynchronous streams of values are represented by Flows in the SDK instead of repeatedly invoked callbacks or listeners, for example:
val snapshots: Flow<DocumentSnapshot>
The flows are cold, which means a new listener is added every time a terminal operator is applied to the resulting flow. A buffer with the default size is used to buffer values received from the listener, use the buffer
operator on the flow to specify a user-defined value and to control what happens when data is produced faster than consumed, i.e. to control the back-pressure behavior. Often you are only interested in the latest value received, in this case you can use the conflate
operator to disable buffering.
The listener is removed once the flow completes or is cancelled.
The official Firebase SDKs use different platform-specific ways to support writing data with and without custom classes in Cloud Firestore, Realtime Database and Functions.
The Firebase Kotlin SDK uses Kotlin serialization to read and write custom classes to Firebase. To use Kotlin serialization in your project add the plugin to your gradle file:
plugins {
kotlin("multiplatform") // or kotlin("jvm") or any other kotlin plugin
kotlin("plugin.serialization") version "1.3.72"
}
Then mark you custom classes @Serializable
:
@Serializable
data class City(val name: String)
Instances of these classes can now be passed along with their serializer to the SDK:
db.collection("cities").document("LA").set(City.serializer(), city, encodeDefaults = true)
The encodeDefaults
parameter is optional and defaults to true
, set this to false to omit writing optional properties if they are equal to theirs default values.
You can also omit the serializer for classes that does not have generic type arguments, these functions are marked @ImplicitReflectionSerializer
and their usage is discouraged in general because it is implicit and uses reflection (and therefore not working on Kotlin/Native), but may be useful shorthand in some cases.
Firestore and the Realtime Database provide a sentinel value you can use to set a field in your document to a server timestamp. So you can use these values in custom classes they are of type Double
:
@Serializable
data class Post(val timestamp: Double = ServerValue.TIMESTAMP)
To improve readability functions such as the Cloud Firestore query operators use named arguments:
citiesRef.whereEqualTo("state", "CA")
citiesRef.whereArrayContains("regions", "west_coast")
//...becomes...
citiesRef.where("state", equalTo = "CA")
citiesRef.where("regions", arrayContains = "west_coast")
In cases where it makes sense, such as Firebase Functions HTTPS Callable, operator overloading is used:
val addMessage = functions.getHttpsCallable("addMessage")
//In the official android Firebase SDK this would be addMessage.call(...)
addMessage(mapOf("text" to text, "push" to true))
The Firebase Kotlin SDK provides a common API to access Firebase for projects targeting iOS, Android and JS meaning you can use Firebase directly in your common code. Under the hood, the SDK achieves this by binding to the respective official Firebase SDK for each supported platform.
In some cases you might want to access the underlying official Firebase SDK in platform specific code, for example when the common API is missing the functionality you need. For this purpose each class in the SDK has android
, ios
and js
properties which holds the equivalent object of the underlying official Firebase SDK.
These properties are only accessible from the equivalent target's source set. For example to disable persistence in Cloud Firestore on Android you can write the following in your Android specific code (e.g. androidMain
or androidTest
):
Firebase.firestore.android.firestoreSettings = FirebaseFirestoreSettings.Builder(Firebase.firestore.android.firestoreSettings)
.setPersistenceEnabled(false)
.build()
If you are building a Kotlin multiplatform library which will be consumed from JS code you may need to include the SDK in your package.json
, you can do it as follows:
"dependencies": {
"@gitlive/firebase-auth": "0.2.0",
"@gitlive/firebase-database": "0.2.0",
"@gitlive/firebase-firestore": "0.2.0",
"@gitlive/firebase-functions": "0.2.0",
"@gitlive/firebase-storage": "0.2.0",
"@gitlive/firebase-messaging": "0.2.0"
}