Kotlin Gmail Sign In Integration Example
Project Flow:
Step 1:
Open AndroidManifest.xml and add INTERNET Permission.
<uses-permission android:name="android.permission.INTERNET"/>
Step 2:
Open build.gradle(Project) and add following code.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.1.2' apply false
id 'com.android.library' version '7.1.2' apply false
id 'org.jetbrains.kotlin.android' version '1.5.30' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Step 3:
Open build.gradle(Module) and add following code.
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.kotlinkatta.demo"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
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'
}
buildFeatures{
dataBinding true;
viewBinding true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.google.android.gms:play-services-auth:20.1.0'
}
Step 4:
Open Google Developers Console -> Enabled API & services -> CREATE PROJECT.
Step 5:
Open Start integrating google sign in your android app -> Start Integrating -> Configure a project.(Select Your Project)
Step 6:
Configure Your OAuth client -> Select Android -> Enter your package name & SHA-1 key
Find SHA-1 Key:
Enter Following Command into terminal:
$ keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
Step 7:
Download Client Configuration json file and paste into project Module(app).
Step 8:
Open activity_main.xml and add following code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
tools:context=".MainActivity">
<com.google.android.gms.common.SignInButton
android:layout_margin="10dp"
android:id="@+id/signinbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_margin="10dp"
android:id="@+id/txtinfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"/>
</LinearLayout>
Step 9:
Open MainActivity.kt and add following code.
package com.kotlinkatta.demo
import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isGone
import androidx.core.view.isVisible
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.kotlinkatta.demo.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
lateinit var activityMainBinding: ActivityMainBinding;
lateinit var mGoogleSignInClient: GoogleSignInClient
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setContentView(R.layout.activity_main)
activityMainBinding = ActivityMainBinding.inflate(layoutInflater)
val view = activityMainBinding.root
setContentView(view)
activityMainBinding.signinbutton.visibility = View.VISIBLE
activityMainBinding.txtinfo.visibility = View.GONE
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build()
mGoogleSignInClient = GoogleSignIn.getClient(this, gso)
activityMainBinding.signinbutton.setOnClickListener {
signIn();
}
}
private fun signIn() {
val signInIntent = mGoogleSignInClient.signInIntent
launcher.launch(signInIntent)
}
private val launcher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val task = GoogleSignIn.getSignedInAccountFromIntent(result.data)
handleSignInResult(task)
}
}
private fun handleSignInResult(completedTask: Task<GoogleSignInAccount>) {
try {
val account = completedTask.getResult(ApiException::class.java)
// Signed in successfully
activityMainBinding.signinbutton.visibility = View.GONE
activityMainBinding.txtinfo.visibility = View.VISIBLE
val googleId = account?.id ?: ""
Log.i("Google ID", googleId)
val googleFirstName = account?.givenName ?: ""
Log.i("Google First Name", googleFirstName)
val googleLastName = account?.familyName ?: ""
Log.i("Google Last Name", googleLastName)
val googleEmail = account?.email ?: ""
Log.i("Google Email", googleEmail)
val googleProfilePicURL = account?.photoUrl.toString()
Log.i("Google Profile Pic URL", googleProfilePicURL)
val googleIdToken = account?.idToken ?: ""
Log.i("Google ID Token", googleIdToken)
activityMainBinding.txtinfo.text = "Google First Name: " + googleFirstName +
"\nGoogle Last Name: " + googleLastName +
"\nGoogle Email: " + googleEmail
} catch (e: ApiException) {
activityMainBinding.signinbutton.visibility = View.VISIBLE
activityMainBinding.txtinfo.visibility = View.GONE
Log.e("failed code=", e.statusCode.toString())
}
}
}
Comments
Post a Comment