Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To connect an Android app to Firebase Realtime Database, register the app in a Firebase project, add the project’s google-services.json file and Gradle configuration, include the Database SDK, then read and write through a DatabaseReference. Before releasing the app, replace any open test rules with rules that require authentication and limit access to the data each user is allowed to reach.

Before you start

Firebase’s Android setup guide lists these project requirements: AndroidX, Android Gradle Plugin 7.3.0 or later, compile against API 28 or later, and a device or emulator for running the app. Its stated minimum target API level is 23. Check the Firebase Android setup guide for the current requirements and setup instructions.

You’ll also need a Firebase project and an Android app whose package name you can enter during registration. The package name in Firebase must match the app’s package name.

Register the Android app and add its configuration file

  1. In the Firebase console, create a project or open one you already use.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  2. Add an Android app to the project. Enter the app’s Android package name exactly as it appears in your project configuration.

  3. Download the generated google-services.json file.

  4. Place the file in the Android app module directory—for a typical project, that is app/google-services.json, alongside the app module’s build file. Do not put it only in the project’s top-level directory.

Firebase describes the file’s contents as unique but non-secret project and app identifiers. The Google services Gradle plugin reads the file and makes configuration values available to Firebase SDKs; it is not a substitute for database security rules. See Firebase’s setup instructions and the Google services Gradle plugin guide.

Configure Gradle and add the Database SDK

Apply the Google services plugin in the project’s Gradle configuration and in the app module. The exact syntax depends on whether the project uses Gradle’s plugins DSL or the older buildscript style; use the syntax matching your existing files and consult the current Firebase setup guide.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In a Kotlin DSL project using the plugins block, the configuration commonly has this shape. The plugin version below is the version listed by Firebase when this article was prepared; verify the current version in the setup guide before using it.

// Project-level build.gradle.kts
plugins {
    id("com.google.gms.google-services") version "4.5.0" apply false
}

// App-level app/build.gradle.kts
plugins {
    id("com.android.application")
    id("com.google.gms.google-services")
}

Add the Firebase Android BoM and Realtime Database dependency to the app module’s dependencies. The BoM version shown here, 34.19.0, is a dated example from Firebase’s setup documentation, not a permanent value. Check Firebase’s setup page for the current BoM version. The BoM selects compatible Firebase library versions, so don’t add an inline version to firebase-database.

dependencies {
    implementation(platform("com.google.firebase:firebase-bom:34.19.0"))
    implementation("com.google.firebase:firebase-database")
}

Sync the project with Gradle after editing the files. If Gradle reports that the plugin cannot be found or the configuration file is missing, check the plugin declaration, the app module’s plugin application, and the location of google-services.json.

Write data and listen for changes

Once the SDK is configured, obtain a database instance and a reference to a path in its JSON tree. This example writes a string to messages/hello and attaches a listener to read its value and subsequent changes:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
val database = FirebaseDatabase.getInstance()
val messageRef = database.getReference("messages/hello")

messageRef.setValue("Hello from Android")

messageRef.addValueEventListener(object : ValueEventListener {
    override fun onDataChange(snapshot: DataSnapshot) {
        val value = snapshot.getValue(String::class.java)
        // Use value to update the UI or application state.
    }

    override fun onCancelled(error: DatabaseError) {
        // Handle permission, connectivity, or other database errors.
    }
})

setValue() writes the value at the referenced path. A ValueEventListener receives the current snapshot and is called again when data at that location changes. Firebase documents that connected clients synchronize local data for active listeners; remove listeners when they are no longer needed, such as when a screen stops observing the data. The Realtime Database Android read and write guide documents these APIs.

For production code, handle write completion as well as read cancellation: a successful method call does not by itself tell the UI whether the server accepted a write under the active rules. Use the write task’s completion listener or callbacks to report success and errors.

Secure the database before release

Realtime Database rules determine which clients can read or write each path. Firebase warns that a database created in test mode can be world-readable and world-writable until its rules are changed. Do not ship an app while relying on permissive test rules. Review the Realtime Database security rules guide and the Android read and write documentation.

  • Require identity where appropriate. Pair database access with Firebase Authentication and write rules that check the authenticated user.

    Free tools Windows power users keep installed

    One-click scans. No signup required.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Restrict access by path. Grant each user access only to the records they should read or modify; avoid broad read or write permissions at the database root.

  • Validate incoming data. Use rules to enforce expected types, required fields, and acceptable values rather than trusting the app’s UI to reject invalid data.

  • Test the rules. Confirm that intended users can perform the necessary operations and that unauthorized reads and writes are rejected before release.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

If the app cannot read or write

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.