This page refers to a deprecated version of Google Sign-In. New projects should use the current version.

Integrating Google Sign-In into Your Android App

The Google Sign-In button authenticates the user and manages the OAuth 2.0 flow, which simplifies your integration with the Google APIs.

Before you begin

Configure a project and initialize the GoogleApiClient object.

Add the Google Sign-In button to your app

  1. Add the SignInButton in your application's layout:

    <com.google.android.gms.common.SignInButton
        android:id="@+id/sign_in_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
  2. In the Android activity (for example, in the onCreate method), register your button's OnClickListener to sign in the user when clicked:

    findViewById(R.id.sign_in_button).setOnClickListener(this);
    
  3. To use the sign-in button you will need to modify your Activity's sign-in flow. Instead of starting user interaction to resolve errors immediately when your Activity receives an onConnectionFailed callback, your Activity should wait for the user to click the sign-in button.

    /* Is there a ConnectionResult resolution in progress? */
    private boolean mIsResolving = false;
    
    /* Should we automatically resolve ConnectionResults when possible? */
    private boolean mShouldResolve = false;
    
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        // Could not connect to Google Play Services.  The user needs to select an account,
        // grant permissions or resolve an error in order to sign in. Refer to the javadoc for
        // ConnectionResult to see possible error codes.
        Log.d(TAG, "onConnectionFailed:" + connectionResult);
    
        if (!mIsResolving && mShouldResolve) {
            if (connectionResult.hasResolution()) {
                try {
                    connectionResult.startResolutionForResult(this, RC_SIGN_IN);
                    mIsResolving = true;
                } catch (IntentSender.SendIntentException e) {
                    Log.e(TAG, "Could not resolve ConnectionResult.", e);
                    mIsResolving = false;
                    mGoogleApiClient.connect();
                }
            } else {
                // Could not resolve the connection result, show the user an
                // error dialog.
                showErrorDialog(connectionResult);
            }
        } else {
            // Show the signed-out UI
            showSignedOutUI();
        }
    }
    
  4. After the user has clicked the sign-in button, you should set the mShouldResolve flag and attempt to reconnect. This time, the code in onConnectionFailed will attempt to resolve any connection errors that occur. Possible connection errors include prompting the user to select an account, and granting access to your app.

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.sign_in_button) {
            onSignInClicked();
        }
    
        // ...
    }
    
    private void onSignInClicked() {
        // User clicked the sign-in button, so begin the sign-in process and automatically
        // attempt to resolve any errors that occur.
        mShouldResolve = true;
        mGoogleApiClient.connect();
    
        // Show a message to the user that we are signing in.
        mStatus.setText(R.string.signing_in);
    }
    
  5. You should then reset the state of the flags when control returns to your Activity in onActivityResult.

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, "onActivityResult:" + requestCode + ":" + resultCode + ":" + data);
    
        if (requestCode == RC_SIGN_IN) {
            // If the error resolution was not successful we should not resolve further.
            if (resultCode != RESULT_OK) {
                mShouldResolve = false;
            }
    
            mIsResolving = false;
            mGoogleApiClient.connect();
        }
    }
    
  6. When the user has successfully signed in, your onConnected handler will be called. At this point, you are able to retrieve the user’s account name or make authenticated requests. For example, once your GoogleApiClient is connected you can use the Plus.PeopleApi.getCurrentPerson method to retrieve information about the signed-in user.

    @Override
    public void onConnected(Bundle bundle) {
        // onConnected indicates that an account was selected on the device, that the selected
        // account has granted any requested permissions to our app and that we were able to
        // establish a service connection to Google Play services.
        Log.d(TAG, "onConnected:" + bundle);
        mShouldResolve = false;
    
        // Show the signed-in UI
        showSignedInUI();
    }
    

If you need to pass the currently signed-in user to a backend server, send the user's ID token to your backend server and validate the token on the server.

Cross-platform single sign on

When a user runs your app for the first time on the Android device, the GoogleApiClient.connect() method automatically checks if the user previously granted authorization to your app on another platform. This allows the user to be signed in to your app immediately, if your project clients are configured to meet the following requirements:

  • The OAuth 2.0 client IDs must be in the same Google API Console project.
  • The OAuth scopes must be the same in both clients.

If the user signed in to your web app previously, then connect() automatically succeeds and onConnected() is invoked immediately. You can proceed to access Google APIs to retrieve the user’s info and bypass the need for the user to sign in to your app again.

Sign out the user

You can add a sign out button to your app. Create a button in your app to act as your sign out button. Attach an onClickListener to the button and configure the onClick method to disconnect the GoogleApiClient:

@Override
public void onClick(View view) {
  // ...
  if (view.getId() == R.id.sign_out_button) {
    onSignOutClicked()
  }
}

private void onSignOutClicked() {
    // Clear the default account so that GoogleApiClient will not automatically
    // connect in the future.
    if (mGoogleApiClient.isConnected()) {
        Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
        mGoogleApiClient.disconnect();
    }

    showSignedOutUI();
}

This code clears which account is connected to the app. To sign in again, the user would choose their account again.

Localization

The SDK provides localized strings for the com.google.android.gms.common.SignInButton button and these are automatically available to users of your app. To view a full list of languages, you can examine the following directory in the SDK: <android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib/res/. In that location, you will find directories named values-<langcode>.