> Documentation index: https://developer.android.com/identity/llms.txt

While Credential Manager supports password-based sign-in, we strongly recommend
prioritizing modern, more secure authentication methods like [passkeys](https://developer.android.com/identity/passkeys.md.txt) and
[Sign in with Google](https://developer.android.com/identity/sign-in/credential-manager-siwg.md.txt). These methods offer significantly better security and
user experience. However, if your application must support passwords, use this
guide as a reference to implement passwords with Credential Manager.

Credential Manager provides a unified API for your app to save and retrieve user
credentials, including usernames and passwords. This simplifies the sign-in
process for users and allows them to utilize credential providers seamlessly.

## Prerequisites

Credential Manager's passwords implementation works on devices running Android
4.4 (API level 19) and higher.

## Overview

This guide focuses on the changes required in your app to create, save, and sign
in with a password.

- **Add dependencies to your app**: Add the required Credential Manager libraries.
- **Instantiate Credential Manager**: Create a Credential Manager instance.
- **Save a user's password**: Store user credentials securely.
- **Sign in with a password**: Retrieve and use stored credentials for login.
- **Configure autofill on credential fields**: Suppress duplicate Autofill with Google save dialogs when saving passwords with Credential Manager.
- **Add support for Digital Asset Links**: Add support to share passwords across linked apps and websites.

## Add dependencies to your app

Add the following dependencies to your app module's `build.gradle` file:

### Kotlin

```kotlin
dependencies {
    implementation("androidx.credentials:credentials:1.7.0-alpha03")
}
```

### Groovy

```groovy
dependencies {
    implementation "androidx.credentials:credentials:1.7.0-alpha03"
}
```

## Instantiate Credential Manager

Use your app or activity context to create a `CredentialManager` object.

```kotlin
// Use your app or activity context to instantiate a client instance of
// CredentialManager.
private val credentialManager = CredentialManager.create(context)
```

## Save a user's password

When a user successfully signs up or updates their password within your app,
save these credentials in their password manager.

1. **Construct a password request** : The
   [`CreatePasswordRequest`](https://developer.android.com/reference/androidx/credentials/CreatePasswordRequest.md.txt) object contains the username and
   password to be saved. Call `credentialManager.createCredential()` to
   initiate the saving process.

2. **Handle the response** : Process the [`CreatePasswordResponse`](https://developer.android.com/reference/androidx/credentials/CreatePasswordResponse.md.txt) and
   manage any errors (for example, if the user cancels the request).

> [!NOTE]
> **Note:** To avoid a potential memory leak, pass in a `MutableContextWrapper` of your foreground `Activity` to the `getCredential()` call. Credential Manager handles updating the context during `Activity` reconstruction.

```kotlin
suspend fun registerPassword(username: String, password: String) {
    // Initialize a CreatePasswordRequest object.
    val createPasswordRequest =
        CreatePasswordRequest(id = username, password = password)

    // Create credential and handle result.
    // Use an activity-based context to avoid undefined system UI
    // launching behavior.
    val context = MutableContextWrapper(activityContext)
    coroutineScope {
        try {
            val result =
                credentialManager.createCredential(
                    // Use MutableContextWrapper to avoid memory leak during configuration changes
                    context = context,
                    createPasswordRequest
                )
            // Handle register password result
        } catch (e: CreateCredentialException) {
            handleFailure(e)
        }
    }
}
```

## Sign in with a password

To retrieve saved credentials, build a [`GetCredentialRequest`](https://developer.android.com/reference/androidx/credentials/GetCredentialRequest.md.txt) with a
[`GetPasswordOption`](https://developer.android.com/reference/androidx/credentials/GetPasswordOption.md.txt) and call [`getCredential()`](https://developer.android.com/reference/androidx/credentials/GetCredentialResponse.md.txt#getCredential()). To filter which
passwords are fetched based on the specified user IDs, use the optional field
[`allowedUserIds`](https://developer.android.com/reference/androidx/credentials/GetPasswordOption.md.txt#GetPasswordOption(kotlin.collections.Set,kotlin.Boolean,kotlin.collections.Set)).

```kotlin
val getPasswordOption = GetPasswordOption()

val credentialRequest = GetCredentialRequest.Builder()
    .addCredentialOption(getPasswordOption)
    .build()
```

## Configure autofill on credential fields

When your app saves a password with Credential Manager using a
[`CreatePasswordRequest`](https://developer.android.com/reference/androidx/credentials/CreatePasswordRequest.md.txt) object and the `createCredential` method, Autofill
with Google can also prompt to save the password when the user navigates away.
As a result, the user can see duplicate save dialogs.

To suppress the Autofill with Google credential save dialog on Android 8.0
(API level 26) and higher, add the `saveSuppressionCredential` autofill hint to
the credential fields in your Credential Manager save flow. Combine
`saveSuppressionCredential` with your existing credential autofill hints rather
than replacing them so that field detection and autofill fill suggestions
continue working.

Keep the following details in mind when adding the hint:

- **Case-sensitive literal string** : No SDK or AndroidX constant is defined, so you must pass the literal string `saveSuppressionCredential`.
- **Autofill with Google only**: This hint applies only when Autofill with Google is the active autofill service. Third-party autofill services ignore it.
- **Targeted suppression** : When `saveSuppressionCredential` is present on any detected credential field in a form, Autofill with Google suppresses only its credential save dialog. Non-credential save flows---such as payment cards and addresses---and autofill fill suggestions aren't affected.
- **Multi-screen flows** : Autofill with Google checks only the current form. If your flow places the username and password fields on separate screens, add `saveSuppressionCredential` to the password screen.

> [!NOTE]
> **Note:** The [`android:isCredential`](https://developer.android.com/reference/android/view/View.md.txt#attr_android:isCredential) attribute is deprecated and no longer needed. Use standard autofill hints and `saveSuppressionCredential` instead.

The following examples show how to combine `saveSuppressionCredential` with
credential autofill hints on a sign-up screen:

### Compose

With Jetpack Compose UI 1.9.0 and later, use [`Modifier.contentType`](https://developer.android.com/reference/kotlin/androidx/compose/ui/autofill/contentType.modifier.md.txt#(androidx.compose.ui.Modifier).contentType(androidx.compose.ui.autofill.ContentType))
and combine `ContentType("saveSuppressionCredential")` with your existing
credential `ContentType` using the `+` operator:


```kotlin
val saveSuppressionHint = ContentType("saveSuppressionCredential")

TextField(
    state = usernameState,
    modifier = Modifier.contentType(ContentType.NewUsername + saveSuppressionHint),
)
SecureTextField(
    state = passwordState,
    modifier = Modifier.contentType(ContentType.NewPassword + saveSuppressionHint),
)
```

<br />

For sign-in screens, combine `saveSuppressionHint` with
`ContentType.Username` and `ContentType.Password` instead.

For Compose UI 1.8.x, import `contentType` from
`androidx.compose.ui.semantics` instead of `androidx.compose.ui.autofill`
and set it inside `Modifier.semantics`:


```kotlin
modifier = Modifier.semantics {
    contentType = ContentType.NewPassword + saveSuppressionHint
}
```

<br />

For more information, see [Autofill in Compose](https://developer.android.com/develop/ui/compose/text/autofill.md.txt) and
[Create a secure password field](https://developer.android.com/develop/ui/compose/text/user-input.md.txt#create-secure).

### Views (XML)

Add `saveSuppressionCredential` alongside the credential hints in
[`android:autofillHints`](https://developer.android.com/reference/android/view/View.md.txt#attr_android:autofillHints) as a comma-separated list:


```xml
<EditText
    android:id="@+id/username_edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:autofillHints="newUsername,saveSuppressionCredential" />

<EditText
    android:id="@+id/password_edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:autofillHints="newPassword,saveSuppressionCredential" />
```

<br />

For sign-in screens, combine `saveSuppressionCredential` with `username` and
`password` instead.

### Views (Kotlin)

Pass `saveSuppressionCredential` alongside the credential hints to
[`setAutofillHints`](https://developer.android.com/reference/kotlin/android/view/View.md.txt#setAutofillHints(kotlin.String)). Using [`HintConstants`](https://developer.android.com/reference/kotlin/androidx/autofill/HintConstants.md.txt) requires the
[`androidx.autofill:autofill`](https://developer.android.com/jetpack/androidx/releases/autofill.md.txt) library:


```kotlin
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    usernameEditText.setAutofillHints(
        HintConstants.AUTOFILL_HINT_NEW_USERNAME,
        "saveSuppressionCredential",
    )
    passwordEditText.setAutofillHints(
        HintConstants.AUTOFILL_HINT_NEW_PASSWORD,
        "saveSuppressionCredential",
    )
}
```

<br />

For sign-in screens, use `View.AUTOFILL_HINT_USERNAME` and
`View.AUTOFILL_HINT_PASSWORD` instead, which don't require the
`androidx.autofill:autofill` library.

## Add support for Digital Asset Links

To ensure compatibility with password managers, you must configure [Digital
Asset Links](https://developer.android.com/identity/credential-manager/prerequisites#configure-digital-asset-links) between your app and your website. This allows credentials to be
shared securely across both platforms.

> [!IMPORTANT]
> **Important:** Digital Asset Links are required for passkeys and highly recommended for password-based authentication.