Search code examples
androidautofillgoogle-smartlockpasswords

Credentials are not being saved with Smart Lock API for passwords when Autofill is enabled


I am currently implementing Smart Lock for password in an android App, I am following the documentation: https://developers.google.com/identity/smartlock-passwords/android/store-credentials

in android Oreo and above there is this Autofill feature, and I DO see the autofill pop-Up in the field, my issue is when trying to save the credentials, I am getting the error:

16: Credentials API's save confirmation dialog has been disabled to avoid conflicts with the Android Autofill feature. This choice may be overridden via CredentialsOptions.

which I have read in other posts, is an expected error, and it is there to prevent as it states conflicts with the Android Autofill feature but I don't see any other dialog to confirm the saving credential.

I tried with emulators and real devices I get the same result. Then I tried to force the display the Smart Lock for Passwords save dialog following the documentation:

Enable the save confirmation dialog for all platforms via the CredentialsOptions when building the CredentialsClient.

CredentialsOptions options = new CredentialsOptions.Builder()
        .forceEnableSaveDialog()
        .build();
mCredentialsClient = Credentials.getClient(this, options);

Suppress the Autofill save dialog by marking the root view associated with the login fields as not important for Autofill:

<!-- Mark the root view with android:importantForAutofill="noExcludeDescendants" -->
<LinearLayout
    android:importantForAutofill="noExcludeDescendants"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <!-- ... -->
</LinearLayout>     

I did it and it worked, the dialog was being shown when saving, but the autofill feature is gone (as expected), And I need the pop-ups, it is part of my requirements.

I also tried only applying the CredentialsOptions options mentioned above without the property in the XML, which didn't work. even wors, when undoing that, the app stopped working as expected and started throwing the error former mentioned, I uninstalled and restarted the phone, and still not showing the dialog and throwing the same error even though it has the fix to Suppress the Autofill applied.

I have check and the app is not in the declined sites and apps list I checked system -> language -> more -> autofill -> google is selected

I hope someone can help me.


Solution

  • Guys, I have found the answer myself, I am posting this in case someone else needs it.

    In my specific scenario, the issue was caused by not changing the activity, I was having this issue in a small prof of concept single activity app, so the "autofill context" was not being finished. I found this concept here in the autofill documentation:

    https://developer.android.com/guide/topics/text/autofill-optimize#ensure

    bottom line is either you finish the current activity and move to another one or you call:

     val autofillManager = requireContext().getSystemService(AutofillManager::class.java)
    autofillManager.commit()