Smart Lock per password, che non è più supportato dal 2022, è ora rimosso dall'SDK Google Play Services Auth (com.google.android.gms:play-services-auth) . Per ridurre al minimo le modifiche che provocano errori
che potrebbero avere un impatto sulle integrazioni esistenti, le funzionalità di Smart Lock per tutte le app esistenti
nel Play Store continueranno a funzionare correttamente. Le nuove versioni di app compilate con l'SDK aggiornato (com.google.android.gms:play-services-auth:21.0.0) non possono più accedere all'API Smart Lock per password e non creeranno build correttamente. Tutti gli sviluppatori devono eseguire la migrazione dei propri progetti Android per iniziare a usare Gestore delle credenziali il prima possibile.
Vantaggi della migrazione all'API Credential Manager
Gestore delle credenziali offre un'API unificata e semplificata che consente di supportare funzionalità e pratiche moderne, migliorando al contempo l'esperienza di autenticazione per i tuoi utenti:
- Gestore delle credenziali supporta completamente il salvataggio e l'autenticazione delle password. Ciò significa che non ci saranno interruzioni per gli utenti durante la migrazione dell'app da Smart Lock a Gestore delle credenziali.
- Gestore delle credenziali integra il supporto di più metodi di accesso, tra cui le passkey e i metodi di accesso federato come Accedi con Google, per aumentare la sicurezza e consentire la conversione se prevedi di supportare uno di questi metodi in futuro.
- A partire da Android 14, Gestore delle credenziali supporta fornitori di password e passkey di terze parti.
- Gestore delle credenziali offre un'esperienza utente coerente e unificata per tutti i metodi di autenticazione.
Opzioni di migrazione:
| Caso d'uso | Suggerimento |
|---|---|
| Salva la password e accedi con la password salvata | Utilizza l'opzione della password della guida Fai accedere l'utente con Gestore delle credenziali. I passaggi dettagliati per il salvataggio della password e l'autenticazione sono descritti di seguito. |
| Accedi con Google | Segui la guida Integra Gestore delle credenziali con Accedi con Google. |
| Mostra il suggerimento per il numero di telefono agli utenti | Utilizza l'API Phone Number Hint di Servizi di identità Google. |
Accedi con le password utilizzando Gestore delle credenziali
Per utilizzare l'API Credential Manager, completa i passaggi descritti nella sezione dei prerequisiti della guida di Gestore delle credenziali e assicurati di svolgere le seguenti operazioni:
- Aggiungi le dipendenze richieste
- Conserva le classi nel file ProGuard
- Aggiungi il supporto per Digital Asset Links (questo passaggio dovrebbe essere già implementato se utilizzi Smart Lock per password)
- Configura Gestore delle credenziali
- Indica i campi delle credenziali
- Salva la password di un utente
Accedi con le password salvate
Per recuperare le opzioni relative alla password associate all'account dell'utente, completa questi passaggi:
1. Inizializza l'opzione di password e autenticazione
// Retrieves the user's saved password for your app from their
// password provider.
val getPasswordOption = GetPasswordOption()
2. Utilizza le opzioni recuperate dal passaggio precedente per creare la richiesta di accesso
val getCredRequest = GetCredentialRequest(
listOf(getPasswordOption)
)
3. Avvia il flusso di accesso
fun launchSignInFlow() {
coroutineScope.launch {
try {
// Attempt to retrieve the credential from the Credential Manager.
val result = credentialManager.getCredential(
// Use an activity-based context to avoid undefined system UI
// launching behavior.
context = activityContext,
request = getCredRequest
)
// Process the successfully retrieved credential.
handleSignIn(result)
} catch (e: GetCredentialException) {
// Handle any errors that occur during the credential retrieval
// process.
handleFailure(e)
}
}
}
private fun handleSignIn(result: GetCredentialResponse) {
// Extract the credential from the response.
val credential = result.credential
// Determine the type of credential and handle it accordingly.
when (credential) {
is PasswordCredential -> {
val username = credential.id
val password = credential.password
// Use the extracted username and password to perform
// authentication.
}
else -> {
// Handle unrecognized credential types.
Log.e(TAG, "Unexpected type of credential")
}
}
}
private fun handleFailure(e: GetCredentialException) {
// Handle specific credential retrieval errors.
when (e) {
is GetCredentialCancellationException -> {
/* This exception is thrown when the user intentionally cancels
the credential retrieval operation. Update the application's state
accordingly. */
}
is GetCredentialCustomException -> {
/* This exception is thrown when a custom error occurs during the
credential retrieval flow. Refer to the documentation of the
third-party SDK used to create the GetCredentialRequest for
handling this exception. */
}
is GetCredentialInterruptedException -> {
/* This exception is thrown when an interruption occurs during the
credential retrieval flow. Determine whether to retry the
operation or proceed with an alternative authentication method. */
}
is GetCredentialProviderConfigurationException -> {
/* This exception is thrown when there is a mismatch in
configurations for the credential provider. Verify that the
provider dependency is included in the manifest and that the
required system services are enabled. */
}
is GetCredentialUnknownException -> {
/* This exception is thrown when the credential retrieval
operation fails without providing any additional details. Handle
the error appropriately based on the application's context. */
}
is GetCredentialUnsupportedException -> {
/* This exception is thrown when the device does not support the
Credential Manager feature. Inform the user that credential-based
authentication is unavailable and guide them to an alternative
authentication method. */
}
is NoCredentialException -> {
/* This exception is thrown when there are no viable credentials
available for the user. Prompt the user to sign up for an account
or provide an alternative authentication method. Upon successful
authentication, store the login information using
androidx.credentials.CredentialManager.createCredential to
facilitate easier sign-in the next time. */
}
else -> {
// Handle unexpected exceptions.
Log.w(TAG, "Unexpected exception type: ${e::class.java.name}")
}
}
}
Risorse aggiuntive
- Riferimento di esempio di Gestore delle credenziali
- Codelab di Gestore delle credenziali
- Fai accedere l'utente con Gestore delle credenziali