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.
Before you begin
Configure a Google API Console project and set up your Android Studio project.
Configure Google Sign-In and the GoogleApiClient object
-
In your sign-in activity's
onCreatemethod, 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 aGoogleSignInOptionsobject with theDEFAULT_SIGN_INparameter. To request users' email addresses as well, create theGoogleSignInOptionsobject with therequestEmailoption.// Configure sign-in to request the user's ID, email address, and basic // profile. ID and basic profile are included in DEFAULT_SIGN_IN. GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .build();If you need to request additional scopes to access Google APIs, specify them with
requestScopes. -
Then, also in your sign-in activity's
onCreatemethod, create aGoogleApiClientobject with access to the Google Sign-In API and the options you specified.// Build a GoogleApiClient with access to the Google Sign-In API and the // options specified by gso. mGoogleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .build();
Add the Google Sign-In button to your app
-
Add the SignInButtonin 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" /> -
Optional: If you are using the default sign-in button graphic instead of providing your own sign-in button assets, you can customize the button's size with the
setSizemethod.// Set the dimensions of the sign-in button. SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button); signInButton.setSize(SignInButton.SIZE_STANDARD);
-
In the Android activity (for example, in the
onCreatemethod), register your button'sOnClickListenerto sign in the user when clicked:findViewById(R.id.sign_in_button).setOnClickListener(this);
Start the sign-in flow
-
In the activity's onClickmethod, handle sign-in button taps by creating a sign-in intent with thegetSignInIntentmethod, and starting the intent withstartActivityForResult.@Override public void onClick(View v) { switch (v.getId()) { case R.id.sign_in_button: signIn(); break; // ... } }private void signIn() { Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, RC_SIGN_IN); }Starting the intent prompts the user to select a Google account to sign in with. If you requested scopes beyond
profile,email, andopenid, the user is also prompted to grant access to the requested resources. -
In the activity's
onActivityResultmethod, retrieve the sign-in result withgetSignInResultFromIntent.@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); if (requestCode == RC_SIGN_IN) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); handleSignInResult(result); } }After you retrieve the sign-in result, you can check if sign-in succeeded with the
isSuccessmethod. If sign-in succeeded, you can call thegetSignInAccountmethod to get aGoogleSignInAccountobject that contains information about the signed-in user, such as the user's name.private void handleSignInResult(GoogleSignInResult result) { Log.d(TAG, "handleSignInResult:" + result.isSuccess()); if (result.isSuccess()) { // Signed in successfully, show authenticated UI. GoogleSignInAccount acct = result.getSignInAccount(); mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName())); updateUI(true); } else { // Signed out, show unauthenticated UI. updateUI(false); } }You can also get the user's email address with
getEmail, the user's Google ID (for client-side use) withgetId, and an ID token for the user with withgetIdToken. 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.
Cross-platform single sign on
You can use silentSignIn
to automatically sign the user in 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, silent sign-in succeeds. 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.
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>.