Use our Android sample app to see how Sign-In works, or add Sign-In to your existing app.
Required: The latest versions of Android Studio and Google Play Services.
Get the project
If this is your first time using a Google services sample, check out the google-services repository.
$ git clone https://github.com/googlesamples/google-services.git
Open Android Studio.
Select File > Open, browse to where you cloned the
google-services
repository, and open
google-services/android/signin
.
Configure a Google API Console project
- Open an existing project in the API Console, or create a project if you don't already have one.
- On the OAuth consent screen page, make sure all of the information is complete and accurate.
-
On the Credentials page, create an Android type client ID. The package name for
the sample app is
com
. You will also need to provide the SHA-1 hash from your signing certificate fingerprint. See Authenticating Your Client for information..google .samples .quickstart .signin -
On the Credentials page, create a Web application type client ID. You can leave
the Authorized JavaScript Origins and Authorized redirect URIs fields blank. This web
client ID is required by the sample's
IdTokenActivity
andServerAuthCodeActivity
examples. In a real app, this client ID would represent your app's backend server. -
Copy and paste the client ID into your project's
strings.xml
file:<string name="server_client_id">YOUR_SERVER_CLIENT_ID</string>
Run the sample
Now you're ready to build the sample and run it from Android Studio.
Build the sample and click the run button and select a connected device or emulator with the latest version of Google Play services.
How it works
The application builds a GoogleSignInClient
, specifying
the sign-in options it needs. Then, when the sign-in button is
clicked, the application starts the sign-in intent, which prompts the
user to sign in with a Google account.
// 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();
// Build a GoogleSignInClient with the options specified by gso. mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
private void signIn() { Intent signInIntent = mGoogleSignInClient.getSignInIntent(); startActivityForResult(signInIntent, RC_SIGN_IN); }
Next steps
If you want to see how you can implement Google Sign-In in your own app, take a look at our implementation guide.
Did you have a good experience? Run into trouble? Let us know!