Expert: features and shipping · Publishing on the Play Store and Firebase · topic 46 of 50 · 35 min
Firebase basics
Explanation
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
- Create a project in the Firebase console and add an Android app with your exact applicationId.
- Download google-services.json and put it in the app/ folder.
- Add the Google services Gradle plugin and the Firebase BoM, then the libraries you need.
// 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 main services
| Service | What it does | Example |
|---|---|---|
| Authentication | Sign-in with email/password, phone number OTP, Google and more | Login with mobile OTP |
| Cloud Firestore | A NoSQL cloud database of collections and documents, with real-time updates | Chat messages, orders |
| Cloud Storage | Store user files like photos | Profile pictures |
| Crashlytics | Real-time crash reports with stack traces from users' phones | Find crashes you never saw in testing |
| Cloud Messaging (FCM) | Push notifications to devices | "Your order is out for delivery" |
| Analytics | Usage events and audiences | Which 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.
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()
}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.
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;
}
}
}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.
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
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.
// 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() }
}Practice (0/3 done)
Where does google-services.json go?
Why are Firestore security rules essential?
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Release builds, signing and the App BundlePublishing on the Play Store and Firebase · same module
- Firebase in FlutterFlutter and Dart · Networking, storage and Firebase
- Core Google Cloud servicesCloud Computing (AWS, Azure, Google Cloud) · Google Cloud and comparing the clouds
- Login with Google, OTP and passkeysBackend Development · Authentication and authorization
- Portfolio and next stepsAndroid career path
- AWS vs Azure vs Google Cloud: the service mapCloud Computing (AWS, Azure, Google Cloud) · Google Cloud and comparing the clouds
Questions people ask about this
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
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.
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.
Which Google sample app demonstrates modern Android architecture?Related
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
- This is the last topic of “Publishing on the Play Store and Firebase”. The module exam comes next.
- 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.
- Then the next module, Android career path. 50 topics left in the course before the final exam.