Integrating Firebase with Android

This tutorial outlines the process of integrating Firebase to an android application by manual Firebase configuration or through the usage of Firebase assistant in Android Studio.

July 9, 20246 min read

Introduction

Firebase is a feature-rich platform developed by Google for creating and expanding online and mobile applications. It offers a range of resources, including real-time databases, cloud storage, authentication, and hosting, to assist developers in producing high-quality programs.

Firebase attempts to make backend development less complicated so that developers may concentrate more on the main features and user experience of their applications. Developers may effectively manage application data, optimize application speeds, and raise user engagement by utilizing Firebase's products.

Firebase's Realtime Database, which enables real-time data synchronization and storage across users, is one of its most notable features. This NoSQL cloud database enables offline functionality, so information is accessible even when the user is not online. It also automatically synchronizes upon reestablishing connectivity. This is very helpful for developing collaborative Android applications such as chat apps, real-time gaming, or any other application that needs to use the most recent data across several devices.

Additionally, Firebase provides strong authentication options, which facilitate the management of user identification and security for developers. Developers may quickly integrate sign-in procedures using several providers, such as phone number, email address and password, and third-party providers like Google, Facebook, and Twitter, with Firebase Authentication. This guarantees that user data is safe and authentication runs smoothly by lowering the overhead involved in creating secure authentication systems from scratch.

This tutorial offers a step by step procedure on how to integrate Firebase to Android applications in two ways.The first option is integrating using Firebase Console while the second option is done using the Android Studio Firebase Assistant.

Prerequisites

  • Ensure the latest Android studio installed
  • Ensure that an android device is setup and connected.Optionally,use an emulator

Try Kodaschool for free

Click below to sign up and get access to free web, android and iOs challenges.

Sign Up

Option 1:Integration using Firebase Console

Step 1:Create a project in the Firebase console

  • Enter the project name of your choice and check the boxes to accept the terms and why you want to use Firebase services then click continue.In this example name your project ImageUpload.

Image

  • Enable Google Analytics in your Firebase project to allow you to get real time data analytics about your application in the Firebase Google Analytics Console.This choice is optional.
Image
  • Next, choose location then accept Google Analytics terms and click Create project.

Image
  • The project will take less than a minute to provision resources and be ready for use.After the setup process is complete,click continue to start exploring the created project.

Step 2:Adding Android App to Firebase Project

  • Create an Android application in Kotlin and name it ImageUploader.Once the project is completely configured note down the package name within the MainActivity.kt class.In this the app the package name is com.example.imageuploader.

Image
  • Under the project overview,click the android icon to navigate to the next step.

Image

  • Enter the Android package name,app nick name and Debug signing certificate SHA-1(Optional) and click Register app.
  • To generate Debug signing certificate use this this command for Mac or Linux keytool -list -v \-alias androiddebugkey -keystore ~/.android/debug.keystore and keytool -list -v \-aliasandroiddebugkey-keystore%USERPROFILE%\.android\debug.keystore for windows users.

After entering the command in Android Studio terminal you will be prompted to enter a password.The password is android.

Image

Step 3:Entering android package,nickname and SHA-1

Image
  • Download google-services.json file that will be added to the android app-level root directory then click next.

Image

Contents of google-services.json file


     {
  "project_info": {
    "project_number": "860662134691",
    "project_id": "imageupload-eb8fa",
    "storage_bucket": "imageupload-eb8fa.appspot.com"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:860662134691:android:adc8d890400b745f33cc16",
        "android_client_info": {
          "package_name": "com.example.imageuploader"
        }
      },
      "oauth_client": [],
      "api_key": [
        {
          "current_key": "AIzaSyAxrC03-5tVQMaSiuVpt5nDzoXsU5eeB7c"
        }
      ],
      "services": {
        "appinvite_service": {
          "other_platform_oauth_client": []
        }
      }
    }
  ],
  "configuration_version": "1"
}

Google-services.json in android app

Image

Project level Build.gradle.kts configuration

Image


App level Build.gradle configuration

Image
  • The next step guides you on how to include the essential dependencies in the build.gradle file.

ImageImage

  • Clicking next navigates you to the page where you will be required to go to the console.

Image
  • After all the setup is complete,this will be the final appearance in the Firebase Console.

Option 2:Integration using Firebase Assistant

Step 1:Configure Android Studio with Latest Update

  • To use Firebase Assistant ensure that you are using the latest Android Studio.If you are using previous versions,you can go to Help and choose check updates(Linux/Windows) while for MacOS go to Android Studio then check updates.

Step 2:Select Firebase Products

  • Next go to Tools then select Firebase.

Image

Step 3:Choose Google Analytics

  • Next select Firebase products such as Analytics.Click Get started with Analytics then select Connect to Firebase.

Image
  • Firebase assistant will navigate to the Firebase Console.

Image

  • Follow the next steps similar to the way we configured Firebase in the First option.
  • Add the Analytics selection to your app.

Image
  • Next,after moving through all the steps as guided by the Firebase Assistant,the app will automatically be linked to Firebase.

Image
  • Finally,if all the steps outlined by the Firebase assistant you will end up with a complete integration as shown below.

Image

Conclusion

The integration of Firebase to an Android application does not need to be complicated when you follow the steps outlined by the Firebase Assistant.Additionally, the option of creating a project in the Firebase manually is very seamless and straightforward and will let you set up a backend for your web and mobile applications within a very short time.

Wilson Ochieng

About Wilson Ochieng

Android Expert developing mobile applications with 4+ Years of Experience.