Kickstart your Android app with Firebase

Pulkit Aggarwal
Coding Blocks
Published in
5 min readApr 29, 2020

Recently I've built an entire application using Firebase as BaaS(Backend as a Service) and I got to know about a lot of features that one can use to make good quality apps, so I will be going over all these topics one by one with all the best practices possible along with the resolution of every problem you might face while implementing a particular set of feature in your application.

This story is first in the part of the series, Firebase for Mobile Developers.

Series Pit Stops

  • Kickstart your Android App with Firebase(You are here)
  • Implementing Authentication using Firebase
  • Choosing the best database for your app: Firestore Vs Realtime
  • Implementing Firebase Firestore in your app
  • Getting started with Firebase Storage
  • Using Firebase Extensions to improve your app
  • Secure your app with Firebase Rules
  • Implementing Realtime Database
  • Getting started with Firebase Cloud Functions
  • Implementing Remote Config
  • Push Notification using Cloud Functions & Firebase Messaging

All the codes can be found here https://github.com/aggarwalpulkit596/Firebase-Android-Starter

So, first, let's start by creating a project and connecting it to our application.

Step 1: Create a project on Firebase Console.

Start by clicking on the add project card and you’ll be redirected to a page where you are supposed to name your Project. Let’s name it Firebase-Android-Starter or you can also choose any other name depending upon your choice and then you’ll be given an option whether to turn on Firebase Analytics or not. You can enable Firebase Analytics anytime if not enabled at the time of project creation but I’d suggest that you start with analytics enabled because sooner or later you have to enable it.

Step 2: Connecting the Firebase Project with your existing/new Android Project.

Once you create your project you’ll be redirected to the dashboard of your project in which you’ll be seeing options to connect iOS, Android, Web, etc apps to your project, Select android, and then you have to fill up the page shown below.

First, you need to fill up your android app package name make sure it is the same package name that you used while creating the project. Also, you can look for the package name inside your build.gradle file named applicationId.

Then you need to give this application a name(internal use only) which is usually like Project-AndroidApp. Last but most important the SHA-1 key which is a very crucial thing if you like to integrate Mobile, Gmail, Facebook, etc authentication in your application.

For now, we’ll just see how to add the SHA-1 Key quickly although we’ll be coming back to this part when we integrate Social Authentication.

Step 1:Open Android Studio and click on the Gradle tab which should be on the right side of the studio window.

Step 2:Select Tasks and then android and then double-tap on signingReport.

Step 3:Once done there should be some set of keys displayed in your run window of Android Studio. To be precise you’ll be presented with 3 sets of different SHA-1 keys namely debug, debugAndroidTest, debugUnitTest.

Out of these three keys, you have to select the key with variant name as debug.

SHA-1 Key for debug version

Now just copy the SHA1 key and paste it in the SHA-1 Key field. Also, firebase does not allow us to use the same set of the package name and SHA1 key, so choose your package name wisely whenever starting with a new project.

Step 3: Adding Config File and Setting up Firebase Dependencies

After the successful completion of Step 2, you’ll get a file named google-services.json.This file is one of the most important part of your project for connecting with firebase. This file contains all the important information which is required to connect your Android App to the firebase Project, information like your package name, SHA1 key configuration, etc and because of this reason, this file shouldn’t be shared with anyone/shared on Github. One can use your firebase project resources/temper with data etc.

Now after downloading the file, you need to paste the file inside your app folder. You can do that by following the images below as a reference.

Now we need to add the firebase dependencies in our project as firebase is 3rd Party Library for android and for this we need to modify our build.gradle files of app and project respectively.

Project-level build.gradle (<project>/build.gradle):

buildscript {
repositories {
google()
}
dependencies {

classpath 'com.google.gms:google-services:4.3.3'

}
}

App-level build.gradle (<project>/<app-module>/build.gradle):

apply plugin: 'com.android.application'
//Other Plugins
apply plugin: 'com.google.gms.google-services'
dependencies {
implementation 'com.google.firebase:firebase-analytics:17.4.0'
}

After modifying the above files you need to sync your project with the internet so that all the files for the above libraries can be downloaded then you are done with the basic setup of firebase for your Android App.

That’s it for this blog in the next blog of the series we’ll see what is Firebase Authentication and how can we use different authentications in your Android app.

--

--