How to add Android fingerprint authentication to your app
ArcTouch’s app development team shares step-by-step instructions for how to add fingerprint authentication to an Android app.
5 min. read - January 16, 2020
The experience of fingerprint authentication is really delightful. Once you have used a phone with a fingerprint scanner, it’s difficult to imagine going back to one without it. Remember how annoying it was to provide your password every time you needed to access your bank’s app? And every time you needed to perform a sensitive operation, like an account transfer? And even rapidly drawing some random lines to unlock your phone while fearing someone getting a glimpse of it?
As an Android app developer and an app user, I know just how big of a difference fingerprint authentication can make for the user experience. In this post, we provide step-by-step instructions for how developers can add Android fingerprint authentication into apps.
A bit of Android fingerprint authentication history
Fingerprint authentication has been officially supported by Android since API 23 (M). At that time, the feature was supported through the FingerprintManager
API. Although there was the material.io UI standard for it, there wasn’t a unified authentication dialog — which meant app developers were wildly inconsistent how they supported it.
API 28 added the BiometricPrompt
, a unified authentication dialog, and FingerprintManager
was marked as deprecated. AndroidX’s BiometricPrompt reached beta in August 2019! ?
All code snippets provided in this guide were made using AndroidX’s BiometricPrompt
(at the time of writing this post, the latest version is 1.0.0-beta01), so make sure to add its dependency in your app’s build.gradle:
1
Step by step: Implementing Android fingerprint authentication
While improving the UX is a great reason to implement fingerprint authentication in an Android app, companies must also take great care to protect features that run behind fingerprint authentication. Accessing them can only occur after the Android system confirms that the individual using the device is recognized and authorized.
Step 1: Check device support
First of all, check if the device supports fingerprint authentication by calling:
1
This method returns an int
, which can have one of these values:
1
2
3
4
Obviously, you should only show the option for biometric authentication if it’s available on the device, that is, if canAuthenticate()
returns BIOMETRIC_SUCCESS
. Some smartphones that had fingerprint scanners before Android officially supported it will return BIOMETRIC_ERROR_HW_UNAVAILABLE
. Generally speaking, those smartphones were released in APIs before 23 and could be updated to 23 (like the Samsung Galaxy Note 4). They can’t be used by the official API because they don’t attend to some specifications given by the Android M Compatibility Definition Document, including this one:
“MUST have a hardware-backed keystore implementation, and perform the fingerprint matching in a Trusted Execution Environment (TEE) or on a chip with a secure channel to the TEE.”
Step 2: Create a BiometricPrompt instance
We need to create an instance of BiometricPrompt
that will be responsible to prompt the user for authentication. Its constructor has three parameters:
Fragment
orFragmentActivity
: A reference to the client’s activityExecutor
: An executor to handle callback eventsBiometricPrompt.AuthenticationCallback
: An object to receive authentication events
The first two arguments are quite direct. For the first argument, use the calling FragmentActivity
or Fragment
. For the second one, you should use the executor that fits best your needs. Here, for simplicity, we’re going to use the mainExecutor
.
The third argument deserves more attention. It will receive the authentication results (such as if there was an error, or if it was canceled or successful) and can give us a CryptoObject
associated with the authentication. We’ll come back to the CryptoObject
in a later blog post, but for now, here’s a simple example which doesn’t need it:
1
2
3
4
5
For the callback
, we can use:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
The dialog displays the correct message (errString
) by itself while taking into consideration the user’s Locale
, so it’s pretty much automatic. You might need to redirect the user to some other flow in case of an error. The possible error codes are listed here. It’s worth noting that when the user presses the negative button, it’ll result in onAuthenticationError
with ERROR_NEGATIVE_BUTTON
.
Step 3: Build the authentication dialog
The dialog is built using the BiometricPrompt.PromptInfo.Builder
. It follows the commonly seen builder pattern and can be as simple and direct as:
1
2
3
4
But it also provides some optional methods for displaying a description and assistive text, such as allowing the device’s credential authentication (PIN, pattern or password). Those methods are described here in the documentation. Notice that if you enable the use of the device’s credentials to authenticate, you can’t set a negative button.
Step 4: Authenticate
Now, with two objects at hand, we can call biometricPrompt.authenticate(promptInfo)
and perform the actual authentication. This will display the authentication dialog and send its events to the callback
specified. Here is a simple example of how it might look:
That is a simple way to implement fingerprint authentication in Android.
Need help with your Android app project?
ArcTouch’s Android developers have been building lovable apps for more than a decade. If you need help with your Android project or some advice on how to support the latest features in Android 10, contact us to set up a free consultation.
Article Author:
Subscribe for more insights
Get our newsletter in your inbox.
Contact us.
Let's build something lovable. Together.
We help companies of all sizes build lovable apps, websites, and connected experiences.