Published: 31 December 2020
Create a new Android kotlin project.
Add Google’s Maven repository in project level build.gradle file.
allprojects {
repositories {
google()
// If you're using a version of Gradle lower than 4.1, you must instead use:
// maven {
// url 'https://maven.google.com'
// }
}
}
Add dependency in app level build.gradle file.
dependencies {
implementation 'com.google.android.gms:play-services-auth:19.0.0'
}
Configure Google Sign-in and the GoogleSignInClient object in onCreate() method.
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
var gso:GoogleSignInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build()
// Build a GoogleSignInClient with the options specified by gso.
var mGoogleSignInClient:GoogleSignInClient = GoogleSignIn.getClient(this, gso);
Add a sign-in button to your xml layout.
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Add code in onCreate method to make the sign-in button functional.
var signInButton: SignInButton = findViewById(R.id.sign_in_button)
signInButton.setSize(SignInButton.SIZE_STANDARD) //Optional
signInButton.setOnClickListener{
signIn()
}
Implement the signIn() method
private fun signIn() {
val signInIntent: Intent = mGoogleSignInClient.getSignInIntent()
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 = GoogleSignIn.getSignedInAccountFromIntent(data)
handleSignInResult(task)
}
}
Implement the handleSignInResult() method. If the sign-in is successful, update the UI in this method.
private fun handleSignInResult(completedTask: Task<GoogleSignInAccount>){
try {
val account = completedTask.getResult(ApiException::class.java)
// Signed in successfully, Update the UI here
Toast.makeText(context, "Sign in successful", Toast.LENGTH_SHORT).show()
} catch (e: ApiException) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Toast.makeText(context, "Sign in failed: "+ e.getStatusCode(), Toast.LENGTH_SHORT).show()
}
}
When integrating google sign-in we have to use Firebase or App Signing in Google Play console. In this example we are using Firebase. Login to Firebase console.
Register your app by giving the package name and SHA-1 key. The steps to generate SHA-1 key is shown below:
Open Gradle tab in your Android Studio Go to app/android/signingReport and run.
SHA-1 key will be generated in the bottom tab in your Android studio.
Use this SHA-1 key to register the app in firebase.
Check if project level build.gradle has the required code.
buildscript {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
}
dependencies {
...
// Add this line
classpath 'com.google.gms:google-services:4.3.4'
}
}
allprojects {
...
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
...
}
}
Check if the app level build.gradle file has the required code.
apply plugin: 'com.android.application'
// Add this line
apply plugin: 'com.google.gms.google-services'
dependencies {
// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:26.2.0')
// Add the dependencies for the desired Firebase products
// https://firebase.google.com/docs/android/setup#available-libraries
}
Now run the app and test. Feel free to download the source code from Github