Integrating Google Sign-In into Your Android App
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 Developers Console project and set up your Android Studio project.
Add the configuration file to your project
Copy the google-services.json file you just downloaded into the app/ or mobile/ directory of your Android Studio project. Open the Android Studio Terminal pane:
$ mv path-to-download/google-services.json app/
Add the Google Services plugin
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:classpath 'com.google.gms:google-services:2.0.0-alpha6' 
- Add the plugin to your app-level build.gradle:apply plugin: 'com.google.gms.google-services' 
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.If you need to request additional scopes to access Google APIs, specify them with requestScopes.
- 
Then, also in your sign-in activity's Note: To useonCreatemethod, create aGoogleApiClientobject with access to the Google Sign-In API and the options you specified.enableAutoManage, your activity must extendFragmentActivityorAppCompatActivity(a subclass ofFragmentActivity), both of which are part of the Android Support Library. You can useGoogleApiClientin aFragment; however, the fragment's parent activity must be aFragmentActivity. If you can't extendFragmentActivity, you must manually manage theGoogleApiClientconnection lifecycle.
Add the Google Sign-In button to your app
- 
![The standard Google sign-in button]() Add the Add theSignInButtonin 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 and color scheme with the setSizeandsetScopesmethods. Also, if you specify a Google+ social scope tosetScopes, the sign-in button will be rendered with the red Google+ branding.
- 
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
- 
![Image of the Sign-In account chooser]() In the activity's In the activity'sonClickmethod, 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;
 // ...
 }
 }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.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.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 Developers 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>.
 
                     
                    
                 
                    
                 Add the
Add the In the activity's
In the activity's 
                
            
         
 
         浙公网安备 33010602011771号
浙公网安备 33010602011771号