Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

Adding Firebase to Android App

Firebase offers a suite of cloud-based services that help developers to develop high-quality apps, grow their user base, and earn more profit. Adding Firebase to an Android app can be broken down into a series of steps:

Step 1: Set up a Firebase project

  • Go to the Firebase Console.
  • Click on Add project.
  • Follow the on-screen instructions to create a new project.

Step 2: Add your app to the Firebase project

  • In the Firebase console, click on the Android icon to add an Android app to your project.
  • Enter your app's package name.
  • Optionally, enter other app information like App nickname and Debug signing certificate SHA-1.
  • Click on Register app.

Step 3: Add the Firebase configuration file to your app

  • Download the google-services.json file.
  • Copy this file to your app's module app/ directory.

Step 4: Add Firebase SDK to your app

  • Open your project-level build.gradle file (<project>/<project-name>/build.gradle):

Add the following to the dependencies section:

classpath 'com.google.gms:google-services:4.3.8' // Check for the latest version
  • Open your app-level build.gradle file (<project>/<project-name>/app/build.gradle):

Add the following at the bottom of the file:

apply plugin: 'com.google.gms.google-services'
  • Now, to add specific Firebase services, add their respective dependencies. For instance, if you wish to add Firebase Authentication, add the following to your app-level build.gradle:
implementation 'com.google.firebase:firebase-auth:21.0.1' // Check for the latest version

If you want to add Firestore:

implementation 'com.google.firebase:firebase-firestore:23.0.3' // Check for the latest version

Remember to always check for the latest versions of these dependencies.

  • Sync your project with gradle files.

Step 5: Initialize Firebase in your app

In your main Application class or the main activity, initialize Firebase:

import com.google.firebase.FirebaseApp;

public class MyApplication extends Application {
    
    @Override
    public void onCreate() {
        super.onCreate();
        
        FirebaseApp.initializeApp(this);
    }
}

If you're initializing in the main activity, replace Application with Activity and MyApplication with your activity name.

That's it! You've now successfully added Firebase to your Android app. Depending on the Firebase services you wish to use, you'll have different steps for initialization and usage. Always refer to the official Firebase documentation for detailed and up-to-date instructions.

  1. Firebase authentication in Android example:

    Firebase Authentication provides simple authentication using various methods. Here's an example of email and password authentication:

    // MainActivity.java
    FirebaseAuth mAuth = FirebaseAuth.getInstance();
    
    // ...
    
    // Example of email and password authentication
    mAuth.createUserWithEmailAndPassword("user@example.com", "password123")
        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    // Authentication successful
                    FirebaseUser user = mAuth.getCurrentUser();
                } else {
                    // Authentication failed
                    Toast.makeText(MainActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
                }
            }
        });
    
  2. Firebase Authentication with Email and Password Android:

    We already covered email and password authentication in the first example. To sign in an existing user:

    mAuth.signInWithEmailAndPassword("user@example.com", "password123")
        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    // Sign-in successful
                    FirebaseUser user = mAuth.getCurrentUser();
                } else {
                    // If sign in fails, display a message to the user.
                    Toast.makeText(MainActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
                }
            }
        });