Connect your Android App to Firebase Database

On how to Connect your Android App to Firebase Database just follow the below steps.

Installation & Setup

1. But first you have to setup a Firebase project and follow the screen instructions firebase console.

2.Then Navigate to database section and create one and click on create database > test mode to access > enable.

3.Go to project Overview > Project settings > General > click on android icon in your apps.

4.Register your app and download google-services.json and paste it inside project > app.

5. Then in Project-level build.gradle add the following code.

buildscript {
  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google’s Maven repository
  }
  dependencies {
    …
    // Add this line
    classpath ‘com.google.gms:google-services:4.3.3’
  }
}

allprojects {
  …
  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google’s Maven repository
    …
  }
}

6. In App-level build.gradle add.

apply plugin: ‘com.android.application’
// Add this line
apply plugin: ‘com.google.gms.google-services’

dependencies {
  // add the Firebase SDK for Google Analytics
  implementation ‘com.google.firebase:firebase-analytics:17.2.2’
  // add SDKs for any other desired Firebase products
  // https://firebase.google.com/docs/android/setup#available-libraries
}

7. now click on sync now in the Android Studio.

Now, Run your project for Connect your Android App to Firebase Database.

8. copy this refrence of code to onCreate of MainActivity class .

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");

myRef.setValue("Hello, World!");

Now, run the app to print the hello world in your console.

You can access the Github code from here.

Tagged : / / / /