In this article, we will learn about Authentication- integrating Google SignIn in Android Application using firebase. If you want to integrate Google SignIn into your Android app, you’ve come to the right place.
People seem to hate filling out forms and remembering passwords, so including a Google SignIn in your Android application is a great idea. So, if you have a Google SignIn option in your Android app, it makes it easier for your users to SignUp.
If you don’t know what is firebase then please refer to What is Firebase | All about Firebase.
Output:
Step by step exaplanation:
Step 1: Go to the Main menu and select “Tools” and after that select “Firebase”.
Step 2: Select Authentication as shown in above figure.
Step 3: Select the option- “Authentication using Google SignIn Kotlin”.

Step 4: Connect your app to firebase: First.
- After that Add the Firebase Authentication SDK to your app as shown in figure given below:

Step 5: Go to Firebase console: Enable Google Authentication as shown in below:

Step 6: Enable it, select a email address and click save as shown in figure below:

Step 7: Open General setting and paste SHA key as shown below:

To generate SHA key : Go to the Android Studio Project and press ctrl 2 times and Run Anything pop up in this pop-up type – signingReport and Enter.
dependency required in Google Signin
Step 8: Add this to your file build.gradle(Module:app) :
| //Firebase implementation platform(‘com.google.firebase:firebase-bom:28.4.0’) implementation ‘com.google.firebase:firebase-auth-ktx’ implementation ‘com.google.android.gms:play-services-auth:19.2.0’ // we use Coroutines also in this project to improve performance. implementation ‘org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.1.1’ implementation ‘org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8’ implementation ‘org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0’ |
Plugin required in Google Sign in
Add this to your file build.gradle(Module:app):
| id ‘com.google.gms.google-services’ |
Add dependencies in build.gradle(Project:app)
| classpath ‘com.google.gms:google-services:4.3.10’ |
Finally, your build.gradle(Module:app) file code look like:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.gms.google-services'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.sagar.googlesignup"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'com.google.firebase:firebase-auth-ktx:21.0.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
//Firebase
implementation platform('com.google.firebase:firebase-bom:28.4.0')
implementation 'com.google.firebase:firebase-auth-ktx'
implementation 'com.google.android.gms:play-services-auth:19.2.0'
//Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.1.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'
}
and build.gradle(Project:app) code look like:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.5.10"
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.10'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Step 9: In activity_main.xml file add the code given below:
<?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=".SignIn">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="DevelopersDome"
android:textSize="30sp"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center_horizontal" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 10: In MainActivity.kt file keep it is as default:
package com.sagar.googlesignup
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Step 11: Create a New Activity(Empty activity) and name it SignIn and add the code given below in your activity_sign_in.xml file.
<?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"
android:background="@color/white"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="116dp"
android:src="@drawable/download"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.gms.common.SignInButton
android:id="@+id/signInButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="115dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="71dp"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="@+id/signInButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 12: In SignIn.kt file add the code given below:
package com.sagar.googlesignup
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ProgressBar
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.SignInButton
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.tasks.Task
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.GoogleAuthProvider
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.withContext
class SignIn : AppCompatActivity() {
private val RC_SIGN_IN: Int = 123
private val TAG = "DevelopersDome"
private lateinit var auth: FirebaseAuth
private lateinit var googleSignInClient: GoogleSignInClient
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sign_in)
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
googleSignInClient = GoogleSignIn.getClient(this, gso)
auth = Firebase.auth
findViewById<SignInButton>(R.id.signInButton).setOnClickListener {
signIn()
}
}
private fun signIn() {
val signInIntent = googleSignInClient.signInIntent
startActivityForResult(signInIntent, RC_SIGN_IN)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
handleSignInResult(task)
}
}
private fun handleSignInResult(completedTask: Task<GoogleSignInAccount>) {
try {
val account =
completedTask.getResult(ApiException::class.java)!!
Log.d(TAG, "firebaseAuthWithGoogle:" + account.id)
firebaseAuthWithGoogle(account.idToken!!)
} catch (e: ApiException) {
Log.w(TAG, "signInResult:failed code=" + e.statusCode)
}
}
private fun firebaseAuthWithGoogle(idToken: String) {
val credential = GoogleAuthProvider.getCredential(idToken, null)
findViewById<SignInButton>(R.id.signInButton).visibility = View.GONE
findViewById<ProgressBar>(R.id.progressBar).visibility = View.VISIBLE
GlobalScope.launch(Dispatchers.IO) {
val auth = auth.signInWithCredential(credential).await()
val firebaseUser = auth.user
withContext(Dispatchers.Main) {
updateUI(firebaseUser!!)
}
}
}
private fun updateUI(firebaseUser: FirebaseUser?) {
if (firebaseUser != null) {
val mainActivityIntent = Intent(this, MainActivity::class.java)
startActivity(mainActivityIntent)
finish()
} else {
findViewById<SignInButton>(R.id.signInButton).visibility = View.VISIBLE
findViewById<ProgressBar>(R.id.progressBar).visibility = View.GONE
}
}
}
Run the application now. You will get the output shown in the output section.
Source Code:
We’ve completed the development of Google SignIn using Firebase in the Android application. You can get the Source code from the given below button.
- We hope that this guide will assist you in understanding all about the concepts of Google SignIn using firebase in android application. We have concentrated on making a basic, meaningful and easy-to -learn guide to the concepts of Google SignIn using firebase with suitable examples. Still if you have any problems regarding this, please post them in the comments section, we will be glad to assist you.


Pingback: What is Firebase | All about Firebase - Developers Dome
Thanks you for writing this blog, it helped me to my app authentication.
Pingback: Email Authentication Using Firebase in Android - Developers Dome
Pingback: Building Real-time Chat App in Android using Firebase - Developers Dome