Gmail Integration in Android using Firebase

Gmail Integration in Android using Firebase: Full Guide (2026)

Gmail Integration in Android using Firebase: Full Working Guide

Letting users sign in with their Google account is one of the fastest ways to get them into your app. No new password to remember, no email verification to wait for. Firebase makes this easy by handling the credential exchange, and Google Sign-In provides the account chooser that users already trust. In this guide, I will walk you through the entire Gmail integration in Android using Firebase — Gradle setup, layouts, manifest, and the full working Java code for sign-in, profile display, and logout.

What is Gmail integration in Android?

Gmail integration means letting your users sign into your Android app using their Google account. Instead of typing a new username and password, they tap a button, pick their Google account from a list, and they are in.

Behind the scenes, Firebase Authentication takes the ID token from Google and creates a Firebase user. Your app gets back a FirebaseUser object with the user's name, email, photo URL, and unique user ID.

The whole flow has four steps:

  1. The user taps the Google Sign-In button
  2. Google shows the account chooser
  3. Firebase exchanges the Google token for a Firebase session
  4. Your app navigates to the home screen with the user's profile

Let's build all of it step by step.

What the output looks like

Here is what you get at the end:

  • A sign-in screen with the standard Google Sign-In button
  • After sign-in, a home screen showing the user's profile photo, name, email, UID, and provider
  • A logout button that signs the user out of both Firebase and Google

The screens are simple but complete. You can style them however you like after the functionality works.

Add dependencies in Gradle

Open your app-level build.gradle file and add these lines inside the dependencies block:

implementation 'com.google.firebase:firebase-auth:19.3.2'
implementation 'com.google.android.gms:play-services-auth:18.1.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

What each one does:

  • firebase-auth — Firebase Authentication, the core of the login
  • play-services-auth — Google Sign-In, which shows the account chooser
  • glide — loads the user's profile photo from a URL into an ImageView
  • glide compiler — the annotation processor Glide needs at build time

You also need the Google Services plugin at the project level, and your google-services.json file from the Firebase console placed in the app/ directory. Without that file, Firebase will not work.

activity_main.xml — the sign-in screen

The main activity layout has just one element: the Google Sign-In button.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.gms.common.SignInButton
        android:id="@+id/google_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

The SignInButton is provided by Google Play Services. It already has the correct branding and styling, so you do not have to build it yourself. The constraint attributes center it horizontally and push it near the top of the screen.

activity_home_page.xml — the profile screen

The home page layout shows the profile photo, user details, and a logout button.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher_background"
        android:scaleType="centerCrop"
        android:layout_gravity="center"
        android:id="@+id/myimage" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/myid"
        android:layout_marginTop="20dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Logout"
        android:onClick="logout" />

</LinearLayout>

Note the button's android:onClick="logout". This tells Android to call a public method called logout(View) in the activity when the button is tapped. We will write that method in HomePage.java.

AndroidManifest.xml setup

The manifest needs the internet permission and the name of your custom Application class.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.firebasedemo">

    <uses-permission-sdk-23 android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:name=".SingleTask"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".HomePage"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Two important points:

  • android:name=".SingleTask" on the <application> tag tells Android to use your custom Application class
  • MainActivity is the launcher activity — this is where the sign-in button lives

SingleTask.java — the Application class

The Application class runs once when your app starts. It is the perfect place to build the GoogleSignInClient, because you only need to build it one time and then share it with any activity that needs it.

package com.example.firebasedemo;

import android.app.Application;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;

public class SingleTask extends Application {
    private GoogleSignInClient mGoogleSignInClient;

    @Override
    public void onCreate() {
        super.onCreate();
        GoogleSignInOptions gso = new GoogleSignInOptions
                .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();
        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
    }

    public GoogleSignInClient getmGoogleSignInClient() {
        return mGoogleSignInClient;
    }
}

Let's look at the important parts:

  • GoogleSignInOptions.DEFAULT_SIGN_IN — the standard sign-in configuration
  • .requestIdToken(getString(R.string.default_web_client_id)) — asks Google for an ID token, which Firebase needs
  • .requestEmail() — asks for the user's email along with the basic profile
  • GoogleSignIn.getClient(this, gso) — builds the client that activities will use

The default_web_client_id string is generated automatically by the Google Services plugin when you add google-services.json. You do not need to write it yourself.

Having a getter method means any activity can call ((SingleTask) getApplication()).getmGoogleSignInClient() to get the shared client.

MainActivity.java — Google sign-in

This is the biggest class. It shows the sign-in button, launches the account chooser, and exchanges the Google token for a Firebase session.

package com.example.firebasedemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;

import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
import com.google.android.gms.common.SignInButton;

public class MainActivity extends AppCompatActivity {
    private FirebaseAuth mAuth;
    private SingleTask singleTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAuth = FirebaseAuth.getInstance();

        singleTask = (SingleTask) getApplication();

        SignInButton signbutton = findViewById(R.id.google_button);
        signbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                signIn();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        FirebaseUser user = mAuth.getCurrentUser();
        if (user != null) {
            goToHomePage();
        }
    }

    private void signIn() {
        Intent signInIntent = singleTask.getmGoogleSignInClient()
                .getSignInIntent();
        startActivityForResult(signInIntent, 1001);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1001) {
            Task<GoogleSignInAccount> task = GoogleSignIn
                    .getSignedInAccountFromIntent(data);
            try {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = task.getResult(ApiException.class);
                firebaseAuthWithGoogle(account.getIdToken());
            } catch (ApiException e) {
                // Google Sign In failed, update UI appropriately
                Log.w("error", "Google sign in failed", e);
            }
        }
    }

    private void firebaseAuthWithGoogle(String idToken) {
        AuthCredential credential = GoogleAuthProvider
                .getCredential(idToken, null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            goToHomePage();
                        } else {
                            Log.w("error", "signInWithCredential:failure",
                                    task.getException());
                        }
                    }
                });
    }

    private void goToHomePage() {
        Intent in = new Intent(MainActivity.this, HomePage.class);
        startActivity(in);
        finish();
    }
}

Let's walk through the flow:

onCreate

We get the FirebaseAuth instance and the shared SingleTask application. We set a click listener on the Google Sign-In button that calls signIn().

onStart

If the user is already signed in (Firebase remembers the session), we skip the login screen and jump straight to the home page. This is what makes the app feel seamless.

signIn

We build the sign-in intent from the GoogleSignInClient and start it with a request code of 1001.

onActivityResult

When the account chooser returns, we check the request code. We get the GoogleSignInAccount from the intent. If it succeeds, we pass the ID token to Firebase.

firebaseAuthWithGoogle

This is the key step. We build an AuthCredential from the Google ID token and call signInWithCredential. Firebase verifies the token, creates or fetches a user, and calls back with success or failure. On success we navigate to the home page.

goToHomePage

We start HomePage and call finish() on MainActivity so the user cannot press Back and return to the login screen.

HomePage.java — profile and logout

The home page shows the user's profile photo and details, and gives them a way to log out.

package com.example.firebasedemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class HomePage extends AppCompatActivity {
    private FirebaseAuth mAuth;
    private SingleTask singleTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);

        ImageView image = findViewById(R.id.imageprofile);
        TextView detail = findViewById(R.id.detail);

        mAuth = FirebaseAuth.getInstance();
        FirebaseUser user = mAuth.getCurrentUser();

        if (user != null) {
            Glide.with(this).load(user.getPhotoUrl()).into(image);
            detail.setText(user.getDisplayName() + "\n" +
                    user.getEmail() + "\n" + user.getUid() + "\n" +
                    user.getProviderId());
        }

        singleTask = (SingleTask) getApplication();
    }

    public void logout(View view) {
        mAuth.signOut();
        singleTask.getmGoogleSignInClient().signOut()
                .addOnCompleteListener(new OnCompleteListener<Void>() {

            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Intent in = new Intent(HomePage.this, MainActivity.class);
                    startActivity(in);
                    finish();
                }
            }
        });
    }
}

What happens here:

onCreate

We get the current FirebaseUser. If the user is not null, we load their photo with Glide and set the TextView to show name, email, UID, and provider. The four pieces of information are joined with newlines so each one appears on its own line.

logout

This is the method called by the button's android:onClick="logout". It does two things:

  1. mAuth.signOut() — signs the user out of Firebase
  2. singleTask.getmGoogleSignInClient().signOut() — signs the user out of Google

Both are necessary. If you only sign out of Firebase, the next time the user taps the login button, Google will automatically pick the same account without showing the chooser. Signing out of Google too forces the chooser to appear again.

After sign-out completes, we navigate back to MainActivity and call finish() so HomePage is removed from the back stack.

Common questions

What is Gmail integration in Android using Firebase?

Gmail integration in Android using Firebase means letting users sign into your app with their Google account. Firebase Authentication handles the credential exchange, and Google Sign-In provides the account chooser UI.

Which Gradle dependencies are needed for Firebase Google Sign-In?

You need firebase-auth, play-services-auth, and Glide for loading the profile image. Add the Glide annotation processor too. Version numbers should match your project's Firebase setup.

Why use the Application class for GoogleSignInClient?

Creating GoogleSignInClient in the Application class means it is built once and shared across all activities. This avoids repeating the setup code and keeps the sign-in client available everywhere in the app.

How do I sign out from both Firebase and Google?

Call mAuth.signOut() to sign out from Firebase, and call getmGoogleSignInClient().signOut() to sign out from Google. Both must be done so the user is fully logged out and the account chooser appears again on next login.

What is default_web_client_id in strings.xml?

default_web_client_id is generated automatically by the Google Services Gradle plugin when you add your google-services.json file. It is the OAuth 2.0 web client ID used to request an ID token during Google Sign-In.

How do I show the user's profile photo and name after login?

After login, FirebaseUser gives you getPhotoUrl(), getDisplayName(), getEmail(), and getUid(). Load the photo with Glide and set the text on a TextView to show the details.

Where to go next

You now have a complete, working Gmail integration in Android using Firebase. You know the Gradle setup, the layouts, the manifest changes, the Application class, the sign-in flow, and the logout flow.

If you remember only one thing, remember this: Firebase does the heavy lifting. Google gives you an ID token. Firebase turns it into a session. Your app just shows the UI and reacts.

The real learning happens when you run this in Android Studio. Set up your Firebase project, add the google-services.json, and run the app on a real device. Sign in, look at the profile details, log out, and sign in again. That is how the concept sticks.

If this helped, bookmark it. Next, try adding Firebase Realtime Database or Firestore to store per-user data after login. That is the natural next step.

3 comments:

CEEcrets said...

Valuable post. If you have a secrets to share then CEEcrets is highly secured Anonymous Social Network Apps for sharing secrets.

CEEcrets said...

Amazing post, CEEcrets is an interesting medium of sharing secrets. It is an Anonymous Social Media Apps for enjoying secrets.

NewzKast said...

It is an informative post.
Now get Trending news celebrity on the go with the best news app NewzKast.