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 GoogleSignInClient 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 theDEFAULT_SIGN_IN
parameter. To request users' email addresses as well, create theGoogleSignInOptions
object with therequestEmail
option.// 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
. For the best user experience, on sign-in, only request the scopes that are required for your app to minimally function. Request any additional scopes only when you need them, so that your users see the consent screen in the context of an action they performed. See Requesting Additional Scopes.Then, also in your sign-in activity's
onCreate
method, create aGoogleSignInClient
object with the options you specified.// Build a GoogleSignInClient with the options specified by gso. mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
Check for an existing signed-in user
In your activity's onStart
method, check if a user has already signed in to
your app with Google.
// Check for existing Google Sign In account, if the user is already signed in // the GoogleSignInAccount will be non-null. GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this); updateUI(account);
If GoogleSignIn.getLastSignedInAccount
returns a GoogleSignInAccount
object
(rather than null
), the user has already signed in to your app with Google.
Update your UI accordingly—that is, hide the sign-in button, launch your
main activity, or whatever is appropriate for your app.
If GoogleSignIn.getLastSignedInAccount
returns null
, the user has not yet
signed in to your app with Google. Update your UI to display the Google Sign-in
button.
Add the Google Sign-in button to your app
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" />
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
setSize
method.// Set the dimensions of the sign-in button. SignInButton signInButton = findViewById(R.id.sign_in_button); signInButton.setSize(SignInButton.SIZE_STANDARD);
In the Android activity (for example, in the
onCreate
method), register your button'sOnClickListener
to sign in the user when clicked:findViewById(R.id.sign_in_button).setOnClickListener(this);
Start the sign-in flow
In the activity's
onClick
method, handle sign-in button taps by creating a sign-in intent with thegetSignInIntent
method, 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 = mGoogleSignInClient.getSignInIntent(); 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.After the user signs in, you can get a
GoogleSignInAccount
object for the user in the activity'sonActivityResult
method.@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...); if (requestCode == RC_SIGN_IN) { // The Task returned from this call is always completed, no need to attach // a listener. Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); handleSignInResult(task); } }
The
GoogleSignInAccount
object contains information about the signed-in user, such as the user's name.private void handleSignInResult(Task<GoogleSignInAccount> completedTask) { try { GoogleSignInAccount account = completedTask.getResult(ApiException.class); // Signed in successfully, show authenticated UI. updateUI(account); } catch (ApiException e) { // The ApiException status code indicates the detailed failure reason. // Please refer to the GoogleSignInStatusCodes class reference for more information. Log.w(TAG, "signInResult:failed code=" + e.getStatusCode()); updateUI(null); } }
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 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.