Skip to content

Expert: features and shipping · Publishing on the Play Store and Firebase · topic 46 of 50 · 35 min

Firebase basics

Explanation

Firebase is Google's set of backend services for apps. Add it with the Firebase console, the google-services.json file, the Google services Gradle plugin and the Firebase BoM. Common services: Authentication (email, phone OTP, Google sign-in), Cloud Firestore (a cloud database with real-time listeners), Crashlytics (crash reports), Cloud Messaging (push notifications) and Analytics. Always write Firestore security rules.

Why Firebase?

Many apps need the same backend features: user login, a cloud database, file storage, push notifications and crash reports. Building and running a server for these takes time and skills. Firebase provides them as managed services with Android SDKs, so a small team or a solo developer can ship a full app quickly.

Adding Firebase to an Android app

  1. Create a project in the Firebase console and add an Android app with your exact applicationId.
  2. Download google-services.json and put it in the app/ folder.
  3. Add the Google services Gradle plugin and the Firebase BoM, then the libraries you need.
kotlin
// app/build.gradle.kts
plugins {
    id("com.google.gms.google-services")
}

dependencies {
    implementation(platform("com.google.firebase:firebase-bom:<version>"))
    implementation("com.google.firebase:firebase-auth")
    implementation("com.google.firebase:firebase-firestore")
    implementation("com.google.firebase:firebase-crashlytics")
}
The BoM keeps all Firebase library versions compatible; you set its version once. Follow the setup guide for the current plugin and BoM versions.

The main services

ServiceWhat it doesExample
AuthenticationSign-in with email/password, phone number OTP, Google and moreLogin with mobile OTP
Cloud FirestoreA NoSQL cloud database of collections and documents, with real-time updatesChat messages, orders
Cloud StorageStore user files like photosProfile pictures
CrashlyticsReal-time crash reports with stack traces from users' phonesFind crashes you never saw in testing
Cloud Messaging (FCM)Push notifications to devices"Your order is out for delivery"
AnalyticsUsage events and audiencesWhich screens people use

Firestore in practice

Firestore stores data as documents (like JSON objects) inside collections. For example, a collection orders holds one document per order. You can read once, or listen for changes and get updates in real time, which is how chat apps show new messages instantly.

kotlin
data class Order(val item: String = "", val qty: Int = 0, val status: String = "placed")

val db = Firebase.firestore

// Write
db.collection("orders").add(Order("Veg biryani", 2))

// Listen for real-time changes (wrap in a Flow in a repository)
db.collection("orders").whereEqualTo("status", "placed")
    .addSnapshotListener { snapshot, error ->
        val orders = snapshot?.toObjects(Order::class.java).orEmpty()
    }
Default values in the data class let Firestore create objects from documents. In real apps, put this inside a repository and expose a Flow.

Security rules: never skip this

Firestore and Storage are accessed directly from the app, so security rules on Firebase's servers decide who can read and write what. The "test mode" offered when you create a database allows anyone to read and write for a limited time. Before real users arrive, write rules such as "users can read and write only their own documents". Leaving open rules has exposed many apps' user data.

output
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId}/orders/{orderId} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
  }
}
Only a signed-in user can access orders under their own user id.

Pricing

Firebase has a free plan (Spark) with generous limits for learning and small apps, and a pay-as-you-go plan (Blaze) for growth; some features, such as Cloud Functions, need Blaze. Phone authentication SMS and heavy database usage cost money at scale. Check the current pricing page, set budget alerts, and design queries to read only what you need.

Think of it like this: Firebase is like renting a fully serviced office: reception (Authentication), filing cabinets (Firestore and Storage), a notice board (Cloud Messaging) and a maintenance team that reports every broken tap (Crashlytics). You just bring your business. But you still decide who gets keys to which cabinet (security rules).

Common mistakes

  • Leaving Firestore in test mode with open rules.
  • google-services.json for a different applicationId: the app cannot connect.
  • Reading whole collections when the screen needs ten items: slow and expensive. Use queries and limits.
  • Not declaring Firebase data collection in the Data safety form.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Firebase basics · 1/7

Backend as a service

Firebase gives apps ready backend services: login, a cloud database, files, push notifications and crash reports.

Prefer a video by a teacher? Find free videos on “firebase android kotlin setup authentication firestore crashlytics tutorial” on YouTube ↗

In real life: A tuition centre app with OTP login and live notices

A tuition centre in Patna wants parents to log in with their phone number and see notices instantly. The developer uses Firebase Authentication with phone OTP, stores notices in a Firestore collection, listens for changes so new notices appear immediately, sends a push notification through FCM when an important notice is posted, and watches Crashlytics after release. Security rules allow parents to read notices but only staff to write them.

kotlin
// Repository wrapping a Firestore listener in a Flow
fun noticesFlow(): Flow<List<Notice>> = callbackFlow {
    val reg = Firebase.firestore.collection("notices")
        .orderBy("postedAt", Query.Direction.DESCENDING)
        .addSnapshotListener { snap, _ -> trySend(snap?.toObjects(Notice::class.java).orEmpty()) }
    awaitClose { reg.remove() }
}
callbackFlow turns a listener into a Flow; awaitClose removes the listener when collection stops.

Practice (0/3 done)

Task 1 · Question

Where does google-services.json go?

Task 2 · Question

Why are Firestore security rules essential?

Task 3 · Practice

Add Firebase to a test app: email/password sign-up and login with Firebase Authentication, a Firestore "notes" collection under each user (users/{uid}/notes), a real-time list of the user's notes, and security rules that allow each user to access only their own notes. Force a test crash and see it in Crashlytics.

Quick quiz

Question 1

1.What does the Firebase BoM do?

Question 2

2.Which Firebase service reports crashes from users' devices?

Question 3

3.How is data organised in Cloud Firestore?

Question 4

4.Which service sends push notifications?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What does the Firebase BoM do?

Answer: Keeps all Firebase library versions compatible

Set one BoM version; omit individual versions.

Which Firebase service reports crashes from users' devices?

Answer: Crashlytics

Crashlytics collects crash reports.

How is data organised in Cloud Firestore?

Answer: Collections of documents

Firestore is a document database.

Which service sends push notifications?

Answer: Firebase Cloud Messaging

FCM delivers messages to devices.

Which service delivers push notifications?Related

Answer: Cloud Messaging (FCM)

Use firebase_messaging in Flutter.

From Firebase in Flutter

How is a VPC network scoped in Google Cloud?Related

Answer: Global, with regional subnets

Google Cloud VPC networks are global resources; each subnet lives in one region.

From Core Google Cloud services

In the authorization code flow, where does the user type their Google password?Related

Answer: On Google's own page

The user logs in at Google. Your app never sees the Google password.

From Login with Google, OTP and passkeys

Which Google sample app demonstrates modern Android architecture?Related

Answer: Now in Android

Now in Android is open source.

From Portfolio and next steps

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. This is the last topic of “Publishing on the Play Store and Firebase”. The module exam comes next.
  2. Module 11 exam: Publishing on the Play Store and Firebase · 13 questions · 25 min, without help. 4 topics of this module still to finish first.
  3. Then the next module, Android career path. 50 topics left in the course before the final exam.

← Previous: Play policies: privacy, data safety and targets