Simple Steps to Integrate Android App with Google Firebase
Greetings Everyone,
Hope this blog finds you and your family in good health. Today we are going to learn a brief introduction about Firebase and simple steps to integrate Firebase with your Android Application.
Firebase is one of the Platform-as-a-Service provided by Google.
There are many services provided where we can use it on a pay-as-you-go pricing. Most of the services provided are on the basis of the Free tier so there is a limit set on the Services we use. Currently, Firebase is supported for Android, iOS, the web, C++, and Unity. For every platform, there is Official Documentation available for Integration and the Next Steps.
We will be focussing on the Android Platform today and the step-by-step process we need to follow to Integrate Firebase into our Android Application. There are many services provided like Authentication, Realtime Database, Cloud Firestore, Crashlyitcs, Machine Learning where we can use them in our Application once Integration is Successful.
Step 1: Create a Firebase Account
Creating a Firebase Account is very easy and it’s free for everyone. Just log on to https://firebase.google.com/ and navigate to the Sign-in Button on the right end. Now it will prompt you to select the Gmail Account or enter your Gmail address, and hit the Sign in Button. That’s all you have your Firebase Account ready with you. Let’s get ready for the next step which is gonna be more interesting.
Step 2: Creating a New Project
Once after a Successful login, click on the Go to Console Button situated on the right end. Now you will enter your Console aka Dashboard containing the list of all the projects created under your Account. Now since it’s a fresh account and no projects are present, you will see a Homepage something like below:
Hit on Create a Project Button to get started with your first Project. There will be a total of 3 Steps in which you are going to create your Project in Firebase.
Step 1: Now you will be prompted to enter the Name of the Project which needs to be unique since automatically your Firebase Account will be linked to Google Cloud Platform (GCP). So this means in backend once you create a Firebase Project, automatically a Project in GCP will be created.
As you can see here once you enter the name of the project automatically Firebase checks whether the name of the Project is available in GCP and whether it’s unique. You can also observe that a random alphanumeric number gets added at the end of the Project name to maintain its uniqueness and collision of names with other projects. You need to accept the terms and conditions and then click on Continue Button to proceed.
Step 2: Enabling Google Analytics for the Project. This is one of the powerful tools provided by Google where we can track our Project by various parameters. I recommend you to enable Google Analytics for your project which helps us to gather various insights from our Users.
Step 3: Configuring Google Analytics. Here we need to choose our nearest Country and accept the Terms of Google Analytics and make sure you select the Use the default settings for sharing Google Analytics data.
Now hit on Create Project Button and now we are good to go! Wait for some time until the project gets created and visible in your Console. Once successful creation of the project you will see a homepage like below:
Congratulations! Now it’s time to integrate our Android Application into the project what we have created just now.
Step 3: Adding Android Application to Project
Click on the Android Logo now a new Window opens where we need to add our Android Studio project to Firebase.
Before this, first, we need to create a project in Android Studio. Now open Android Studio on your Desktop/Laptop by searching in Search Menu and once it’s open.
Click on File > New > New Project > Empty Activity. Now you will see the following window where you need to enter your Project details.
Note: Both the Firebase Project name and Android Application Name need not be the same. Here in my case, I am keeping the same name to my Android App.
Once you are done entering all the details of your Android Application, hit on Finish Button. Now your Android Studio project gets built. Wait until the whole project gets built successfully.
Now, in the Firebase Console, this window will be opened once you click the Android Icon, here we need to enter details of our Android Studio Project.
We need to enter our Package Name for our app where we can get it from the Android Studio, now navigate to Android Studio, and open Module Level Gradle build.gradle which is located in the left Navigation Menu. Now a file will get opened containing all the details like plugins, dependencies, etc.
Copy the applicationId which is present in defaultConfig and paste into the Android Package Name field in Firebase Console.
It is optional to enter the App Nickname and Debug Signing Certificate SHA-1. This certificate is required when you use services like Phone Authentication and Single Sign-On Authentication. Now click on Register App Button.
Then we need to download the google-services.json file and we need to upload it to our Android Studio Project. This JSON file contains all the details of our Firebase Project and some parameters which help Firebase to monitor our Project. Now once the file gets downloaded, navigate to Android Studio, in the left Navigation Menu, change from Android to Project. Then all the files which are at the Project level will be displayed.
Now we need to place our google-services.json file under the app directory. So here in my case, the Directory Structure will be
File Path: MySampleProject > app > google-services.json
Once done adding the file, then again come back to Android Directory.
Now the next step is to we need to add the Firebase SDK (Software Development Kit) to our App, in the form of Dependencies in Gradle Files.
Add the following dependencies in the following files
In project level gradle, in dependencies section add this line
classpath ‘com.google.gms:google-services:4.3.8’
In module level gradle, add the following line in the plugin and dependencies section
apply plugin: ‘com.google.gms.google-services’
dependencies {
// add these
implementation platform(‘com.google.firebase:firebase-bom:28.3.0’)
implementation ‘com.google.firebase:firebase-analytics-ktx’
}
Finally, your file needs to look like the below figure, Now you can see a prompt at the top like the “Sync Now” Button.
Click on Sync Now and now all the Firebase SDK gets downloaded to your project. In the Firebase Console, click on Continue now it shows a message like “Waiting for Analytics Data…”.
This message is shown because, since we have integrated Firebase to our app now once our App gets successfully installed on the Phone then it starts sending data to Firebase Servers then our Analytics Dashboard will be shown.
Congratulations, We have finally integrated Firebase with our Android App!!!
Now you can use various services provided by Firebase in your app by simply adding dependencies in your application.
Hey Reader, I am extremely thankful 🙏 to you for reading till the end of my blog. Please share this with your friends, and do clap 👏 if you like the Article. I keep writing Blogs on Web Development and Android Development.
Do follow Saikiran Kopparthi for more Tech Blogs.