Google Sign-In in Android Studio (Java)
Add "Sign in with Google" to your app and show the user's name, email and photo on a profile screen.
What you'll learn
- Set up Google Sign-In with your app's SHA-1
- Start the sign-in flow
- Read the account name, email and photo
- Sign the user out
Before you start
- Android Studio (any recent version)
- Basic Java: variables, methods and if/else
- A Google Cloud project
- An emulator or an Android phone
How it fits together
- 1Sign in with Google button
- 2Credential Manager sheet
- 3GoogleIdTokenCredential
- 4ProfileActivity
Build it step by step
The video used the old Google Sign-In library, which Google has deprecated. This version uses Credential Manager, the API Google recommends today.
1. Create the OAuth clients
In the Google Cloud console, create:
- an Android OAuth client with your package name and SHA-1 (run
./gradlew signingReportto see it), and - a Web application OAuth client. Copy its client ID.
Also fill in the OAuth consent screen (app name and your email).
2. Create the project
In Android Studio choose New Project → Empty Views Activity, with Language Java and Minimum SDK API 24. Then add these to app/build.gradle.kts inside dependencies { } and press Sync Now:
implementation("androidx.credentials:credentials:1.6.0")
implementation("androidx.credentials:credentials-play-services-auth:1.6.0")
implementation("com.google.android.libraries.identity.googleid:googleid:1.2.1")
implementation("com.github.bumptech.glide:glide:5.0.9")3. Add your Web client ID
Paste the Web client ID (not the Android one) into strings.xml as web_client_id.
4. Ask for a Google account
Build a GetSignInWithGoogleOption with the Web client ID, wrap it in a GetCredentialRequest, and call credentialManager.getCredentialAsync. Android shows its own account sheet.
5. Read the result
The credential is a GoogleIdTokenCredential. It gives you getDisplayName(), getId() (the email) and getProfilePictureUri().
6. Show the profile
ProfileActivity shows the name and email, and loads the photo with Glide, which downloads, caches and crops it into a circle.
7. Sign out
clearCredentialStateAsync makes the app forget the chosen account, so the sheet asks again next time.
8. Run it
Run on a device or emulator with Google Play and a Google account added.
If something goes wrong
- "Sign-in did not work": usually the SHA-1 or package name in the Android client doesn't match, or you pasted the Android client ID instead of the Web one.
- "Add a Google account": the device has no Google account; add one in Settings.
- Keep users signed in: send
getIdToken()to your server or to Firebase Auth. This sample keeps it simple.
What's improved over the video
It moves from the deprecated Google Sign-In library to Credential Manager, handles cancel and no-account cases, and shows the photo in a circle.
Files affected
Check what to add and what to replace before copying code.
- app/build.gradle.ktsAdd these lines
- app/src/main/java/com/example/googlesignin/MainActivity.javaReplace file
- app/src/main/res/layout/activity_main.xmlReplace file
- app/src/main/res/layout/activity_profile.xmlAdd these lines
- app/src/main/AndroidManifest.xmlReplace file
- app/src/main/java/com/example/googlesignin/ProfileActivity.javaAdd these lines
- app/src/main/java/com/example/googlesignin/Screens.javaAdd these lines
- app/src/main/res/values/strings.xmlReplace file
Required setup
These are additions to an existing project—not replacement project files.
Add Credential Manager and Glide
Add these linesWhere: app/build.gradle.kts
Add these lines inside dependencies, then Sync Now.
implementation("androidx.credentials:credentials:1.6.0")
implementation("androidx.credentials:credentials-play-services-auth:1.6.0")
implementation("com.google.android.libraries.identity.googleid:googleid:1.2.1")
implementation("com.github.bumptech.glide:glide:5.0.9")
Application code
Select a file to see its code and where it belongs.
This code uses package com.example.googlesignin. Change that first line to your own package, the one at the top of your MainActivity.
app/src/main/java/com/example/googlesignin/MainActivity.javaOpens the Google account sheet with Credential Manager and reads the result.
package com.example.googlesignin;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.credentials.Credential;
import androidx.credentials.CredentialManager;
import androidx.credentials.CredentialManagerCallback;
import androidx.credentials.CustomCredential;
import androidx.credentials.GetCredentialRequest;
import androidx.credentials.GetCredentialResponse;
import androidx.credentials.exceptions.GetCredentialCancellationException;
import androidx.credentials.exceptions.GetCredentialException;
import androidx.credentials.exceptions.NoCredentialException;
import com.google.android.libraries.identity.googleid.GetSignInWithGoogleOption;
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential;
/**
* "Sign in with Google" using Credential Manager, Google's current sign-in API.
* It shows the system account picker and returns the user's name, email and photo.
*/
public class MainActivity extends AppCompatActivity {
private CredentialManager credentialManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Screens.setUp(this, R.layout.activity_main);
credentialManager = CredentialManager.create(this);
Button signIn = findViewById(R.id.buttonGoogle);
signIn.setOnClickListener(v -> signInWithGoogle());
}
private void signInWithGoogle() {
// The Web client ID comes from the Google Cloud console (see the guide).
GetSignInWithGoogleOption google = new GetSignInWithGoogleOption.Builder(getString(R.string.web_client_id)).build();
GetCredentialRequest request = new GetCredentialRequest.Builder().addCredentialOption(google).build();
credentialManager.getCredentialAsync(this, request, null, ContextCompat.getMainExecutor(this),
new CredentialManagerCallback<GetCredentialResponse, GetCredentialException>() {
@Override
public void onResult(GetCredentialResponse response) {
handle(response.getCredential());
}
@Override
public void onError(@NonNull GetCredentialException e) {
if (e instanceof GetCredentialCancellationException) return; // the user closed the sheet
// No Google account on this device: tell the user how to fix it.
int message = e instanceof NoCredentialException ? R.string.error_no_account : R.string.error_sign_in;
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
});
}
private void handle(Credential credential) {
if (credential instanceof CustomCredential
&& GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL.equals(credential.getType())) {
GoogleIdTokenCredential google = GoogleIdTokenCredential.createFrom(credential.getData());
Intent profile = new Intent(this, ProfileActivity.class)
.putExtra(ProfileActivity.EXTRA_NAME, google.getDisplayName())
.putExtra(ProfileActivity.EXTRA_EMAIL, google.getId())
.putExtra(ProfileActivity.EXTRA_PHOTO, google.getProfilePictureUri() != null ? google.getProfilePictureUri().toString() : null);
// To keep users signed in across launches, send google.getIdToken() to your backend or to Firebase Auth.
startActivity(profile);
} else {
Toast.makeText(this, R.string.error_sign_in, Toast.LENGTH_LONG).show();
}
}
}


