Adding Firebase to your Android Project -Simple & Easy!

Dinesh Hashan
7 min readOct 31, 2020

In this article, I’ll share the experience that I had with Firebase while doing my Final year research. First of all, I had to say working with Firebase is much easier than I thought. Yes, this topic is not that interesting but mark my words you’ll definitely going to have fun when you start working with both Android Studio & Firebase.

Let’s start with a quick introduction to Firebase.

Photo by Rami Al-zayat on Unsplash

What is Firebase?

Firebase is a platform developed by Google for creating mobile and web applications. It was originally an independent company founded in 2011. In 2014, Google acquired the platform and it is now their flagship offering for app development.

Firebase projects are backed by Google Cloud Platform, letting you scale your app to billions of users. So you know it’s a Google Cloud Platform + Firebase kind of relationship.

So that’s enough for Firebase, now let’s get into Android.

What is Android?

Android is a mobile operating system based on a modified version of the Linux kernel and other open-source software, designed primarily for touchscreen mobile devices such as smartphones and tablets. We will be using Android Studio as our Development tool or also known as our IDE.

Enough on introductions, now let’s get back to our main point.

These are the prerequisites that you need to fulfill before start working on your project:

Prerequisites

To complete this task, install Android Studio to its latest version.

If you’ve installed Android Studio already:

  • Install the latest updates in Android Studio by selecting Help > Check for Updates.

Make sure that your project meets these requirements*:

  • Targets API level 16 (Jelly Bean) or later
  • Uses Gradle 4.1 or later
  • Uses Jetpack (AndroidX), which includes meeting these version requirements:
  • com.android.tools.build:gradle v3.2.1 or later
  • compileSdkVersion 28 or late

*These requirements may differ with time, always make sure to update to the latest

We are all set! Now let’s get started.

There are two options you might need to pick from;

Option 1: (recommended) Add Firebase using the Firebase console

Option 2: Use the Android Studio Firebase Assistant (may require additional configuration).

Option 1: Add Firebase using the Firebase console.

Adding Firebase to your app involves tasks both in the Firebase console and in your open Android project (for example, you download Firebase config files from the console, then move them into your Android project).

Sign in to Firebase using your Google account.

Step 1: Create a Firebase project

Before you can add Firebase to your Android app, you need to create a Firebase project to connect to your Android app. Visit Understand Firebase Projects to learn more about Firebase projects.

  1. In the Firebase console, click Add project, then select or enter a Project name
Adding a project in the Firebase Console.

Give a Project name like the below for your project. A unique identifier will be created alongside it identifies your project among other projects.

Adding a suitable project name.

Note: If you have an existing Google Cloud Platform (GCP) project, you can select the project from the dropdown menu to add Firebase resources to that project.

2. (Optional) If you are creating a new project, you can edit the Project ID.

3. Click Continue

(Optional) Set up Google Analytics for your project, which enables you to have an optimal experience using any of the following Firebase products:

When prompted, select to use an existing Google Analytics account or to create a new account.
If you choose to create a new account, select your Analytics reporting location, then accept the data sharing settings and Google Analytics terms for your project.

Enabling Google Analytics for the project.

5. Click Create project (or Add Firebase, if you’re using an existing GCP project).

Firebase automatically provisions resources for your Firebase project. When the process completes, you’ll be taken to the overview page for your Firebase project in the Firebase console.

Here I will be Enabling Google Analytics for this project. Besides, it won’t hurt our project’s performance or neither does not cost both you or me any.

Step 2: Register your app with Firebase

In order to use Firebase in your Android app, you need to register your app with your Firebase project. Registering your app is often called “adding” your app to your project. Like I said it’s simple and easy!

  1. Go to the Firebase console.
  2. In the center of the project overview page, click the Android icon or Add app to launch the setup workflow.
Adding our app to Firebase to get started.

3. Enter your app’s package name in the Android package name field.

What’s a package name, and where do you find it?

  • A package name uniquely identifies your app on the device and in the Google Play Store.
  • A package name is often referred to as an application ID.
  • Find your app’s package name in your module (app-level) Gradle file, usually app/build.Gradle (example package name: com.yourcompany.yourproject).

Be aware that the package name value is case-sensitive, and it cannot be changed for this Firebase Android app after it’s registered with your Firebase project.

4. (Optional) Enter other app information: App nickname and Debug signing certificate SHA-1.

Here I will not be giving Debug signing certificate SHA-1 instead I will be giving it a nickname since why not?

Configurations on the Firebase side, in order to connect the app with firebase.

5. Click Register app.

Step 3: Add a Firebase configuration file

Steps on how to get the google-services.json file.
  1. Add the Firebase Android configuration file to your app:
  2. Click Download google-services.json to obtain your Firebase Android config file (google-services.json).
  3. Move your config file into the module (app-level) directory of your app.
  4. To enable Firebase products in your app, add the google-services plugin to your Gradle files.
  5. In your root-level (project-level) Gradle file (build.gradle), add rules to include the Google Services Gradle plugin. Check that you have Google’s Maven repository, as well.
buildscript {

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

}

dependencies {
// ...

// Add the following line:
classpath 'com.google.gms:google-services:4.3.4' // Google Services plugin

}
}

allprojects {
// ...

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

// ...
}
}

6. In your module (app-level) Gradle file (usually app/build.gradle), apply the Google Services Gradle plugin:

apply plugin: 'com.android.application'
// Add the following line:
apply plugin: 'com.google.gms.google-services' // Google Services plugin


android {
// ...
}

Step 4: Add Firebase SDKs to your app

  1. Using the Firebase Android BoM, declare the dependencies for the Firebase products that you want to use in your app. Declare them in your module (app-level) Gradle file (usually app/build.gradle).

Since I enabled the Analytics I will be doing the following code change to my project code:

dependencies {
// ...

// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:26.0.0')


// When using the BoM, you don't specify versions in Firebase library dependencies

// Declare the dependency for the Firebase SDK for Google Analytics
implementation 'com.google.firebase:firebase-analytics'

// Declare the dependencies for any other desired Firebase products
// For example, declare the dependencies for Firebase Authentication and Cloud Firestore
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.firebase:firebase-firestore'
}

The code change if did not enable Analytics:

dependencies {
// ...

// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:26.0.0')


// When using the BoM, you don't specify versions in Firebase library dependencies

// Declare the dependencies for the desired Firebase products
// For example, declare the dependencies for Firebase Authentication and Cloud Firestore
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.firebase:firebase-firestore'
}

2. Sync your app to ensure that all dependencies have the necessary versions.

That’s it! You can go ahead to check out the next option.

Option 2: Add Firebase using the Firebase Assistant

The Firebase Assistant registers your app with a Firebase project and adds the necessary Firebase files, plugins, and dependencies to your Android project — all from within Android Studio!

  1. Open your Android project in Android Studio and access the Firebase Assistant:
  • Go to File > Check for updates to make sure that you’re using the latest versions of Android Studio and the Firebase Assistant.
  • Go to Tools > Firebase to open the Assistant pane.

2. Choose a Firebase product to add to your app. Expand its section, then click the tutorial link (for example, Analytics > Log an Analytics event).

  • Click Connect to Firebase to connect your Android project with Firebase.
  • Click the button to add the desired Firebase product (for example, Add Analytics to your app).

3. Sync your app to ensure that all dependencies have the necessary versions.

4. In the Firebase Assistant pane, follow the remaining setup instructions for your selected Firebase product.

5. Add as many other Firebase products as you’d like via the Firebase Assistant!

Firebase Assistant pane on Android Studio.

That’s all for now folks! Hope you guys enjoyed and learned something new with Android & Firebase through this small blog of mine. Since this my first blog, I would appreciate it if you could give a clap & share it with your friends. And please, feel free to leave a comment. I’d love to hear your thoughts and feedback.

Hang tight guys! See you in my next blog.

--

--

Dinesh Hashan

Love learning & building new things. Currently working as a Software Engineer. Twitter @theRealDinnah18 🧑🏻‍💻