Google Signing using Firebase Authentication in Kotlin
Last Updated :
23 Jul, 2025
Firebase is a mobile and web application development platform. It provides services that a web application or mobile application might require. Firebase provides email and password authentication without any overhead of building the backend for user authentication. Google Sign-In is a secure way to authenticate users in your apps. It reduces the hassle of dealing with and handling those extra passwords by the user to get authenticated to the app. Firebase offers a great number of options to implement Login in your app like Email, Phone number, Google, Facebook, etc.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Note that select Java as the programming language.
Step 2: Connect your app to Firebase
After creating a new project in Android Studio. Connect your app to Firebase. For connecting your app to firebase. Navigate to Tools on the top bar. After that click on Firebase. A new window will open on the right side. Inside that window click on Authentication and then email and password authentication.
After clicking on email and password authentication you will get to see the below screen. Inside this screen click on the first option connect to firebase and after that click on the second option to add Firebase authentication to your app.
Add the following dependencies if it is not already present there.
dependencies {
...
implementation ("com.google.firebase:firebase-auth:23.2.0")
implementation ("com.google.android.gms:play-services-auth:21.3.0")
}
Step 3: Enable Google Sign In
Step 4: Get Web Client id
Click on the Google method in Sign-in Method again, scroll down and under Web SDK configuration copy the Web client id and add it in strings.xml file
<resources>
...
<string name="default_web_client_id">add your web client id</string>
</resources>
Step 5: Add SHA1 and SHA256 fingerprint
Now, in the Firebase Console, navigate to the project settings by selecting on the Gear icon on the top-left corner of the screen. Then, under General, scroll down and under Your apps, select Add fingerprint and paste both the keys one by one.
Step 6: Get google-services.json file
In the same section as the previous step, there is an option to download the google-services.json file. After the downloading is complete, paste the file in your android studio project inside the app folder under Project section.
Step 7: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/Signin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:padding="20dp"
android:text="Google Sign In"
android:textColor="#000"
android:textSize="22sp" />
</LinearLayout>
Step 8: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.tasks.Task
import com.google.firebase.FirebaseApp
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.GoogleAuthProvider
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
lateinit var mGoogleSignInClient: GoogleSignInClient
val Req_Code: Int = 123
private lateinit var firebaseAuth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
FirebaseApp.initializeApp(this)
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
mGoogleSignInClient = GoogleSignIn.getClient(this, gso)
firebaseAuth = FirebaseAuth.getInstance()
Signin.setOnClickListener { view: View? ->
Toast.makeText(this, "Logging In", Toast.LENGTH_SHORT).show()
signInGoogle()
}
}
private fun signInGoogle() {
val signInIntent: Intent = mGoogleSignInClient.signInIntent
startActivityForResult(signInIntent, Req_Code)
}
// onActivityResult() function : this is where
// we provide the task and data for the Google Account
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == Req_Code) {
val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(data)
handleResult(task)
}
}
private fun handleResult(completedTask: Task<GoogleSignInAccount>) {
try {
val account: GoogleSignInAccount? = completedTask.getResult(ApiException::class.java)
if (account != null) {
UpdateUI(account)
}
} catch (e: ApiException) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show()
}
}
// this is where we update the UI after Google signin takes place
private fun UpdateUI(account: GoogleSignInAccount) {
val credential = GoogleAuthProvider.getCredential(account.idToken, null)
firebaseAuth.signInWithCredential(credential).addOnCompleteListener { task ->
if (task.isSuccessful) {
SavedPreference.setEmail(this, account.email.toString())
SavedPreference.setUsername(this, account.displayName.toString())
val intent = Intent(this, DashboardActivity::class.java)
startActivity(intent)
finish()
}
}
}
override fun onStart() {
super.onStart()
if (GoogleSignIn.getLastSignedInAccount(this) != null) {
startActivity(
Intent(
this, DashboardActivity
::class.java
)
)
finish()
}
}
}
Step 9: Create a new empty activity
Please refer to Create New Activity in Android Studio and name the activity as DashboardActivity.
Step 10: Working with the activity_dashboard.xml file
Go to the activity_dashboard.xml file and refer to the following code. Below is the code for the activity_dashboard.xml file. Create a Button which when clicked, logs out the user from the app.
XML
<?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=".DashboardActivity">
<Button
android:id="@+id/logout"
android:layout_width="300dp"
android:layout_height="100dp"
android:padding="10dp"
android:text="Logout"
android:textColor="#000"
android:textSize="40sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 11: Working with the DashboardActivity.kt file
Go to the DashboardActivity.kt file and refer to the following code. Below is the code for the DashboardActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
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.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.activity_dashboard.*
class DashboardActivity : AppCompatActivity() {
// declare the GoogleSignInClient
lateinit var mGoogleSignInClient: GoogleSignInClient
private val auth by lazy {
FirebaseAuth.getInstance()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dashboard)
// call requestIdToken as follows
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
mGoogleSignInClient = GoogleSignIn.getClient(this, gso)
logout.setOnClickListener {
mGoogleSignInClient.signOut().addOnCompleteListener {
val intent = Intent(this, MainActivity::class.java)
Toast.makeText(this, "Logging Out", Toast.LENGTH_SHORT).show()
startActivity(intent)
finish()
}
}
}
}
Output:
Similar Reads
Google SignIn using Firebase Authentication in ReactJS Firebase simplifies mobile and web app development by offering pre-built features like user authentication (email/password, Google Sign-In, etc.) without the need to build complex backends. This saves time and resources for developers.In this article, we will discuss about the Google Sign-In feature
5 min read
Google Signing using Firebase Authentication in Android Firebase is a mobile and web application development platform. It provides services that a web application or mobile application might require. Firebase provides email and password authentication without any overhead of building the backend for user authentication. Google Sign-In is a secure way to
8 min read
Google Authentication with Firebase Google Authentication, a method of verifying user identities using Google credentials, provides a seamless and secure way for users to sign in to applications. With the help of Firebase, developers can integrate Google Authentication into their apps and allowing users to sign in with their existing
5 min read
How to authenticate with google using firebase in React ? The following approach covers how to authenticate with Google using firebase in react. We have used firebase module to achieve so.Creating React Application And Installing Module:Step 1: Create a React myapp using the following command:npx create-react-app myappStep 2: After creating your project fo
2 min read
User authentication using Firebase in Android Firebase is a platform that helps developers build mobile and web apps. It provides useful tools like databases, cloud storage, and hosting. One of its main features is email and password login, so developers donât have to create their own system for user authentication. This makes app development e
7 min read
How to Use Authentication Console in Firebase? Almost all apps need to have some permission system in place. In some circumstances, checking a username/password pair against our Users table is sufficient. Still, frequently, we require a more detailed permissions model to grant specific users access to particular resources while preventing them f
4 min read