an Android OAuth client with your package name and SHA-1 (run ./gradlew signingReport to 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:
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.
Opens the Google account sheet with Credential Manager and reads the result.
MainActivity.java
packagecom.example.googlesignin;importandroid.content.Intent;importandroid.os.Bundle;importandroid.widget.Button;importandroid.widget.Toast;importandroidx.annotation.NonNull;importandroidx.appcompat.app.AppCompatActivity;importandroidx.core.content.ContextCompat;importandroidx.credentials.Credential;importandroidx.credentials.CredentialManager;importandroidx.credentials.CredentialManagerCallback;importandroidx.credentials.CustomCredential;importandroidx.credentials.GetCredentialRequest;importandroidx.credentials.GetCredentialResponse;importandroidx.credentials.exceptions.GetCredentialCancellationException;importandroidx.credentials.exceptions.GetCredentialException;importandroidx.credentials.exceptions.NoCredentialException;importcom.google.android.libraries.identity.googleid.GetSignInWithGoogleOption;importcom.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.
*/publicclassMainActivityextendsAppCompatActivity{privateCredentialManager credentialManager;@OverrideprotectedvoidonCreate(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());}privatevoidsignInWithGoogle(){// The Web client ID comes from the Google Cloud console (see the guide).GetSignInWithGoogleOption google =newGetSignInWithGoogleOption.Builder(getString(R.string.web_client_id)).build();GetCredentialRequest request =newGetCredentialRequest.Builder().addCredentialOption(google).build();
credentialManager.getCredentialAsync(this, request,null,ContextCompat.getMainExecutor(this),newCredentialManagerCallback<GetCredentialResponse,GetCredentialException>(){@OverridepublicvoidonResult(GetCredentialResponse response){handle(response.getCredential());}@OverridepublicvoidonError(@NonNullGetCredentialException e){if(e instanceofGetCredentialCancellationException)return;// the user closed the sheet// No Google account on this device: tell the user how to fix it.int message = e instanceofNoCredentialException?R.string.error_no_account :R.string.error_sign_in;Toast.makeText(MainActivity.this, message,Toast.LENGTH_LONG).show();}});}privatevoidhandle(Credential credential){if(credential instanceofCustomCredential&&GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL.equals(credential.getType())){GoogleIdTokenCredential google =GoogleIdTokenCredential.createFrom(credential.getData());Intent profile =newIntent(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();}}}