'Setting the mutability flag in PendingIntent on Android 12 when prompting phone number

In Android 12, it is required to specify the mutability of the PendingIntent. In the signing screen the HintRequest class is used and PendingIntent is returned by the getHintPickerIntent method. The code is as follows,

 HintRequest hintRequest = new HintRequest.Builder().setPhoneNumberIdentifierSupported(true).build();
 PendingIntent intent = Credentials.getClient(activity).getHintPickerIntent(hintRequest);//crashes here on android 12

The error in logcat is this,

java.lang.IllegalArgumentException: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.

My question is how to set the mutability flag in the pending intent once it is created.



Solution 1:[1]

The issue is because of the auth library is not having the latest SDK version (31) requirements. I had a similar issue and it got fixed after updating the latest auth library in build Gradle.

Update your auth library version to 20.2.0 or later version

implementation "com.google.android.gms:play-services-auth:20.2.0"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Asharali V U