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

Start Integrating Google Sign-In into Your Android App

Before you can start integrating Google Sign-In in your own app, you must configure a Google API Console project and initialize the GoogleApiClient within your app. The steps on this page do just that. The next steps then describe how to integrate Google Sign-In into your app.

Prerequisites

Google Sign-In for Android has the following requirements:

  • A compatible Android device that runs Android 4.0 or newer and includes the Google Play Store or an emulator with an AVD that runs the Google APIs platform based on Android 4.2.2 or newer and has Google Play services version 15.0.0 or newer.
  • The latest version of the Android SDK, including the SDK Tools component. The SDK is available from the Android SDK Manager in Android Studio.
  • A project configured to compile against Android 4.0 (Ice Cream Sandwich) or newer.
  • The Google Play services SDK:
    1. In Android Studio, select Tools > Android > SDK Manager.
    2. Scroll to the bottom of the package list and select Extras > Google Repository. The package is downloaded to your computer and installed in your SDK environment at android-sdk-folder/extras/google/google_play_services.

This guide is written for users of Android Studio, which is the recommended development environment.

Configure a Google API Console project

To configure a Google API Console project, click the button below, and specify your app's package name when prompted. You will also need to provide the SHA-1 hash of your signing certificate. See Authenticating Your Client for information.

Configure a project

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:

  1. Add the dependency to your project-level build.gradle:
    classpath 'com.google.gms:google-services:3.3.1'
  2. Add the plugin to your app-level build.gradle:
    apply plugin: 'com.google.gms.google-services'

Add Google Play Services

In your app-level build.gradle file, declare Google Play Services as a dependency:

apply plugin: 'com.android.application'
    ...

    dependencies {
        compile 'com.google.android.gms:play-services-identity:15.0.1'
        compile 'com.google.android.gms:play-services-plus:15.0.1'
    }

Declare permissions

In your AndroidManifest.xml, you must request the following permissions to use the noted features:

  • To access Google APIs:

    <uses-permission android:name="android.permission.INTERNET" />
    
  • To retrieve the account name (email) as part of sign-in:

    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    
  • To retrieve OAuth 2.0 tokens or invalidate tokens to disconnect a user's Google account from an app.

    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    

Initialize the GoogleApiClient

The GoogleApiClient object wraps a ServiceConnection to Google Play services; this GoogleApiClient object is used to communicate with the Google Sign-In API and becomes functional after the asynchronous connection has been established with the service, indicating that:

  • Google Play services is running on the device and your Activity has successfully bound the service connection,
  • the user has selected an account that they wish to use with your app, and
  • the user's account has granted the permissions that your app is requesting.

Typically, you want to manage the GoogleApiClient lifecycle in your activity's lifecycle:

  1. Initialize the GoogleApiClient object in your Activity.onCreate handler.
  2. Invoke GoogleApiClient.connect during Activity.onStart .
  3. Invoke GoogleApiClient.disconnect during Activity.onStop .

Your activity will listen for when the connection has established or failed by implementing the ConnectionCallbacks and OnConnectionFailedListener interfaces.

public class MainActivity extends Activity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        View.OnClickListener {

    /* Request code used to invoke sign in user interactions. */
    private static final int RC_SIGN_IN = 0;

    /* Client used to interact with Google APIs. */
    private GoogleApiClient mGoogleApiClient;

    // ...

    @Override
    public void onClick(View v) {
      // ...
    }

}

Put the following code inside your Activity.onCreate method to instantiate the GoogleApiClient. The addScope method shown below allows you to specify your app's initial scopes. Choose one or more login scopes based on the following criteria:

  • If you just need access to basic profile information, set the scope to profile.
  • If you need access to the user's Google account email address, set the scope to email.

Your app can request additional scopes along with the login scope or request them any time after the initial scopes have been granted. See Requesting additional scopes.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Build GoogleApiClient with access to basic profile
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(new Scope(Scopes.PROFILE))
            .addScope(new Scope(Scopes.EMAIL))
            .build();
}

Then, override the Activity.onStart and Activity.onStop methods of your Activity to connect and disconnect the GoogleApiClient:

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
}

Next steps

Now that you have added GoogleApiClient to your activity, you can integrate Google Sign-In into your app. You can also integrate Google services such as Drive and Games into your app by specifying the necessary scopes when you call GoogleApiClient.Builder or by using incremental authorization.