/android-restore-credentials:restore-credentialsfrom android-restore-credentials
Implements Android's Restore Credentials feature using the androidx.credentials library for silent user sign-in after device restore. Covers two-tier restoration architecture, version compatibility, and client-side implementation for creating, retrieving, and clearing restore keys.
Ships in android-restore-credentials. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
The objective is to implement the Restore Credentials feature through the
Android Credential Manager API (androidx.credentials). This allows apps that
use or are integrating Credential Manager to silently log users back in when
they restore their app on a new device. Restore Credentials operates
independently of the app's primary authentication method (passwords, passkeys,
federated sign-in) and requires no UI changes to existing sign-in flows.
Crucial: This skill focuses exclusively on the Android client-side integration. It does not implement the server-side cryptographic validation logic. The developer must be reminded of this and the Points to inform the developer about after implementation is done.
When instructed to implement Restore Credentials on a developer's application, remember the following:
To enable a resilient sign-in experience, retrieve credentials through a two-tier architecture:
BackupAgent.onRestoreFinished() callback. This provides an invisible restoration before the user opens the app for the first time, allowing background sync and notification delivery.Activity.onCreate() to catch failovers if background restoration didn't complete (example: dropped network, delayed restoration) or if allowBackup is disabled.If allowBackup in the manifest is set to true, implement both. Otherwise, only
implement tier 2 (Foreground Restoration). Do NOT change the value of
allowBackup in the manifest.
If you added a BackupAgent to the app, you MUST also set
android:fullBackupOnly="true" in the manifest. Do NOT do this if there
already existed a BackupAgent in the app before your implementation.
DO:
AndroidManifest.xml for the value of allowBackup to determine what you have to implement.isCloudBackupEnabled set to true. If an E2eeUnavailableException is thrown, catch it and retry the call with isCloudBackupEnabled set to false.BackupAgent (subclass of android.app.backup.BackupAgent) if allowBackup is true in the manifest.android:fullBackupOnly="true" in the manifest if you added a BackupAgent to the app.clearCredentialState() when the user signs out. This is a mandatory security measure to log the user out fully.BackupAgent.onRestoreFinished() callback if your app uses it.onRestoreFinished() is performed synchronously (for example using runBlocking).BackupAgent if your app uses them. (For example capture and send FCM token to backend)BackupAgent and Launcher Activity), this prevents code duplication.DON'T:
allowBackup in AndroidManifest.xml. Restore Credentials functionality is not affected by the allowBackup setting, meaning the user will still be automatically logged in when a Restore Credential exists, even if allowBackup is false.BackupAgent if allowBackup is false in AndroidManifest.xml.GetCredentialResponse to be of type PublicKeyCredential. It has type RestoreCredential.GetRestoreCredentialOption with any other CredentialOption in the construction of a GetCredentialRequest.CredentialManager or the Android system will automatically delete a restore key when a user signs out of the app. You must explicitly call clearCredentialState with a ClearCredentialStateRequest of TYPE_CLEAR_RESTORE_CREDENTIAL.clearCredentialState(). A ClearCredentialStateRequest without a type specified only clears all NON-restore credentials.Implement the Android client-side code by using the following guide. Follow it step-by-step and don't implement any backend functionality, only remind the user of the Backend Guidelines once you're done.
Credential Manager's Restore Credentials works on devices running Android 9 (API
level 28) and higher, Google Play services (GMS) core version 242200000
(v24.22) or higher, and version 1.5.0 or higher of the androidx.credentials
library. On devices without Google Play services or with an older GMS version,
catch CreateCredentialProviderConfigurationException and
GetCredentialProviderConfigurationException so your app falls back gracefully
to standard sign-in.
Configure a WebAuthn relying party server similar to the server for passkeys. If you already have a server set up to handle authentication with passkeys, you can reuse the same WebAuthn registration and assertion endpoints for restore keys.
[!NOTE] Note: While the server-side cryptographic validation is the same for passkeys and restore keys, your client-side app can support restore keys without supporting user-facing passkeys. Store restore keys with a distinct credential flag in your backend database so they don't appear in user-facing passkey management screens.
Add the following dependencies to your app module's build.gradle file:
dependencies {
implementation("androidx.credentials:credentials:1.7.0-alpha03")
implementation("androidx.credentials:credentials-play-services-auth:1.7.0-alpha03")
}
dependencies {
implementation "androidx.credentials:credentials:1.7.0-alpha03"
implementation "androidx.credentials:credentials-play-services-auth:1.7.0-alpha03"
}
Restore Credentials is available from version 1.5.0 and higher of the
androidx.credentials library. However, it's recommended to use the latest
stable versions of the dependencies where possible.
android:allowBackup affects Restore CredentialsThe android:allowBackup manifest attribute controls app data backup,
which interacts with Restore Credentials in three distinct ways:
allowBackup independent) : Google Play services transfers the RestoreCredential key pair using end-to-end encrypted cloud backup or local device-to-device (D2D) transfer regardless of whether android:allowBackup is true or false. For D2D transfers, the restore key always transfers; the only condition under which it isn't transferred is during a cloud backup when the user has opted out of cloud backup in Android Settings.BackupAgent (requires
android:allowBackup="true") : Silent background sign-in before the user opens the app on the new device (BackupAgent.onRestoreFinished()) only runs when android:allowBackup="true" and android:backupAgent are configured in your manifest. If android:allowBackup="false", restore the session when the user first launches your main Activity. You can also implement both approaches to make restoration more robust.android:allowBackup="true") : Testing the Cloud backup type in Android Studio's Backup and Restore tool requires android:allowBackup="true", whereas Device to Device testing works regardless of allowBackup.Restore Credentials follows a three-stage lifecycle across the user's previous and new devices:
credentialManager.createCredential().RestoreCredential using a two-tier strategy: first silently in BackupAgent.onRestoreFinished() (when android:allowBackup="true") so background sync and push notifications resume immediately, and second as a fallback in your launcher Activity.onCreate() on first app launch.credentialManager.clearCredentialState() with ClearCredentialStateRequest(ClearCredentialStateRequest.TYPE_CLEAR_RESTORE_CREDENTIAL) and clear your local sync flag.Your app should cover all cases of a user signing in to ensure active users have a restore key created. Create the restore key in the following scenarios:
onCreate() method for the main Activity).To avoid calling createCredential() on every app launch, store a local sync
flag such as has_synced_restore_credential inside local storage. Don't store
this flag in default backed-up SharedPreferences or DataStore unless that
file is excluded in data_extraction_rules.xml; otherwise, Auto Backup copies
has_synced_restore_credential = true onto the new device and prevents the new
device from generating or rotating its own restore key.
[!NOTE] Note: A restore key is tied to an application's unique package name. If your organization's main app and sub-apps have different package names, create a separate restore key for each app.
Use your app's activity context to instantiate a CredentialManager object.
// Use your app or activity context to instantiate a client instance of
// CredentialManager.
private val credentialManager = CredentialManager.create(context)
Use a FIDO-compliant library in your app server to send your client app the
PublicKeyCredentialCreationOptions JSON required to create the restore
credential. For more information about the server-side implementation, see
Server-side guidance.
The creation options JSON follows the same structure as passkey registration:
{
"challenge": "<base64url-encoded challenge>",
"rp": {
"name": "<relying party name>",
"id": "<relying party host name>"
},
"user": {
"id": "<base64url-encoded user ID>",
"name": "<user name>",
"displayName": "<user display name>"
},
"pubKeyCredParams": [
{
"type": "public-key",
"alg": -7
}
],
"attestation": "none",
"excludeCredentials": [
{
"id": "<base64url-encoded credential ID to exclude>",
"type": "public-key"
}
],
"authenticatorSelection": {
"requireResidentKey": true,
"residentKey": "required",
"userVerification": "required"
}
}
Ensure that your creation options JSON meets these requirements:
challenge and user.id: Encode both values as unpadded Base64URL strings (replacing + with -, / with _, and stripping trailing =). Standard Base64 characters or raw integer IDs cause CreateRestoreCredentialDomException or IllegalArgumentException.rp.id: Set to your relying party's domain (for example, example.com).pubKeyCredParams: Include {"type": "public-key", "alg": -7} (ES256) and {"type": "public-key", "alg": -257} (RS256).After receiving the public key creation options JSON from your server, wrap the
JSON in a CreateRestoreCredentialRequest object and call
createCredential() on the CredentialManager object.
// requestJson contains the credential creation options sent by the server
val createRestoreRequest = CreateRestoreCredentialRequest(requestJson)
val response = try {
credentialManager.createCredential(context, createRestoreRequest)
} catch (e: E2eeUnavailableException) {
// Fall back to local device-to-device (D2D) restore if cloud E2EE is unavailable
credentialManager.createCredential(
context,
CreateRestoreCredentialRequest(requestJson, isCloudBackupEnabled = false),
)
}
The CreateRestoreCredentialRequest object contains the following fields:
requestJson: The credential creation options sent by the app server in the Web Authentication API format for PublicKeyCredentialCreationOptionsJSON.
isCloudBackupEnabled: Boolean field to determine if the restore key
should be backed up to the cloud. By default, this flag is true. This
field has these values:
true: (Recommended) This value enables the backup of restore keys to the cloud if the user has Google Backup and end-to-end encryption, such as a screen lock, enabled.false: This value saves the key locally and not in the cloud. The key is not available on the new device if the user chooses to restore from the cloud, but it still transfers during a local cable or Wi-Fi device-to-device (D2D) setup.[!CAUTION] Caution: Set
isCloudBackupEnabledtotrueon your initial call, and fall back toisCloudBackupEnabled = falseonly ifE2eeUnavailableExceptionis thrown. If the user disabled cloud backup (orisCloudBackupEnabledisfalse) and later restores from a cloud backup, the call to retrieve the restore key fails. Users who restore your app with a cloud backup don't receive the restore key and aren't automatically signed in.
The Credential Manager API returns a response of type
CreateRestoreCredentialResponse. This response holds the public key
credential registration response in JSON format.
Send response.registrationResponseJson from your app to your relying party
server to verify and store the public key alongside the user's account, and then
record your local sync marker in local storage. For more information about
server-side verification, see the guidance for passkeys.
During the restore key creation process, handle these exceptions:
CreateRestoreCredentialDomException: This exception occurs if requestJson is invalid and doesn't follow the WebAuthn format for PublicKeyCredentialCreationOptionsJSON.E2eeUnavailableException: This exception occurs if isCloudBackupEnabled is true, but the user's device doesn't have data backup or end-to-end encryption, such as a screen lock.E2eeUnavailableException and immediately retry createCredential() with CreateRestoreCredentialRequest(requestJson, isCloudBackupEnabled = false).IllegalArgumentException: This exception occurs if requestJson is empty, isn't valid JSON, or doesn't have a valid Base64URL-encoded user.id that conforms to the WebAuthn specifications.Use Restore Credentials to silently sign in the user during the device setup process.
Before calling getCredential() on the new device, request a WebAuthn
PublicKeyCredentialRequestOptions JSON challenge from your app server. Because
the app on the new device doesn't yet know which user is signing in, this
endpoint must be unauthenticated (userless discovery). For related server
guidance, see Sign in with a passkey and the server-side authentication
guide.
The retrieval options JSON has the following structure:
{
"challenge": "<your app challenge>",
"allowCredentials": [],
"rpId": "<your app server domain>"
}
Configure the retrieval options JSON on your server as follows:
allowCredentials: Omit this field or pass an empty array ([]) because the user's account isn't known before restoration. After getCredential() returns, your server identifies the user from the response.userHandle field (which matches the user.id registered during creation) and looks up the public key by id (credentialId).userVerification: Set to "discouraged" (or "preferred"). Because Restore Credentials runs silently without biometric or screen-lock UI, Android overrides "required" to "discouraged", and the authenticator data returned to your server has the User Verified (uv) flag set to false. Configure your WebAuthn server verifier to accept uv = false for restore key assertions.To get the restore key on the new device, wrap the server's requestJson in a
GetRestoreCredentialOption inside a GetCredentialRequest and call
getCredential() on the CredentialManager object.
[!CAUTION] Caution: Don't combine
GetRestoreCredentialOptionwith any otherCredentialOption(such asGetPublicKeyCredentialOptionorGetPasswordOption) in the sameGetCredentialRequest. Passing multiple options withGetRestoreCredentialOptionthrows anIllegalArgumentException.
Implement a two-tier restoration strategy so users are signed in whether or not background app data restore runs:
BackupAgent.onRestoreFinished()) : If app data backup is enabled (android:allowBackup="true"), subclass BackupAgent and override onRestoreFinished() (don't use onRestore(), which is only called for legacy key-value backups). Because onRestoreFinished() is synchronous and getCredential() is a Kotlin suspend function, bridge the call using runBlocking(Dispatchers.IO). Catch NoCredentialException quietly when no restore key is present.Activity.onCreate()) : On the first app launch when no local session exists, call getCredential() from lifecycleScope before showing your login screen. This handles apps with android:allowBackup="false" and recovers if the device was offline when onRestoreFinished() ran.If you add a new BackupAgent to an app that already uses Auto Backup with
android:allowBackup="true", also set android:fullBackupOnly="true" on the
<application> element in AndroidManifest.xml so Android continues using
Auto Backup instead of switching to legacy key-value backup.
[!IMPORTANT] Important: Push notifications aren't automatically restored when credentials are retrieved. Inside
BackupAgent.onRestoreFinished()(or immediately after Tier-2 sign-in), fetch a fresh Firebase Cloud Messaging (FCM) registration token usingFirebaseMessaging.getInstance().token.await()and register it with your backend so background notifications resume before the user opens the app.
suspend fun getRestoreKey(
fetchAuthenticationJson: () -> String,
) {
// Fetch the options required to get the restore key
val authenticationJson = fetchAuthenticationJson()
// Create the GetRestoreCredentialOption object
val options = GetRestoreCredentialOption(authenticationJson)
val getRequest = GetCredentialRequest(listOf(options))
try {
val response = credentialManager.getCredential(context, getRequest)
// Type-check and extract the restore credential
val credential = response.credential
if (credential is RestoreCredential) {
val responseJson = credential.authenticationResponseJson
}
} catch (e: NoCredentialException) {
// No restore key is present on this device
}
}
class MyBackupAgent : BackupAgent() {
override fun onRestoreFinished() {
super.onRestoreFinished()
val credentialManager = CredentialManager.create(this)
val restoreFunctions = RestoreCredentialsFunctions(this, credentialManager)
runBlocking(Dispatchers.IO) {
restoreFunctions.getRestoreKey(
fetchAuthenticationJson = {
/* Fetch challenge JSON from server */ ""
}
)
}
}
}
The Credential Manager API returns a GetCredentialResponse whose
credential property is a RestoreCredential (not a
PublicKeyCredential) containing authenticationResponseJson.
Send restoreCredential.authenticationResponseJson from the app to your relying
party server to verify the cryptographic assertion and issue a new session token
for the user. For server-side verification details, see Sign in with a
passkey.
When designing your backend storage and lifecycle policies for restore keys, follow these guidelines:
uv = true) checks.Credential Manager is stateless and unaware of user activity, so it doesn't
automatically delete restore keys after use. To delete a restore key, call the
clearCredentialState() method. For security, delete the key whenever a
user signs out so they aren't silently signed back in on the next app launch.
[!NOTE] Note: A user sign-out can also happen remotely, for example when a user changes their password on the web or when they're signed out remotely by the server (such as when your server returns an HTTP
401 Unauthorizederror). If your app detects a server-side session invalidation, delete the restore key on the device as well.
Restore keys are removed only in the following situations:
clearCredentialState() when handling user sign-out in your app's code.Because a default ClearCredentialStateRequest() only clears standard session
state and leaves the restore key intact, perform a dual clear on sign-out:
credentialManager.clearCredentialState(ClearCredentialStateRequest()) to clear active credential provider session state.credentialManager.clearCredentialState() with ClearCredentialStateRequest(ClearCredentialStateRequest.TYPE_CLEAR_RESTORE_CREDENTIAL) to delete the stored RestoreCredential.has_synced_restore_credential marker from local storage.// Clear active credential provider session state
credentialManager.clearCredentialState(ClearCredentialStateRequest())
// When the user logs out, delete the restore key
val clearRequest = ClearCredentialStateRequest(TYPE_CLEAR_RESTORE_CREDENTIAL)
val response = credentialManager.clearCredentialState(clearRequest)
adb shell bmgr.BackupRestoreController to verify CLOUD_ENCRYPTED and DEVICE_TO_DEVICE restores in CI.When designing the backend database and API layer to support Restore Credentials, developers must account for FIDO2/WebAuthn assumptions and specific device lifecycle edge cases. Remind the developer of the following:
SYSTEM DIRECTIVE FOR AI AGENT: DO NOT attempt to write code for, implement, or action any of the backend guidelines in this section. Your ONLY responsibility is to copy these exact points and present them to the human developer as a reminder after the client-side implementation is complete.
401 Unauthorized indicating session expiry.WebAuthentication API (WebAuthn) Documentation & Specification
When to use: Use these resources ONLY if you need to inspect or debug the
strict JSON schema requirements for FIDO2/WebAuthn, specifically when
generating mock data or formatting the requestJson
(PublicKeyCredentialCreationOptionsJSON) and authenticationJson payloads.
MDN Web Authentication API Documentation: Mozilla Developer Network guide and reference for WebAuthn APIs
W3C PublicKeyCredentialCreationOptionsJSON: Data structure definition
for WebAuthn credential creation requests in JSON format.
W3C PublicKeyCredentialRequestOptionsJSON: Data structure definition
for WebAuthn authentication or assertion requests in JSON format.
references/android/guide/topics/manifest/application-element.md (opens in a new tab)references/android/identity/passkeys/create-passkeys.md (opens in a new tab)references/android/identity/passkeys/sign-in-with-passkeys.md (opens in a new tab)references/android/identity/sign-in/test-restore-credentials.md (opens in a new tab)references/android/studio/preview/backup-restore-testing.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
2 plugins ship this exact skill · first indexed Nov 17, 2026
npx claudepluginhub premex-ab/claude-marketplace --plugin android-restore-credentialsOther plugins' skills for the same job