4

I recently modified my app to use the AndroidX libraries and I'm attempting to use the androidx.biometric.BiometricPrompt in an AppCompatActivity.

However, I get the following exception:

java.lang.NoSuchMethodError: No virtual method getMainExecutor()Ljava/util/concurrent/Executor

I've tried to use the application context instead but that didn't work either.

import androidx.biometric.BiometricPrompt;
import androidx.appcompat.app.AppCompatActivity;

class MainActivity extends AppCompatActivity {        
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        final BiometricPrompt.PromptInfo info = new BiometricPrompt.PromptInfo.Builder()
            .setTitle("Login")
            .setSubtitle("Perform login with your fingerprint")
            .setNegativeButtonText("Cancel")
            .build();
        new BiometricPrompt(this, getMainExecutor(), onFingerprintAuthentication())
            .authenticate(info, getCryptoObject());
    }
}

These are the AndroidX libraries I'm importing

implementation 'androidx.core:core:1.1.0-alpha03'
implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha01'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.annotation:annotation:1.0.1'
implementation 'androidx.mediarouter:mediarouter:1.1.0-alpha01'
implementation 'androidx.browser:browser:1.0.0'
implementation 'androidx.exifinterface:exifinterface:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0-alpha01'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'androidx.biometric:biometric:1.0.0-alpha03'
  • 2
    `getMainExecutor` was added to `Context` in API level 28. However, `ContextCompat` (`androidx.core.content.ContextCompat`) has a static `getMainExecutor` method that you should be able to use. – Michael Jan 04 '19 at 09:19
  • @Michael Thank you so much! That fixed my problem. –  Jan 04 '19 at 10:17

1 Answers1

1

As @Michael mentioned in the comments, one can use

ContextCompat.getMainExecutor(this);

This solved my problem.

Alternatively, as mentioned in this answer, one can also create their own Executor

public class MainThreadExecutor implements Executor {
    private final Handler handler = new Handler(Looper.getMainLooper());

    @Override
    public void execute(@Nonnull Runnable runnable) {
        handler.post(runnable);
    }
}