In this codelab, You'll learn how to quickly add a Google Sign-in button to your project

What you'll learn

What you'll need

We will use a sample project to demonstrate a simple implementation of the Google sign-in feature. You can download the project as a zip file

Download Zip

Or you can clone the project from GitHub with the below command:

git clone google-signin

The sample project contains 3 android application modules:

The Google Sign-in service requires the SHA-1 of your signing certificate for it to be able to authenticate your app. You are also required to provide the package and name of your app. If you are running in debug mode as per this code lab. We will use the debug SHA1 key that you can get by running the keytool command on your command line as below:

On Mac/Linux (password is android)
 keytool -exportcert -list -v -alias androiddebugkey -keystore  ~/.android/debug.keystore 
On Windows (password is android)
keytool -exportcert -list -v -alias androiddebugkey -keystore  %USERPROFILE%\.android\debug.keystore
if you are signing a release version of your app then you would need to create your own keystore and then get the SHA1 as below:
keytool -exportcert -list -v -alias   -keystore 

Once you have your SHA1 key you can now generate the play services configuration file that you will then add to your app. This is used to communicate with the play services api within your app. Google makes this simple by providing a plugin and configuration file that will handle this configuration for you.

Get the configuration file

Add the Google Services plugin

Once you have the google-services.json file, add it to your app in the checkpoint 1 module as shown below.

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

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

      apply plugin: ''
declare the google play services plugin as a dependency in your app.

      apply plugin: ''
      dependencies {
          compile ''

We will continue with the changes your made to the checkpoint_1.

Since we now have the play services configuration file in place , we will now configure the client that we will use to communicate with the play services. Open MainActivity and make the below -changes to declare a GoogleApiClient create a GoogleSignInOptions object inorder for you to request the users email,id and basic profile info as below.

      GoogleSignInOptions options = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
now instantiate the Apiclient with the options object:

      googleClient = new GoogleApiClient.Builder(this)
                                        .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                                        .addApi(Auth.GOOGLE_SIGN_IN_API, options)

Now that we have setup the client, we need to handle the click events for this demo, we want to be able to signin by clicking on the sign-in button. We will then change this to signout if the user is logged in. Create a click listener for your button as below so it calls a method called signin. Create a flag to check if user is loggedin or not. When logged in we then call the signout method.

Your checkpoint_1 module should now look exactly as the checkpoint_2 module.

To handle Sign in, we will call the sign--in intent. this will call the account selection list , where the user selects the account they would like to use. We would then have to handle this in onActivityResult of our MainActivity. Change your signin method to look like the below implementation:

      private void signIn() {
          Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(client);
          startActivityForResult(signInIntent, SIGNIN_REQUEST_CODE);
This shows a progress indicator and starts the account selection activity. We can then handle the result from this call as below in OnActivityResult:

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SIGNIN_REQUEST_CODE) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);

    private void handleResult(GoogleSignInResult result) {
        if (result.isSuccess()) { //signed in
            GoogleSignInAccount acct = result.getSignInAccount();
            message.setText(acct.getDisplayName() + "\n" + acct.getEmail());
        } else { //signed out

    void updateButton(boolean loggedIn){
        this.loggedIn = loggedIn;
        if(loggedIn) {
First we check that the result is from the signin request then we handle it in the handleResult method. In the handleResult method, we check if signin was successful and change the button text to "SignOut". At this point we get the user details as requested when we defined the GoogleApiClient and the GoogleSignInOptions.

Change your signout method to look as below:

    private void signOut() {
                new ResultCallback() {
                    public void onResult(Status status) {
Sign-Out is a bit different from signin. it has a ResultCallback where you handle the result of the sign out request. If the sign out request is successfull then we update the button to show the "Sign-In" text again.

You have now completed the signin and signout implementation. There are a few more things you can update in the app.