Introduction
In this blog, I will demonstrate how to connect the Firebase as the backend to your Android Application.
There are two ways of connecting Firebase to the Android application:
1 - Using the Firebase console
2 - Using Android Studio's Firebase Assistant
In this blog, the 1st way will be demonstrated. (i.e. using the Firebase Console)
Prerequisites
- Update Android Studio to its latest version
- You should have Android Device or Emulator with API level 9 or later and Play services 9.0 or later
- Your app must target API level 16 or later
- Your app must use Gradle 4.1 or later
Steps to add Firebase to your Android App
1. Go to the Firebase website and sign in with your mail ID.
2. Go to the Firebase Console
3. Create a new Firebase project
Reuse the same project if you need an iOS version for the same app you are using.
- Click Create a project
- Enter your Firebase project name and click continue
If you wish to add Google Analytics to your Firebase Project, enable Google Analytics. Google Analytics provides a clear view of your app's usage, user engagement and is not limited only to this. So, enabling Google Analytics is recommended.
Choose the Google Analytics location for your Firebase project and accept the terms and conditions, then click create project
4. Add your app
Click Project Overview in the side navigation bar and click the Android icon
Add your Android app's package name. For this, open your Android project on Android Studio. Then, Click Project->Android->Gradle Scripts->build.gradle (Module:app)
Your Package name will be visible as the applicationId
Add a Nickname to your app. This is optional and this is only visible to you on Firebase Console.
Add Debug signing certificate SHA-1. This also is optional but required for Dynamics Links, App invites and Google sign-in and phone number support in Auth. To add this, go to Gradle->your app name->app->Tasks->android->signingReport.
After executing the signing report, get your Debug signing certificate SHA-1 displayed as SHA-1.
Click Register app on your Firebase Console
5. Download Config file "google-services.json" directly to your Android app module root directory or else download and move it into your Android app module root directory.
Steps to find the Android app module root directory:
- Open your Android project folder in File Explorer
- Click app and paste the downloaded JSON file.
To check the downloaded file in Android studio:
- Go to Project->app
6. Modify build.gradle files
The JSON file you just downloaded can be loaded by the Google services plugin. Modify your build.gradle files by adding Google services plugin.
To modify a Project-level build.gradle file:
- Go to Project-level build.gradle
- Project->Android->Gradle scripts->build.gradle (Project:project name)
- Check that you have Google's Maven repository at the required places
- Add Google Services plugin
- buildscript {
- repositories {
-
- google()
-
- }
- dependencies {
-
- classpath 'com.google.gms:google-services:4.3.3'
- }
- }
- allprojects {
-
- repositories {
-
- google()
-
-
- }
- }
7. Modify App-level build.gradle file
- Goto App-level build.gradle
- Project->Android->Gradle scripts->build.gradle (Module:app)
- Add the Google Services plugin
- Add the Google Analytics Firebase SDK, if you enabled Google Analytics for your Project. If needed, add your desired Firebase products SDK.
- apply plugin: 'com.android.application'
-
- apply plugin: 'com.google.gms.google-services'
- android {
-
-
-
- }
- dependencies {
-
-
-
- implementation 'com.google.firebase:firebase-analytics:17.2.2'
-
-
-
- }
Press Sync now that appears in the Android Studio IDE.
Click Next on your Firebase Console. Run your app, if you added Firebase SDK for Google Analytics to send verification to Firebase that you have successfully integrated Firebase to your app. Or you may skip the step too.
Before running your App change the executing file configuration from signingReport to your app.
After running your App, on the Logcat, if you find FirebaseApp initialization Successful message, then congrats! You have successfully added Firebase to your app
You will see the Congrats message on the Firebase Console.
Click to continue to the console and proceed with the next step like adding Firebase Realtime Database, Cloud Storage, etc.
So, we have seen one way of connecting Firebase to an Android app. In Part 2 of the blog, I will demonstrate how to connect Firebase to your Android app using Android Studio's Firebase Assistant.