In this post, we are going to learn how to access firebase real-time database using anonymous authentication. We will cover accessing default and non-default database in this post.


  • Google Firebase Project
  • Android App development environment

Note: This post would be useful for those who are likely to integrate firebase real-time database into their Android App.

Firebase project settings

In this section, we will cover all the steps needs to be done under Google Firebase project.

Enable Firebase Anonymous Authentication

Go to ‘SIGN-IN METHOD’ section under Authentication service of your firebase project. Enable Anonymous provider under sign-in providers.
firebase enable anonymous authentication

Add Android app to Firebase Project

Create a new Android app and add to firebase project.
firebase android add app

Add root android app package.
firebase android add package

google-services.json download and configuration

Download google-services.json from Project Settings -> General section.
project setttings google services json

Paste google-services.json into app sub-directory under your Android app project.

Firebase Realtime Database Setup

Setup a new realtime database in your project (Skip this step if already done before). Add a new child user -> user1 in the database. Under user1 and attributes fristName and lastName. Example database screenshot:

firebase database data

Android Firebase dependency configuration

In this section, we will apply all configurations needed to run Android app.

Android build.gradle (project) file configuration

Under dependencies section of the project build.gradle add google-services:3.1.0.

dependencies {
    classpath ''

Add repository in project build.gradle file.

allprojects {
    repositories {
        maven {
            url ""

Android build.gradle (module) file configuration

Under module build.gradle firebase-core:11.4.0, firebase-database:11.4.0 and firebase-auth:11.4.0.

dependencies {
    compile ''
    compile ''
    compile ''

Apply plugin under module build.gradle file. Note: you need to app google-services.json file in your app directory before applying the following plugin.

apply plugin: ''

Android Firebase Anonymous Authentication

Now we need to initialize firebase authentication into Android app. The following code will initialize and perform anonymous login:

FirebaseOptions options = new FirebaseOptions.Builder().setApplicationId("firebase-android-integration")
FirebaseApp app = FirebaseApp.initializeApp(this, options, "firebase-android-integration");
FirebaseAuth auth = FirebaseAuth.getInstance(app);
Task<AuthResult> task = auth.signInAnonymously();

Add code to Main Activity.

Android Firebase Realtime Database Access

Now we are ready to fetch data from firebase real-time database. Following code would fetch user -> user1 JSON data:

FirebaseDatabase database = FirebaseDatabase.getInstance(listener.getFirebaseAppInstance());
DatabaseReference databaseReference = database.getReference("/user/user1");
databaseReference.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        DatabaseRecord record = dataSnapshot.getValue(DatabaseRecord.class);
        Log.d(TAG, record.getFirstName()+":"+record.getLastName()+":"+record.getFullName());

    public void onCancelled(DatabaseError databaseError) {

Append above code after successful anonymous login.


In this post, we learned how to configure firebase anonymous authentication and real-time database connectivity to fetch data using Andriod App.

Android access to Firebase Realtime Database using Anonymous Authentication

Satish Pandey

I am an expert Java Spring Angular developer with 10+ years of rich and varied experience in developing end-to-end Web Applications. I maintain this blog and publish articles in my free time to help the community. Email:

Leave a Reply

Notify of