- Firebase Kotlin SDK
- Kotlin-first design
- Suspending functions
- Flows
- Serialization
- Server Timestamp
- Polymorphic serialization (sealed classes)
- Default arguments
- Named arguments
- Operator overloading
- Multiplatform
- Accessing the underlying Firebase SDK
- NPM modules
- How to Build an Android App with Firebase and Kotlin
- HOW TO USE FIREBASE REALTIME DATABASE IN ANDROID/Kotlin
- Our Example
- Steps
- 1) Create Realtime Database and data node in Firebase.
- 2) Integrate and Code Firebase Realtime Database in your app
- Whole Code
- 3) Run and test your app
- Conclusion
Firebase Kotlin SDK
The following libraries are available for the various Firebase products.
Service or Product | Gradle Dependency | API Coverage |
---|---|---|
Authentication | dev.gitlive:firebase-auth:1.8.0 | |
Realtime Database | dev.gitlive:firebase-database:1.8.0 | |
Cloud Firestore | dev.gitlive:firebase-firestore:1.8.0 | |
Cloud Functions | dev.gitlive:firebase-functions:1.8.0 | |
Cloud Messaging | dev.gitlive:firebase-messaging:1.8.0 | |
Cloud Storage | dev.gitlive:firebase-storage:1.8.0 | |
Installations | dev.gitlive:firebase-installations:1.8.0 | |
Remote Config | dev.gitlive:firebase-config:1.8.0 | |
Performance | dev.gitlive:firebase-perf:1.8.0 | |
Crashlytics | dev.gitlive:firebase-crashlytics:1.8.0 |
Is the Firebase library or API you need missing? Create an issue to request additional API coverage or be awesome and submit a PR
Kotlin-first design
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!
Suspending functions
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
Flows
Asynchronous streams of values are represented by Flows in the SDK instead of repeatedly invoked callbacks or listeners, for example:
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.
Serialization
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:
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. Using @EncodeDefault on properties is a recommended way to locally override the behavior set with encodeDefaults .
You can also omit the serializer but this is discouraged due to a current limitation on Kotlin/JS and Kotlin/Native
Server Timestamp
FirestoreRealtime Database kotlin @Serializable data class Post( // In case using Realtime Database. val timestamp = ServerValue.TIMESTAMP, // In case using Cloud Firestore. val timestamp: Timestamp = Timestamp.ServerTimestamp, // or val alternativeTimestamp = FieldValue.serverTimestamp, // or @Serializable(with = DoubleAsTimestampSerializer::class), val doubleTimestamp: Double = DoubleAsTimestampSerializer.serverTimestamp ) firebase-firestore kotlin @Serializable data class PointOfInterest( val reference: DocumentReference, val location: GeoPoint ) val document = PointOfInterest( reference = Firebase.firestore.collection(«foo»).document(«bar»), location = GeoPoint(51.939, 4.506) )
Polymorphic serialization (sealed classes)
This sdk will handle polymorphic serialization automatically if you have a sealed class and its children marked as Serializable . It will include a type property that will be used to discriminate which child class is the serialized.
You can change this type property by using the @FirebaseClassDiscrminator annotation in the parent sealed class:
@Serializable @FirebaseClassDiscriminator("class") sealed class Parent
In combination with a SerialName specified for the child class, you have full control over the serialized data. In this case it will be:
Default arguments
To reduce boilerplate, default arguments are used in the places where the Firebase Android SDK employs the builder pattern:
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder() .setDisplayName("Jane Q. User") .setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg")) .build(); user.updateProfile(profileUpdates) .addOnCompleteListener(new OnCompleteListener() < @Override public void onComplete(@NonNull Tasktask) < if (task.isSuccessful()) < Log.d(TAG, "User profile updated."); >> >); //. becomes. user.updateProfile(displayName = "state", photoURL = "CA")
Named arguments
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")
Operator overloading
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))
Multiplatform
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.
Accessing the underlying Firebase SDK
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()
NPM modules
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:
This page was generated approximately about 15 hours ago.
For copyright and licensing details please see the LICENSE file.
How to Build an Android App with Firebase and Kotlin
Rahul Pandey is an app developer and Facebook engineer. He has created many apps that are currently on the app store and he made a tutorial to show you how he created one of those apps.
We’ve released the course on the freeCodeCamp.org YouTube channel. In the course, you will learn how to build an Android app in Kotlin which allows users to update their status using only emoji 😇🐼❤️.
The app combines various Firebase services (Authentication, Cloud Functions, and Firestore) to allow users to update and view emoji-only statuses. The user is able to create or sign in to their account with Google.
Here are the sections covered in this course:
- App architecture
- Firebase Authentication logic
- User sign up Cloud Function
- Display user emoji list
- Update emoji status
- Restrict input to emoji
I’m a teacher and developer with freeCodeCamp.org. I run the freeCodeCamp.org YouTube channel.
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)
Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.
HOW TO USE FIREBASE REALTIME DATABASE IN ANDROID/Kotlin
In this article we will learn how to use firebase real time database in our app. The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected client and remains available when your app goes offline.
Our Example
In our example we have textview “data” which displays/reads current value of realtime database node named “data”. We also have an edittext “change_data” which allows users to change value of data node at realtime db on a “change” button click, after changing its value in realtime database corresponding value will also be reflected inside “data” textview of all connected users.
Requirements:
Note: First you need to create Firebase Project and then connect your app to it. You can follow this article to set it up.
Steps
1) Create Realtime Database and data node in Firebase.
2) Integrate and Code Firebase Realtime Database in your app
1) Create Realtime Database and data node in Firebase.
First go to Firebase and open your Firebase Project and click on Database and then click on Create Database under Realtime Database Heading.
Start in test mode during testing, which allows anyone who have your db reference to read and write data to your database. During real time development or production impose security rules by using firebase authentication according to your requirements
After creating Database, create a node named data and assign its value “default”
2) Integrate and Code Firebase Realtime Database in your app
First add firebase realtime database library at app level build.gradle file
implementation 'com.google.firebase:firebase-database:19.3.0'
Layout file conatins textview, edittext and button below is xml code.
Inside oncreate() method we have first created Firebase Realtime Database object inside db varaiable and got reference to data node inside dataNodeRef varaiable. dataNodeRef variable corresponds to data node inside firebase database which we have created in first step.
db = FirebaseDatabase.getInstance() dataNodeRef = db. getReference("data")
addValueEventListener is added to dataNodeRef, which will call onDataChange() whenever dataNodeRef(data) value is changed. Inside onDataChange() we will set that value to our data textview.
dataNodeRef. addValueEventListener(object : ValueEventListener < override fun onDataChange(dataSnapshot: DataSnapshot) < if (dataSnapshot.exists()) data.text = "Value is : " + dataSnapshot.value >override fun onCancelled(databaseError: DatabaseError) <> >)
change() method will be called when change button is clicked. It will get current value of our change_data edittext and set that value to our dataNodeRef(data). Thus dataNodeRef(data) value can be changed by any user by clicking change button and its value will be synced across all our connected user and will be displayed inside data textview as well inside data node of firebase database.
Whole Code
Project level build.gradle
buildscript < ext.kotlin_version = '1.4.0-rc' repositories < jcenter() google() maven < url 'https://dl.bintray.com/kotlin/kotlin-eap' >> dependencies < classpath 'com.android.tools.build:gradle:3.5.3' // Add this line classpath 'com.google.gms:google-services:4.3.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" >> allprojects < repositories < google() jcenter() >> task clean(type: Delete)
apply plugin: 'com.android.application' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-android' // Add this line apply plugin: 'com.google.gms.google-services' android < compileSdkVersion 28 buildToolsVersion "29.0.3" defaultConfig < applicationId "com.programtown.example" minSdkVersion 17 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" >buildTypes < release < minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' >> > dependencies < implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' implementation 'com.google.firebase:firebase-database:19.3.0' implementation "androidx.core:core-ktx:1.3.1" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" >repositories < maven < url 'https://dl.bintray.com/kotlin/kotlin-eap' >mavenCentral() >
package com.programtown.example import android.os.Bundle import android.view.View import androidx.appcompat.app.AppCompatActivity import com.google.firebase.database.* import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() < var db: FirebaseDatabase? = null var dataNodeRef: DatabaseReference? = null override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) db = FirebaseDatabase.getInstance() dataNodeRef = db. getReference("data") dataNodeRef. addValueEventListener(object : ValueEventListener < override fun onDataChange(dataSnapshot: DataSnapshot) < if (dataSnapshot.exists()) data.text = "Value is : " + dataSnapshot.value >override fun onCancelled(databaseError: DatabaseError) <> >) > fun change(view: View?) < dataNodeRef. setValue(changeData.text.toString()) changeData.setText("") >>
3) Run and test your app
On first app launch initially value of data textview will be default.
After changing edittext value to “New value” and clicking on change button, data textview value as well as realtime database node data value will be changed to “New value”.
And this newly updated value will be synced/visible to all our connected user of our application.
Conclusion
So in this post we have learned how to integrate/connect Firebase Real Time Database in android application.