Google Sign-In for Android using Firebase

Ari SWS
DOT Intern
Published in
4 min readOct 24, 2019

--

Well hello people, I just started to learn how to implement Google Auth for my android project using Firebase, and it’s kinda awesome tho. As we know, allowing users to sign-in is a basic requirement in almost every Android Apps. But instead of implementing our own email sign up/authentication we can simply use Google Sign-In method and enjoy all the benefits. In this article, I'll show you how to set up firebase in your android projects, and start using Google Auth for Sign-In terms. Let’s get started!

Setting up Firebase Project

First of all, we need to set up firebase for our project. There are 2 different ways to do so. First, Use the Firebase console setup workflow. Second, Use the Android Studio Firebase Assistant. But now we are going to use the Firebase Console instead of using Android Studio Firebase Assistant.

First, Create a Firebase project

Before you can add Firebase to your Android app, you need to create a Firebase project to connect to your Android app. Now go to firebase console and create your project there by clicking Add Project.

Second, Register your app with Firebase

After you have a Firebase project, the things you need to do next is, add your Android app. Click on that Android icon to add an Android Project.

Then you need to fill the form about your android app. The form also ask you to enter the sha1 code. Where you can get sha1 code? Have a look at this. After that just hit that Register App button.

After fill-up the form, it will ask you to download a file named google-services.json. Download and add it to your android project. Put it on the app root.

After that, just follow all the instructions such as:

Third, Add Firebase SDK

Add this code below to your android app.

Project-level build.gradle (<project>/build.gradle):

buildscript {
repositories {
// Check that you have the following line (if not, add it):
google()
}
dependencies {

classpath ‘com.google.gms:google-services:4.3.2’
}
}
allprojects {

repositories {
// Check that you have the following line (if not, add it):
google()

}
}

App-level build.gradle (<project>/<app-module>/build.gradle):

apply plugin: 'com.google.gms.google-services'android {
...
}
dependencies {
implementation 'com.google.firebase:firebase-analytics:17.2.0'
implementation 'com.google.firebase:firebase-auth:19.1.0'
implementation 'com.google.firebase:firebase-firestore:21.2.0'
}

Now synch your project and you are good to go.

Create Google Auth For Android App

Before we go code, we need to enable google auth from the firebase console. Have a look at this picture.

After that hit that save button.

And now let's move on to Android Studio. The thing we need to do first is creating a layout for our google auth. Add code below to your layout XML to create Google Button.

<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

If you don't want to use Google Button, you can also replace it with ImageButton or just with the regular button. It’s up to you.

After dealing with layout XML, now we are moving on to Kotlin class.

First, declare some variables below.

private lateinit var mGoogleSignInClient: GoogleSignInClient
private lateinit var gso: GoogleSignInOptions
private val RC_SIGN_IN: Int = 1

Second, we need to configure Google Sign In.

val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()

mGoogleSignInClient = GoogleSignIn.getClient(this, gso)

After that, we create signInGoogle function, handleResult function, and onActivityResult

private fun signInGoogle(){
val signInIntent: Intent = mGoogleSignInClient.signInIntent
startActivityForResult(signInIntent, RC_SIGN_IN)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == RC_SIGN_IN) {
val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(data)
handleResult(task)
}
}

private fun handleResult(completedTask: Task<GoogleSignInAccount>){
try {
val account: GoogleSignInAccount = completedTask.getResult(ApiException::class.java)!!
updateUI(account)
} catch (e: ApiException) {
d("handleRequest", e.toString())
}
}

And last, just call signInGoogle function in onCreate method.

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

...

signInGoogleBtn.setOnClickListener {
signInGoogle()
}

}

Here is the final application would look like.

--

--