Authenticate with passwords

While Credential Manager supports password-based sign-in, we strongly recommend prioritizing modern, more secure authentication methods like passkeys and Sign in with Google. 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

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

Groovy

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

Instantiate Credential Manager

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

// 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 object contains the username and password to be saved. Call credentialManager.createCredential() to initiate the saving process.

  2. Handle the response: Process the CreatePasswordResponse and manage any errors (for example, if the user cancels the request).

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 with a GetPasswordOption and call getCredential(). To filter which passwords are fetched based on the specified user IDs, use the optional field allowedUserIds.

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 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.

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 and combine ContentType("saveSuppressionCredential") with your existing credential ContentType using the + operator:

val saveSuppressionHint = ContentType("saveSuppressionCredential")

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

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:

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

For more information, see Autofill in Compose and Create a secure password field.

Views (XML)

Add saveSuppressionCredential alongside the credential hints in android:autofillHints as a comma-separated list:

<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" />

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

Views (Kotlin)

Pass saveSuppressionCredential alongside the credential hints to setAutofillHints. Using HintConstants requires the androidx.autofill:autofill library:

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",
    )
}

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

To ensure compatibility with password managers, you must configure Digital Asset Links between your app and your website. This allows credentials to be shared securely across both platforms.