Search code examples
androidkotlinandroid-studiogoogle-identity

Why doesn't Android Studio recognize the dependencies for googleId


I'm trying to build an app that uses Google sign in, but I get unresolved references errors for GoogleIdTokenCredential, GoogleIdTokenParsingException, and GetGoogleIdOption in this code:

coroutineScope.launch {
    try {
        // Call to get credentials
        val result = credentialManager.getCredential(context, request)
        val credential = result.credential
        val googleIdTokenCredential = GoogleIdTokenCredential.createFrom(credential.data)
        val googleIdToken = googleIdTokenCredential.idToken

        // Pass the token to the ViewModel
        loginViewModel.registerUserWithGoogleId(username, password, googleIdToken)

    } catch (e: GetCredentialException) {
        Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
    } catch (e: GoogleIdTokenParsingException) {
        Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
    }
}

and this code

@Provides
@Singleton // Ensure a single instance is used throughout the app
fun provideGoogleIdOption(): GetGoogleIdOption {
    val rawNonce = UUID.randomUUID().toString()
    val bytes = rawNonce.toByteArray()
    val md = MessageDigest.getInstance("SHA-256")
    val digest = md.digest(bytes)
    val hashedNonce = digest.fold("") { str, it -> str + "%02x".format(it) }

    return GetGoogleIdOption.Builder()
        .setFilterByAuthorizedAccounts(false)
        .setServerClientId("MY_CODE")
        .setNonce(hashedNonce)
        .build()
}

Android gives me the option to add the dependencies and import, like in the picture I added.

enter image description here

But if I press on the suggestion, nothing happens. And if I try to add manually the dependencies that I have copy-pasted from the android developers page (https://developer.android.com/identity/sign-in/credential-manager-siwg), I get this error when I build my project

Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all files for configuration ':app:debugRuntimeClasspath'.

These are the dependencies I have copied in my code:

implementation("androidx.credentials:credentials:1.3.0")
implementation("androidx.credentials:credentials-play-services-auth:1.3.0")

implementation("com.google.android.libraries.identity.googleid:googleid:1.3.0")

I have invalidated cache and restarted and cleaned the project, but I still get the same errors. So what must I do to make the dependencies from the documentation work in order to be permit my users to sign in with Google?


Solution

  • Use the correct version for googleid library which is 1.1.1

    implementation("com.google.android.libraries.identity.googleid:googleid:1.1.1")