How to connect your Android app to Firebase

Nabeel
7 min readOct 7, 2019

Created by Google, Firebase is a powerful and comprehensive app development platform. Firebase harnesses the power of the cloud to help you implement things like storage, analytics and authentication into your applications relatively easily. In this article we will explore how to get started by showing you how to create a Firebase project and set it up with your Android application.

Step 1

First you’re going to need to create an Android app in Android Studio. For now I have created a basic app using an empty activity targeting API 19. If you already created an Android app you’re all good to go!

Step 2

Now you’re going to head to: https://console.firebase.google.com/ and click on ‘Add Project’. First, you’re going to need to come up with the project name and ID. The project name can be changed later but the ID cannot so choose carefully! The ID is the unique global identifier for your Firebase project and will also be used in the URL for your project (For example: https://console.firebase.google.com/project/your-id-here). Once you figure out the project name and ID click continue.

Step 1, for now I am just going to use Firebase-Demo as the project name.

Step 3

Next you’re going to be asked if you want to add Google Analytics into your project. If you plan to release your app onto the Google Play store, Analytics will be invaluable to you. Using Analytics allows you to view useful information about how your users are interacting with your application such as: Current active users, Daily user engagement, Crash-free users and App version adoption, among many other metrics.

Step 2

If you choose to enable Analytics you will have to create a Google Analytics account in the next step:

(Optional) Step 3, make sure to read all the Data sharing settings and Google Analytics terms carefully.

Once you’ve made your choices go ahead and click Create project!

Nice job, you have a Firebase Project all set up! Now it’s time to connect your Android application with Firebase so you can start having fun using all those powerful tools.

Step 1

After completing the setup you will be redirected to the Firebase console. Here is where you will be able to manage all of your applications features. First click on the Android button:

Next you’ll be sent to the setup screen. This might look a little daunting at first but don’t worry, it’s all quite simple.

Step 2

You’re going to be asked for you the package name of your application. The package name is referenced in a lot of places throughout your Android Studio project files, but the best place to find it is in your app level build.gradle file:

Select this build.gradle file under Gradle Scripts

Then in the file you’ll find your package name here:

Package name next to the applicationId

Once you’ve got that go ahead and copy & paste it into the first field:

Next you’ll be asked for a nickname for your app. This is optional and will just be used to refer to your app throughout Firebase so we will skip this.

Finally you’ll be asked for a Debug signing certificate which the description states is: “Required for Dynamic Links, Invites, and Google Sign-In or phone number support in Auth”. If you plan to use any of these features you’ll have to go through some additional steps to get that signing certificate. For our purposes now however, we won’t be needing this and we can just click Register App.

Step 3

Now you’re going to have to download the google-services.json configuration file.

First you’re going to want to switch to the project view in Android Studio so you can place the file in the correct place:

Click on Android and then Project

Then you’ll need to expand these two folders to get into the proper directory:

The google-services.json file will go here

If you want you can right click on the app folder and select Copy Path in order to make sure you have the correct directory to save the file into:

Now go ahead and download the config file. Once done correctly you should see it underneath the app directory:

Once you see the file there go on and click next to move on to the next step.

Step 4

Almost there! Now you will have to modify some files. Specifically both the project and app level build.gradle files.

First go ahead and switch back to the Android view, rather than the project view:

Then open both build.gradle files under Gradle Scripts:

In the project level build.gradle you’re going to need to add three lines. If you generated your project in Android Studio you should already have two of these. Go ahead and check the project level build.gradle to make sure it contains these lines:

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.2’
}
}allprojects {

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

}
}

In the context of Android Studio the project level build.gradle should now look like this:

Yes I’m using breakpoints to highlight lines of code, no you shouldn’t do this

In the app level build.gradle you may only need to add two lines, depending on how many Firebase SDK’s you want to implement. Earlier in the tutorial we opted for adding Google Analytics into our project so we are going to add the Firebase analytics dependency into our gradle:

apply plugin: ‘com.android.application’
dependencies {
// add the Firebase SDK for Google Analytics
implementation ‘com.google.firebase:firebase-analytics:17.2.0’
}

Then finally at the bottom of the file make sure to add this line, this is crucial as your app will not connect to Firebase without it:

apply plugin: ‘com.google.gms.google-services’

So in the context of Android Studio the app level build.gradle should now look like this (all the extra code you see is just what Android Studio loads by default for a new project):

Lastly don’t forget to sync your Gradle:

Click that Sync Now button at the end

Final Step

This is the end! Now you have to make sure you configured everything properly and that Firebase can connect with your app:

Go ahead and plug in your android phone to your computer and run the application from Android Studio:

If all goes well you should see a green checkmark from the setup screen:

Troubleshooting:

If you don’t see a success message try going back through all the steps listed here carefully and make sure you added all the proper lines of code. You can also try clicking the previous button to go back to the Add Firebase SDK step, then clicking next again. Additionally try stopping your app and re-running it again.

Congratulations! You’ve now successfully created a Firebase project and integrated Firebase into your Android app. In upcoming tutorials we will show you how to start using Firebase’s many tools such as Authentication and Firestore Cloud Storage. Make sure to follow me here on Medium to know when I write new tutorials!

--

--