Deploy your apps to a supercloud in a few clicks

This Engineering Education program is supported by Section. Instantly deploy your GitHub apps, Docker containers or K8s namespaces to a supercloud.

Try It For Free

Google Authentication in Android Applications

October 16, 2020

Authentication enables organizations to keep their networks secure by permitting only authenticated users (or processes) to access its protected resources. This shows how important it is to authenticate users. User authentication can be done in various ways.

Google authentication in Android applications

This guide will go over integrating Google authentication in Android applications.


Authentication is the process or action of verifying the identity of a user or process.

Third-party authentication and filling forms are the most popular. In 2019, one of the top ten user experience (UX) trends was using third-party authentication options like Google, Facebook, Twitter, GitHub, etc. This was due to the minimized effort and time required to authenticate a user.


  • Authentication - The process or action of verifying the identity of a user or process.
  • Firebase - A platform developed by Google used to create mobile and web applications. For more information on Firebase check this article out.
  • Dependency - A platform developed by Google used to create mobile and web applications.
  • Json - (JavaScript Object Notation) is a lightweight data-interchange format.
  • FirebaseAuth - Provides backend services, SDKs, and UI libraries to authenticate users to your application.


Users should ideally have a:

Step 1 – Create a new project

In this step, we will create a new Android Studio project.

  • Open Android Studio. Select Start a new Android Studio project and click on next.

Open Android Studio

  • Select Empty Activity and click on next.

Select on Empty Activity

  • We will name the project GoogleSignUp. Click on Finish and wait for the project to build.

Empty Activity

Step 2 – Creating the Google sign up button

In this step, we will add a Google sign in button to our activity’s resource file.

Note: We will use an optional pre-built button. The button can still be customized according to the developer’s preference.

Add the following dependency in your app module build.gradle file:

dependencies {
  implementation 'com.shobhitpuri.custombuttons:google-signin:1.1.0'

Click on Sync Now.

In your XML layout, add the button.

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=""
app:layout_constraintTop_toTopOf="parent" />


Note: Type app:isDarkTheme="{Boolean}": To switch between blue theme and a gray-white theme for the button. The library will handle the changing of text color and background color. It also handles the change of color on button press or button clicks.

Layout Resource File

Step 3 - Setting up Firebase

Set up and connect your project on Firebase by following instructions stated by Google here.

Step 4 – Lets Code

Add the following dependencies to your app module build.gradle and sync the project.

  implementation ''
  implementation ''

In our we will add the following lines of code.

First, initialize the views for the authentication process.c

//Initializing the views required for the Authentication process.
private GoogleSignInClient mGoogleSignInClient;
private FirebaseAuth firebaseAuth;
private Button googleSignIn;
private int RC_SIGN_IN = 1

In the activity’s onCreate method, initiate FirebaseAuth that is used to Authenticate users.

//Initializing Auth
firebaseAuth = FirebaseAuth.getInstance();

Next, configure Google Sign In to request the data required by the app. We will create the GoogleSignInOptions object with the requestEmail option.

// Configure sign-in to request the user's ID, email address, and basic under details.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)

Next, we will create a GoogleSignInClient object.

// Build a GoogleSignInClient with the options specified by gso.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)

Note: The R.string.default_web_client_id is automatically added once you build and run the application on a device or emulator.

Set an onClick listener on the Google Sign Up button which will pass the SignIn method.

googleSignIn = (Button) findViewById(;
        googleSignIn.setOnClickListener(new View.OnClickListener()
         googleSignIn = (Button) findViewById(;

Get the GoogleSignInAccount object for the user in the onActivityResult method.

    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
        super.onActivityResult(requestCode, resultCode, data);
        //Google Sign Up
        if(requestCode ==RC_SIGN_IN)
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);



Note: The GoogleSignInAccount object contains information about the signed-in user.

Next, create a handleSignInResult method that will give out instructions on actions to be done if the process is successful. For instance, we would like to notify a user whether they have signed in or not, and if successful they should be able to access the next activity.

    private void handleSignInResult(Task<GoogleSignInAccount> completedTask)
        try {
            GoogleSignInAccount account = completedTask.getResult(ApiException.class);
        } catch (ApiException e) {
            Toast.makeText(MainActivity.this,"Sign In Failed",Toast.LENGTH_SHORT).show();


Afterwards, we create a FirebaseGoogleAuth method to receive an ID token from the GoogleSignInAccount object. Exchange it for a Firebase credential, that enables us to authenticate with Firebase.

    private void FirebaseGoogleAuth(GoogleSignInAccount account)
        //check if account is null
        if (account != null)
            AuthCredential authCredential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
            firebaseAuth.signInWithCredential(authCredential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
                public void onComplete(@NonNull Task<AuthResult> task)
                    if (task.isSuccessful())
                        Toast.makeText(MainActivity.this, "Authentication Succesful", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(MainActivity.this, Home.class));

                    } else {
                        Toast.makeText(MainActivity.this, "Authentication Failed", Toast.LENGTH_SHORT).show();
                        FirebaseUser user = firebaseAuth.getCurrentUser();
            Toast.makeText(MainActivity.this, "acc failed", Toast.LENGTH_SHORT).show();

Let’s create an UpdateUI method that will be used to display the UI for the Google account options.

 private void updateUI(FirebaseUser fUser)
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(getApplicationContext());
        if (account != null)
            String personName = account.getDisplayName();
            String personEmail = account.getEmail();


We are done! Let’s run the app to test it out.

Screen one


Screen two


Screen three


You can access the full source code here.

You can download the application here.

Peer Review Contributions by: Linus Muema