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'.


android Google Sign-In Integrating

2. Fill all the application detail and select your country/region and click 'Choose and configure services'.


android Google Sign-In Integrating

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.


android Google Sign-In Integrating

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.

  • By using the command prompt.

Windows:

Mac/Linux

  • By Android Studio itself.
    1. Open Android project.
    2. Open the Gradle tab from a right side panel.
    3. Double click on 'signingReport'.
    4. We will found our app SHA-1 key on 'Gradle Console'.

android Google Sign-In Integrating

6. Paste the generated SHA-1 key to Google Sign-In service and click on 'ENABLE GOOGLE SIGN-IN' and 'Generate certification files'.


android Google Sign-In Integrating

7. Now download the 'google-services.json' file to integrate it into the Android application.


android Google Sign-In Integrating

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.


android Google Sign-In Integrating

Required Permission

Add the Internet permission in AndroidMenifest.xml file.

build.gradle (Project)

Add the following dependency in build.gradle file.

build.gradle(Module)

activity_main.xml

Add the TextView and Google SignInButton in activity_main.xml file.

MainActivity.java

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.

activity_profile.xml

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.

ProfileActivity.java

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.

Output:

android Google Sign-In Integrating android Google Sign-In Integrating
android Google Sign-In Integrating android Google Sign-In Integrating




Contact US

Email:[email protected]

Integrating Google Sign-In
10/30