TechBuzzPro

Biometric Authentication in Android Apps: How to Implement

· 5 min read · Phones & Tablets

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Biometric authentication lets your Android app verify a user with their fingerprint, face, or iris instead of a password. You can add it with the AndroidX Biometric library, which handles the system prompt, hardware checks, and crypto integration. This guide walks you through the setup, code, and common pitfalls.

What You'll Need

  • Android Studio (latest stable version recommended)
  • An Android device or emulator with a biometric sensor (fingerprint, face, or iris) and Android 6.0 (API 23) or higher
  • Basic knowledge of Kotlin or Java and Android app structure

Step-by-Step

  1. Add the Biometric dependency – In your app's build.gradle (module level), add implementation 'androidx.biometric:biometric:1.1.0' (or the latest version) and sync the project.
  2. Declare the required permission – In your AndroidManifest.xml, add . For Android 10 and lower, also add (though the system may ignore it on newer versions).
  3. Check hardware and enrollment – Before showing the prompt, use BiometricManager.from(context) and call canAuthenticate(). It returns BIOMETRIC_SUCCESS if ready, BIOMETRIC_ERROR_NO_HARDWARE if no sensor, BIOMETRIC_ERROR_NONE_ENROLLED if no biometrics are set up, and other errors like BIOMETRIC_ERROR_HW_UNAVAILABLE. Handle each case with a friendly message.
  4. Create a BiometricPrompt instance – In your activity or fragment, create a BiometricPrompt with an Executor (usually the main executor) and an AuthenticationCallback. Override onAuthenticationSucceeded, onAuthenticationFailed, and onAuthenticationError to handle the results.
  5. Set up the prompt info – Build a PromptInfo object using PromptInfo.Builder. Set the title (required), subtitle, description, and choose whether to allow negative button (for password fallback) or confirmation (for face/iris). For example: setTitle("Log in"), setSubtitle("Use your fingerprint"), setNegativeButtonText("Cancel").
  6. Launch the prompt – Call biometricPrompt.authenticate(promptInfo). The system will show a native dialog. If you need to use biometrics for encryption (e.g., to unlock sensitive data), you can pass a CryptoObject – but for simple authentication, you can pass null.
  7. Handle the callback – In onAuthenticationSucceeded, proceed with your app's logic (e.g., log the user in). In onAuthenticationFailed, show a message like "Fingerprint not recognized." In onAuthenticationError, check the error code (e.g., ERROR_LOCKOUT) and respond appropriately.
  8. Test on a real device – Emulators often lack biometric hardware. Use a physical device or configure an emulator with a virtual fingerprint (in AVD settings) if available.

Common Mistakes

  • Forgetting to check enrollment – If you skip canAuthenticate(), the prompt may fail or show a confusing error. Always check first.
  • Using the wrong permission – On Android 9+ you must use USE_BIOMETRIC. Using only USE_FINGERPRINT can cause SecurityException on newer devices.
  • Not handling cancellation – If the user cancels the prompt, onAuthenticationError is called with ERROR_NEGATIVE_BUTTON or ERROR_USER_CANCELED. Make sure you don't show an error for a normal cancellation.
  • Ignoring lockout states – After several failed attempts, the system locks biometrics. Handle ERROR_LOCKOUT and ERROR_LOCKOUT_PERMANENT with a fallback to password.

Tips

  • Always provide a fallback – Use the negative button to let users authenticate with a password, PIN, or pattern. Never force biometrics as the only method.
  • Use CryptoObject for sensitive data – If you're storing encrypted data, use a CryptoObject with a Cipher to tie decryption to a successful biometric check. This is more secure than just showing the prompt.
  • Test for all biometric types – Face and iris have different behaviors (e.g., they may require user confirmation). Set setAllowedAuthenticators() to specify which types you accept.
  • Keep UI responsive – The callback runs on the executor you provide. If you need to update UI, use the main executor.

Troubleshooting

Prompt doesn't appear – Check that the device has a biometric sensor and that the user has enrolled a fingerprint or face. Also verify that you've added the permission and that the app isn't in the background.

SecurityException on launch – This often happens when the permission is missing or the device doesn't support the requested authenticator. Ensure you've declared USE_BIOMETRIC and that you're not using USE_FINGERPRINT on Android 9+.

BiometricManager returns error – Handle each error code with a specific message. For BIOMETRIC_ERROR_NONE_ENROLLED, direct the user to the device settings to add a fingerprint or face.

Callback never fires – Make sure you're calling authenticate() after the activity is in a resumed state. If you call it too early, the prompt may not show.

If that did not work – Try clearing the app's cache or reinstalling. Also check that you're using the latest version of the Biometric library.

Frequently Asked Questions

Does biometric authentication work on all Android devices?

No. It requires a device with a fingerprint sensor, face unlock, or iris scanner. Use BiometricManager to check availability before showing the prompt.

Can I use biometrics without a CryptoObject?

Yes, for simple authentication (e.g., unlocking an app) you can pass null as the CryptoObject. However, for secure storage, you should use a CryptoObject to ensure that the biometric result is cryptographically bound.

How do I handle the negative button?

When you set a negative button text, the system calls onAuthenticationError with the error code ERROR_NEGATIVE_BUTTON. In that callback, you should show your fallback authentication screen.

What is the difference between onAuthenticationFailed and onAuthenticationError?

onAuthenticationFailed means the biometric was recognized but didn't match (e.g., wrong fingerprint). onAuthenticationError means a system error occurred, such as cancellation or lockout.

Can I use biometrics in a background service?

No. The BiometricPrompt requires a foreground activity to display the system dialog. You cannot show it from a background service.

Is face unlock supported?

Yes, if the device has a face unlock implementation that supports biometric authentication. Use setAllowedAuthenticators(BIOMETRIC_STRONG | BIOMETRIC_WEAK) to include face, but remember that strong biometrics (fingerprint, iris) are more secure.