Biometric Authentication in Android Kotlin…

Saqib Ahmed
4 min readMar 22, 2022

You wanted to put an authentication to your app using biometric. Then, you landed on the correct page. You want to keep secret information and more concern about the privacy in phone. This article will help you to setup and learn about of biometric authentication in android…

In Day-to-Day of our lives the security and privacy is more important and concern subject in everyone’s life. So, to give privacy and security the Android Engineers will came up with FingerPrintManager API , This API would provide the biometric fingerprint authentication to your app. The API is deprecated in the API level 28 , to add more security and better performance BiometricPrompt API take it place.

So, without wasting time. Let implement the code.

Add the Gradle dependency to your app module:

Firstly, add the dependencies of the biometric authentication in the build.gradle file and sync it.

Implement the biometric prompt API:

to implement the biometric prompt API you need do the three things.

  1. check the device is capable of the biometric authentication.

check the whether the device is support the biometric capabilities.it take the biometric manager which pass the authenticate type that support by the device. Biometric Manager Authenticate class will define the three type of the authenticate it support.

  • BIOMETRIC_STRONG authentication using a Class 3 biometric and using string security.
  • BIOMETRIC_WEAK authenticate using a Class 2 biometric and security is less compared to biometric strong.
  • DEVICE_CREDENTIALS authenticate using a device screen lock, PIN and Patterns.

2. show the biometric prompt dialog for the user.

The Biometric Prompt Builder class come in handy in setup and building the dialog info to the user. you no need to create a UI class for the showing info to the user.

  • setTitle() — setup the title for the dialog.
  • setSubtitle() — setup the subtitle for the dialog.
  • setDescription() — setup the description for the dialog
  • setAllowedAuthnticators() — setup the allowed authenticated type for the device. This will set whether the device should use only the biometric or it can use the other type like device credentials (PIN, Patterns, etc.,).

3. handle the biometric authentication callback.

This will handle the biometric callback which use the BiometricPrompt.AuthenticationCallBack()listen the authenticate event for the user. it has three methods:

  • onAuthenticationError — When an unrecoverable error has been encountered and the authentication process has completed without success, then this callback will be triggered.
  • onAuthenticationFailed — When the fingerprint doesn’t match with any of the fingerprints registered on the device, then this callback will be triggered.
  • onAuthenticationSucceeded — When the fingerprint match with the finger registered on the device, then this callback is triggered.

Now, just call the biometric object from your Activity/Fragment class as given below.

You wonder that where the hell the showBiometricPrompt()came from, bear with me you will get this in a minute…

I have wrote a clean way of writing the code for biometric, the showBiometricPrompt() function call or trigged for the biometric authentication.

Kotlin know for it’s named argument parameter, you can pass the desire argument with the function and set it up the functionality for the desired result.

BiometricPrompt API work well with cryptography, it provide the additional layer of security to your app. For now, we are not implement the cryptography in this article, so type null in cryptoObject so to run the biometric without the cryptography. The BiometricPrompt authenticate() method will take the prompt info builder and execute it, if you want crypto message simply pass the cryptoObject as a parameter to the authenticate method.

So, the code for the biometric file object will look like this.

I have created a listener for success and error event handle listener, which can be implement in your activity file. if the biometric event will success, it will triggered the success listener. if the biometric failed for the device, the error event will be trigged.

That’s it … with this listener, You can do what ever you can do with the result.

output:

setting up the negative button:

If you don’t want your app to use the device credentials, you want the user to login via traditional username and password method then the biometricPrompt API provide the negative button.

First, create an activity with the login username and password. Now, change the .setAllowedAuthenticators(BIOMETRIC_STRONG or DEVICE_CREDENTIAL) to negative button..setNegativeButtonText(“Use account”)

To show the login activity while cliking the use account, handle it with authentication error listener.

You cannot set both .setAllowedAuthenticators() and .setNegativeButtonText() at the same time. it won’t work.

You have successfully implement the biometric in your app. To access the full code, check out the github page.

Happy Coding….☺☺☺

--

--

Saqib Ahmed

I'm basically a mobile developer and became a self-time freelance writer. I spend most of my time with tech related stuff. I’m sharing my learning with you!