Updated 8 days ago | GitHub

Building Data driven Apps with Firebase

Setting up Firebase

Firebase is Google’s app development platform. It bundles a set of backend services and SDKs — a realtime NoSQL database, authentication, cloud messaging, crash reporting, analytics, and more — so you can build data-driven apps without running your own server.

Firebase products you are likely to reach for first:

  1. Realtime Database and Cloud Firestore — cloud-hosted NoSQL databases that sync data to clients in realtime
  2. Authentication — email/password, phone, and federated sign-in (Google, etc.)
  3. Cloud Messaging — push notifications via FCM
  4. Crashlytics — crash reporting
  5. Analytics — usage and event reporting
  6. Cloud Storage — user-generated file storage
  7. Remote Config — server-side feature flags and configuration

Prerequisites

Per the official setup guide, your app needs to:

  • Target API level 23 (Marshmallow) or higher, running on Android 6.0 or higher
  • Use Jetpack (AndroidX), with compileSdkVersion 28 or later
  • Use the Android Gradle plugin (com.android.tools.build:gradle) v7.3.0 or later
  • Be built with a recent version of Android Studio

Registration

To get started, sign into the Firebase console with a Google account.

Setup

1. Create a Firebase project

In the Firebase console, click Add project (or Create a project) and follow the prompts. A Firebase project is the top-level container — one project can hold your Android, iOS, and web apps.

2. Register your Android app

Inside the project dashboard, click the Android icon to register your app. You need at least your app’s package name (the applicationId in your module-level Gradle file). If you plan to use Google Sign-In with Firebase Authentication, also add your debug signing key’s SHA-1 fingerprint.

To print your debug key’s SHA-1 fingerprint, run the following on Linux or macOS (see Authenticating Your Client for details):

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

On Windows:

keytool -list -v -keystore %USERPROFILE%\.android\debug.keystore -alias androiddebugkey -storepass android -keypass android

3. Add the configuration file

Registering the app lets you download a google-services.json file. Move this file into your app’s module (app-level) root directory — usually <project>/app/.

4. Configure Gradle

Firebase ships its Android libraries through the Google Maven repository, versioned together via the Firebase Android BoM (Bill of Materials). With the BoM on the classpath you declare individual Firebase libraries without version numbers and the BoM picks compatible versions for you.

In your root-level build.gradle.kts, add the google-services Gradle plugin:

plugins {
  // ...
  id("com.google.gms.google-services") version "4.5.0" apply false
}

In your module-level app/build.gradle.kts, apply the plugin and import the BoM:

plugins {
  id("com.android.application")
  // Reads google-services.json and wires up your Firebase config
  id("com.google.gms.google-services")
}

dependencies {
  // Import the Firebase BoM — individual Firebase libraries below omit versions
  implementation(platform("com.google.firebase:firebase-bom:34.17.0"))

  // Analytics (recommended baseline)
  implementation("com.google.firebase:firebase-analytics")

  // Realtime Database, for the data-driven examples below
  implementation("com.google.firebase:firebase-database")
}

If your project still uses the Groovy DSL (build.gradle), the equivalents are id 'com.google.gms.google-services' version '4.5.0' apply false in the root file and implementation platform('com.google.firebase:firebase-bom:34.17.0') in the module file.

The plugin version (4.5.0) and BoM version (34.17.0) above are current as of the official setup guide; check that page and the Android release notes for the latest versions.

Sync your Gradle files and you’re ready to use Firebase.

Writing and reading data with the Realtime Database

The Realtime Database getting-started guide walks through the full flow; the short version follows. Grab a DatabaseReference for the path you want:

val database = Firebase.database
val myRef = database.getReference("message")

If your database instance is not in the default us-central1 region, pass its URL explicitly, e.g. Firebase.database("https://DATABASE_NAME.REGION.firebasedatabase.app").

Write a value with setValue():

myRef.setValue("Hello, World!")

Read it back — and get called again whenever it changes — with a ValueEventListener:

myRef.addValueEventListener(object : ValueEventListener {
    override fun onDataChange(dataSnapshot: DataSnapshot) {
        // Called once with the initial value and again whenever the data changes
        val value = dataSnapshot.getValue(String::class.java)
        Log.d("MyApp", "Value is: $value")
    }

    override fun onCancelled(error: DatabaseError) {
        Log.w("MyApp", "Failed to read value.", error.toException())
    }
})

New Realtime Database instances default to locked-down security rules, so configure rules (or use the emulator suite) before expecting reads and writes to succeed.

Attribution

This guide was originally drafted by Segun Famisa.

References and further reading

  1. Add Firebase to your Android project
  2. Get started with Realtime Database on Android
  3. Firebase Android SDK release notes