Integrating Google Sign-In in Android App
In this tutorial, we will integrate the Google Sign-In functionality in our Android application using Google API. Combining Google API in Android app helps users to login using Google account.
To integrate the Google Sign-In API in our app, we need to configure our app to Google developer account and download the 'google-service.json' file for the Android application.
Steps to configure Android App on Google Developer Account
1. Create a Google developer account at https://developers.google.com/identity/sign-in/android/start-integrating and click on 'GET A CONFIGURATION FILE'.
2. Fill all the application detail and select your country/region and click 'Choose and configure services'.
3. After successful creation of the Google app support configuration, it will redirect to next windows for selecting Google services. We will select Google Sign-In service.
4. Now, we need to provide signing certification SHA-1 key of our application.
5. There are two different ways of generating certification SHA-1 key.
6. Paste the generated SHA-1 key to Google Sign-In service and click on 'ENABLE GOOGLE SIGN-IN' and 'Generate certification files'.
7. Now download the 'google-services.json' file to integrate it into the Android application.
Example of Integrate Google Sign-In in Android app
In this example, we will integrate the Google Sign-In in the Android app. Once the user successfully login through Google Sign-In, we will redirect to next activity (ProfileActivity) and retrieve user detail.
We need to paste the downloaded 'google-services.json' file in our Android project app directory.
Add the Internet permission in AndroidMenifest.xml file.
Add the following dependency in build.gradle file.
Add the TextView and Google SignInButton in activity_main.xml file.
In the MainActivity.java class, we call the Auth.GoogleSignInApi.getSignInIntent() method to login through Google Sign-In API. The GoogleApiClient.OnConnectionFailedListener interface of Google API overrides its unimplemented method onConnectionFailed(ConnectionResult) which returns the connection failure result. The GoogleApiClient class is used to manage the connection between an Android application and Google Sign-In API.
Add the following component in activity_profile.xml file.
Create a ProfileActivity.java class in which we will display the user detail after the successful login.
In this class, we will retrieve the user detail if the user successfully login. The GoogleSignInResult class implements the Result interface which represents the final result of invoking an API method of Google Play Services.
The GoogleSignInAccount class holds the basic information of user.