Google Signing using Firebase Authentication in Android using Java
Firebase is a mobile and web application development platform. It provides services that a web application or mobile application might require. Firebase provides email and password authentication without any overhead of building the backend for user authentication. Google Sign-In is a secure way to authenticate users in your apps. It reduces the hassle of dealing with and handling those extra passwords by the user to get authenticated to the app. Firebase offers a great number of options to implement Login in your app like Email, Phone number, Google, Facebook, etc.
What we are going to build in this article?
Here is a sample video of what we are going to build in this article. Note that we will be using Java language to make this application.
Step by Step Implementation
Step 1. Create a New Project
- Open a new project.
- We will be working on Empty Activity with language as Java. Leave all other options unchanged.
- Name the application at your convenience.
- There will be two default files named activity_main.xml and MainActivity.java.
If you don’t know how to create a new project in Android Studio then you can refer to How to Create/Start a New Project in Android Studio?
Step 2. Connecting Firebase with our application
- Navigate to the website https://firebase.google.com.
- Go to the console.
- Add project
Name your project and click on continue and Connect your firebase to your google account.
After completion of the project, the following interface will appear-
Now enter the package name and the name of your application. To enter SHA-1 value follow the steps- Go to Gradle > Task > android > signing report
After opening the signing report you will get the value of SHA-1 as shown below. Copy it and use it in firebase.
After completing the above step download the google-services.json config file provided there and paste it in android studio. Navigate to project > app > src and paste it.
Go to authentication.
Go to Sign-in-method and enable google sign-in.
Step 3. Adding required dependencies and plugins
Navigate to Gradle Scripts > build.gradle(project) and add the following dependency in it-
Navigate to Gradle Scripts > build.gradle(module) and add the following plugin in it-
apply plugin: 'com.google.gms.google-services'
Navigate to Gradle Scripts > build.gradle(module) and add the following dependencies in it-
implementation 'com.google.firebase:firebase-auth:19.4.0' implementation 'com.google.android.gms:play-services-auth:18.1.0' implementation 'com.github.bumptech.glide:glide:4.11.0'
Step 4. Adding Internet permission
Go to the AndroidManifest.xml file and add the following piece of code to it-
Step 5. Working with XML files
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Navigate to app > right-click > new > activity > empty activity and name it as ProfileActivity. Use the following code in activity_profile.xml file-
Step 6. Working with Java files
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Go to the ProfileActivity.java file and use the following code in it-
Here is the final output of our application.
Also, you can see that inside Firebase data of users is being stored-