If you’re integrating Google Sign-In in your Kotlin Android app and facing the issue where GoogleSignIn.getLastSignedInAccount(context) returns null even after a successful login, you’re not alone. This common error can frustrate many developers, especially those new to the integration process.

In this comprehensive guide, we’ll go through:
- Why this issue happens
- How to fix Google Sign-In not returning account in Kotlin
- Full working code example
- Common mistakes and debugging tips
Why GoogleSignIn.getLastSignedInAccount() Returns Null
This method returns null if:
- User is not signed in (you never called
signInIntent) - Sign-in failed silently
- Missing or incorrect configuration in your Google API Console or
google-services.json - Wrong request codes or misused callbacks
- Missing
GoogleSignInOptionswhen trying to retrieve the account
Solution: Proper Google Sign-In Integration in Kotlin
Step 1: Add Required Dependencies
Make sure your build.gradle has the correct dependencies:
implementation 'com.google.android.gms:play-services-auth:21.0.0'=
Step 2: Configure GoogleSignInOptions
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build()
Step 3: Initialize GoogleSignInClient
val googleSignInClient = GoogleSignIn.getClient(this, gso)
Step 4: Start Sign-In Intent
val signInIntent = googleSignInClient.signInIntent startActivityForResult(signInIntent, RC_SIGN_IN)
Where RC_SIGN_IN is a constant (e.g., private val RC_SIGN_IN = 1001)
Step 5: Handle Sign-In Result
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == RC_SIGN_IN) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
try {
val account = task.getResult(ApiException::class.java)
Log.d("GoogleSignIn", "Signed in as: ${account?.email}")
} catch (e: ApiException) {
Log.e("GoogleSignIn", "Sign in failed", e)
}
}
}
How to Retrieve Account After Sign-In
To get the account info later (e.g., in onCreate()), you must not sign out and call:
val account = GoogleSignIn.getLastSignedInAccount(this)
if (account != null) {
Log.d("GoogleAccount", "User email: ${account.email}")
} else {
Log.d("GoogleAccount", "No signed-in user found")
}
Make sure this line is only called after the user has successfully signed in.
Common Mistakes
| Mistake | Description |
|---|---|
Not using GoogleSignInOptions properly | Missing requestEmail() or other scopes |
Forgetting google-services.json | Must be placed under app/ folder |
| Sign-in logic inside wrong lifecycle method | Ensure onActivityResult is implemented correctly |
Using signInIntent twice | Avoid multiple launches before result is returned |
Expecting result from getLastSignedInAccount() before login | It will always return null unless sign-in succeeded |
Debugging Tips
- Add logs in
onActivityResultto ensure you’re getting a valid intent. - Check Google Developer Console: Is the SHA-1 of your debug/release key added?
- Make sure your app package name in Firebase/Google Console matches your app.
- Clean and rebuild your project.
- Test on real device with actual Google account.
Full Example: Kotlin Activity with Google Sign-In
class LoginActivity : AppCompatActivity() {
private val RC_SIGN_IN = 1001
private lateinit var googleSignInClient: GoogleSignInClient
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build()
googleSignInClient = GoogleSignIn.getClient(this, gso)
findViewById<Button>(R.id.sign_in_button).setOnClickListener {
val signInIntent = googleSignInClient.signInIntent
startActivityForResult(signInIntent, RC_SIGN_IN)
}
val account = GoogleSignIn.getLastSignedInAccount(this)
if (account != null) {
Log.d("LoginActivity", "Already signed in as: ${account.email}")
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == RC_SIGN_IN) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
try {
val account = task.getResult(ApiException::class.java)
Log.d("LoginActivity", "Signed in as: ${account?.email}")
// Proceed to next activity
} catch (e: ApiException) {
Log.e("LoginActivity", "Sign in failed", e)
}
}
}
}