1

I am using BiometricPrompt to let the user use fingerprint authentication to log into the app I have done the following in my PasswordActivity class:

     Executor executor = Executors.newSingleThreadExecutor();

    FragmentActivity activity = this;

    final BiometricPrompt biometricPrompt = new BiometricPrompt(activity, executor, new BiometricPrompt.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
            super.onAuthenticationError(errorCode, errString);
            if (errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON) {
                // user clicked negative button
            } else {
                //TODO: Called when an unrecoverable error has been encountered and the operation is complete.
            }
        }

        @Override
        public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);
            //TODO: Called when a biometric is recognized.
            final String decryptedText = decryptText();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (decryptedText != null && !decryptedText.isEmpty()) {
                        editPassword.setText(decryptedText);
                        buttonNext();
                    }
                }
            });

        }

        @Override
        public void onAuthenticationFailed() {
            super.onAuthenticationFailed();
            //TODO: Called when a biometric is valid but not recognized.
        }
    });

    final BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
            .setTitle("My App"))
            .setSubtitle("Log on into the app"))
            .setNegativeButtonText("Cancel").toUpperCase())
            .build();

    if (sharedPreferenceManager.isFingerprintEnabled(this))
        biometricPrompt.authenticate(promptInfo);   

This is the exception that I am getting. Do I have to set?

setNegativeButton (CharSequence text, 
            Executor executor, 
            DialogInterface.OnClickListener listener) as well?

I am using implementation 'androidx.biometric:biometric:1.0.0-alpha03' this version.

Caused by java.lang.IllegalArgumentException: Executor must not be null
   at android.hardware.biometrics.BiometricPrompt$Builder.setNegativeButton + 182(BiometricPrompt.java:182)
   at androidx.biometric.BiometricFragment.onCreate + 201(BiometricFragment.java:201)
   at androidx.fragment.app.Fragment.performCreate + 2414(Fragment.java:2414)
   at androidx.fragment.app.FragmentManagerImpl.moveToState + 1418(FragmentManagerImpl.java:1418)
   at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState + 1784(FragmentManagerImpl.java:1784)
   at androidx.fragment.app.FragmentManagerImpl.moveToState + 1861(FragmentManagerImpl.java:1861)
   at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange + 3269(FragmentManagerImpl.java:3269)
   at androidx.fragment.app.FragmentManagerImpl.dispatchCreate + 3223(FragmentManagerImpl.java:3223)
   at androidx.fragment.app.FragmentController.dispatchCreate + 190(FragmentController.java:190)
   at androidx.fragment.app.FragmentActivity.onCreate + 369(FragmentActivity.java:369)
   at androidx.appcompat.app.AppCompatActivity.onCreate + 85(AppCompatActivity.java:85)
user2234
  • 1,282
  • 1
  • 21
  • 44

3 Answers3

2

Can you try replacing Executor executor = Executors.newSingleThreadExecutor(); with:

private Handler handler = new Handler();

private Executor executor = new Executor() {
    @Override
    public void execute(Runnable command) {
        handler.post(command);
    }
};

This is according to the code given in this developer.android.com tutorial.

Vansh Arora
  • 392
  • 2
  • 13
0

Try to update the dependency, the currently latest version is already a release candidate:

implementation "androidx.biometric:biometric:1.0.0-rc01"
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • this method is not available in AndroidX – user2234 Sep 25 '19 at 01:16
  • The `androidx` [BiometricPrompt.java](https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/biometric/src/main/java/androidx/biometric/BiometricPrompt.java) indeed does not have it. The `Executor` might already be `null` when constructing, because that's where it is being passed and where the error message `Executor must not be null` exists. – Martin Zeitler Sep 25 '19 at 01:31
  • Yes I agree. Not sure how should I fix this. Apparently I never came across this crash, but lot of devices with Android 9 and 10 in production are facing these – user2234 Sep 25 '19 at 01:40
  • 1
    You could check if `executor != null `, before attempting to show the prompt... however, when having a closer look at the stack-trace, the last one line is still `android.hardware.biometrics.BiometricPrompt` and not `androidx`... try version `1.0.0-beta02`. – Martin Zeitler Sep 25 '19 at 02:44
  • Thanks Martin, I will give it a try and keep this post updated with my findings – user2234 Sep 25 '19 at 02:54
  • The [issue tracker](https://issuetracker.google.com/issues?q=componentid:559537%20status:open) does not seem to have such a bug. – Martin Zeitler Sep 25 '19 at 03:06
  • I have similar problem, but the executor and the callback that i'm passing are not null. https://stackoverflow.com/questions/58286606/biometricprompt-executor-and-or-callback-was-null – Kostadin Georgiev Oct 15 '19 at 11:28
  • @MartinZeitler: the issue has been fixed when I updated to the latest version 1.0.0-beta02, as suggested by you. Can you please update your answer so that I can accept it – user2234 Oct 17 '19 at 22:33
0
Caused by java.lang.IllegalArgumentException: Executor must not be null
   at android.hardware.biometrics.BiometricPrompt$Builder.setNegativeButton + 182(BiometricPrompt.java:182)

This indicates that the framework on the device you're testing is either not receiving the executor from the support library (bug in support library), or the framework itself has a bug.

Could you try on a later version of the androidx.biometric library? Beta02 was recently released, a lot of things have been fixed since alpha03.

Also, what device are you testing, if it's reproducible on Beta02 could you grab a bugreport via adb bugreport foo.zip and attach your sample app with the bug to the public issue tracker?

Kevin
  • 168
  • 11