Google Login in Android

Google Login in Android

Hello guys, in our previous articles we looked at Facebook and Twitter login in Android. In this article, we are now going to try doing the same using Google.

Nowadays everyone has a Gmail account, and we could make it easy for our app user’s to use Gmail login to simplify their authentication process in our app.

Google Login in Android

Before you can start integrating Google Sign-In in your own app, you must configure a Google API Console project and set up your Android Studio project. We are going to cover the steps involved, step by step.

Today Almost all web and mobile apps allow you to login with Google and Facebook Login. Android Login with Google is a really useful feature for both the app developer and the user, since almost everybody tend to have a Google and Facebook account and moreover with android login with Google you don’t need to remember your UserId and password.

In this Android login with Google tutorial we will describe how to integrate Google Login to your Android Application, We will be creating a sample Google Login app describing how to integrate google login and retrieve user profile. The user can login using their existing Google Account. Integration of Google Login to the App allows the app developers to perform actions on user behalf.

Prerequisites

Google Sign-In for Android has the following requirements:

  • A compatible Android device that runs Android 2.3 or newer and includes the Google Play Store or an emulator with an AVD that runs the Google APIs platform based on Android 4.2.2 or newer and has Google Play Services version 9.8.0 or newer.
  • The latest version of the Android SDK, including the SDK Tools component. The SDK is available from the Android SDK Manager in Android Studio.
  • A project configured to compile against Android 2.3 (Gingerbread) or newer.
  • The Google Play Services SDK:
    1. In Android Studio, select Tools > Android > SDK Manager.
    2. Scroll to the bottom of the package list and select Extras > Google Repository. The package is downloaded to your computer and installed in your SDK environment at android-sdk-folder/extras/google/google_play_services.

This guide is written for users of Android Studio, which is the recommended development environment.

Get a Configuration File

The configuration file provides service-specific information for your app. Go to Google Developer’s Page, and create a New Project or choose an Existing Project. You will also need to provide a package name for your app.

Before this, we MUST have an Android Project that we are using.  If you don’t have one, create a new project in android studio project, Name the project Demo Login and give it a package name. Choose the activity name as LoginActivity.

Now add app name and package name on Google Developers page as shown below.

Click on Choose and configure services button. Choose Google Sign-In the service page.

We will continue on this page, but first, we have to generate digitally signed a public certificate which will be.

Generate SHA-1 fingerprint

In order to consume google plus services, first we need to enable the Google Plus API on google console and we need to register our digitally signed .apk file’s public certificate in the Google APIs Console.

Java key tool an be used to generate the SHA-1 fingerprint. We had used this earlier, in our Login with Facebook Tutorial, so you should already be familiar with the process.

Generating a Development Key Hash

You’ll have a unique development key hash for each Android development environment. To generate a development key hash, on Mac, run the following command:


On Windows, run this command:

This command will generate a 28-character key hash unique to your development environment.

Generating a Release Key Hash

Android apps must be digitally signed with a release key before you can upload them to the store. To generate a hash of your release key, run the following command on Mac or Windows substituting your release key alias and the path to your keystore:


This will generate a 28-character string.

Copy the SHA-1 ID generated in your terminal as image below

Enter the SHA-1 ID to the Google developer’s page.

Click on ENABLE SIGN IN button.

Click on CONTINUE TO GENERATE CONFIGURATION FILE button. This will open download and install configuration page, click on Download google-services.json button

Copy the google-services.json file you just downloaded into the app directory of your Android Studio project.

Add Google Services Plugins and Google Play Services

The Google Services plugin for Gradle parses configuration information from the google-services.json file. Add the plugin to your project by updating your top-level build.gradle and your app-level build.gradle files as follows:

Add the dependency to your project-level build.gradle:

Add the plugin to your app-level build.gradle:

In your app-level build.gradle file, declare Google Play Services as a dependency:

We also need to add Glide, to be able to load our user’s profile image with ease.

Remember to add the Internet permission in your Manifest File!

Integrating Google Sign-In

To integrate Google Sign-In into your Android app, configure Google Sign-In and add a button to your app’s layout that starts the sign-in flow. Create a simple User Interface that looks like this:

Lets go ahead and start doing some magic to make our UI lively.

Configure Google Sign-In and the GoogleApiClient object

In your sign-in activity’s onCreate method, configure Google Sign-In to request the user data required by your app. For example, to configure Google Sign-In to request users’ ID and basic profile information, create aGoogleSignInOptions object with the DEFAULT_SIGN_IN parameter. To request users’ email addresses as well, create the GoogleSignInOptions object with the requestEmail option.

Then, also in your sign-in activity’s onCreate method, create a GoogleApiClient object with access to the Google Sign-In API and the options you specified.

Note: To use enableAutoManage, your activity must extend FragmentActivity or AppCompatActivity (a subclass of FragmentActivity), both of which are part of the Android Support Library. You can use GoogleApiClient in a Fragment; however, the fragment’s parent activity must be a FragmentActivity. If you can’t extendFragmentActivity, you must manually manage the GoogleApiClient connection lifecycle.

Start the sign-in flow

In the activity’s onClick method, handle sign-in button taps by creating a sign-in intent with the getSignInIntent method, and starting the intent with startActivityForResult.

Starting the intent prompts the user to select a Google account to sign in with. If you requested scopes beyond profileemail, and openid, the user is also prompted to grant access to the requested resources.

In the activity’s onActivityResult method, retrieve the sign-in result with getSignInResultFromIntent.

After you retrieve the sign-in result, you can check if sign-in succeeded with the isSuccess method. If sign-in succeeded, you can call the getSignInAccount method to get a GoogleSignInAccount object that contains information about the signed-in user, such as the user’s name.

You can also get the user’s email address with getEmail, the user’s Google ID (for client-side use) with getId, and an ID token for the user with with getIdToken. If you need to pass the currently signed-in user to a backend server, send the ID token to your backend server and validate the token on the server.

This is how our app will look like initially.

After clicking the Sign In button, the user is requested to choose the account they would like to use:

Retrieve profile information for a signed-in user

Use the GoogleSignInResult.getSignInAccount method to request profile information for the currently signed in user.

You can call the getSignInAccount method after the sign-in intent succeeds.

Note: A Google account’s email address can change, so don’t use it to identify a user. Instead, use the account’s ID, which you can get on the client with GoogleSignInAccount.getId, and on the backend from the sub claim of the ID token.

For additional profile data that might be available, see GoogleSignInAccount. Note that any of the profile fields can be null, depending on which scopes you requested and what information the user’s profile includes.

This is the full source code for the demo app, Login Activity java class.

Sign out users

You can enable your users to sign out of your app, and to disconnect their accounts from your app entirely. To add a sign out button to your app, first create a button in your app to act as your sign out button. Then, attach an onClickListener to the button and configure the onClick method to call signOut.

This code clears which account is connected to the app. To sign in again, the user must choose their account again. Try this in your app, to see how much you have learnt in this article.

Conclusion

Your app is now complete. When you build it and deploy it on your Android device, you will see the Google login button. Tapping the login button prompts you to choose and authorize which account to use, if you have multiple accounts signed in the device. After successfully logging in, the user’s name and profile image will be displayed.

Remember to subscribe to get updates instantly and also leave questions or comments below, and I will be glad to help you.

Happy Coding!

2 Replies to “Google Login in Android”

Leave a Reply

Your email address will not be published. Required fields are marked *