In today’s high-tech world, securing user data is paramount, and one of the most effective ways to ensure security is through biometric authentication. Developers often face challenges such as integrating this seamlessly into their applications, ensuring robustness, and keeping up with various biometric methods like fingerprints, face recognition, and more. Thankfully, several Kotlin-supported biometric libraries are available to simplify this process. This tutorial covers some of the best biometric authentication libraries for Kotlin Android, offering in-depth knowledge, features, installation guides, and code examples to help you get started.
1. AndroidX Biometric Library
The AndroidX Biometric Library is Google's official library for integrating biometric authentication into Android applications. It supports fingerprint, face, and iris authentication and is consistently updated and maintained by Google.
Features
- Supports fingerprint, face, and iris authentication.
- Provides a unified API that works across various Android versions.
- Handles UI components and the authentication flow natively.
- Offers robust error handling and fallback mechanisms.
Installation
To integrate the AndroidX Biometric Library into your Kotlin Android project, add the following dependency to your build.gradle
file:.
implementation "androidx.biometric:biometric:1.2.0-alpha03"
Code Example
This example demonstrates how to implement fingerprint authentication using the AndroidX Biometric Library.
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.biometric.BiometricPrompt
import androidx.core.content.ContextCompat
class MainActivity : AppCompatActivity() {
private lateinit var biometricPrompt: BiometricPrompt
private lateinit var promptInfo: BiometricPrompt.PromptInfo
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val executor = ContextCompat.getMainExecutor(this)
biometricPrompt = BiometricPrompt(this, executor, object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
// Handle authentication error
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
// Handle authentication success
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
// Handle authentication failure
}
})
promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric login for my app")
.setSubtitle("Log in using your biometric credential")
.setNegativeButtonText("Use account password")
.build()
// Trigger the biometric authentication prompt
biometricPrompt.authenticate(promptInfo)
}
}
The code initializes a BiometricPrompt and sets up PromptInfo to provide information to the user. It then configures handlers for success, failure, and errors.
Comparison
Compared to other libraries, the AndroidX Biometric Library:.
- Is officially supported by Google, ensuring long-term maintenance and support.
- Provides a consistent and unified API across different Android versions.
- Is relatively straightforward to implement and requires minimal configuration.
Reference
2. FingerprintManagerCompat
The FingerprintManagerCompat is a part of the AndroidX support library that provides a backward-compatible way to integrate fingerprint authentication.
Features
- Works on API level 23 and above.
- Provides backward compatibility for older Android versions.
- Simple and easy to use API.
Installation
To add FingerprintManagerCompat to your project, include the following in your build.gradle
:.
implementation "androidx.core:core:1.7.0"
Code Example
Below is an example of how to use FingerprintManagerCompat for fingerprint authentication.
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.hardware.fingerprint.FingerprintManagerCompat
import androidx.core.os.CancellationSignal
class MainActivity : AppCompatActivity() {
private lateinit var fingerprintManager: FingerprintManagerCompat
private lateinit var cancellationSignal: CancellationSignal
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fingerprintManager = FingerprintManagerCompat.from(this)
if (fingerprintManager.isHardwareDetected) {
cancellationSignal = CancellationSignal()
fingerprintManager.authenticate(null, 0, cancellationSignal, object : FingerprintManagerCompat.AuthenticationCallback() {
override fun onAuthenticationError(errMsgId: Int, errString: CharSequence) {
super.onAuthenticationError(errMsgId, errString)
// Handle authentication error
}
override fun onAuthenticationSucceeded(result: FingerprintManagerCompat.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
// Handle authentication success
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
// Handle authentication failure
}
}, null)
}
}
override fun onStop() {
super.onStop()
cancellationSignal.cancel()
}
}
This code initializes a FingerprintManagerCompat object and uses it to authenticate the user’s fingerprint. It handles success, failure, and error callbacks while ensuring that the process is canceled when the activity stops.
Comparison
Compared to AndroidX Biometric Library:.
- FingerprintManagerCompat focuses solely on fingerprint authentication, whereas the AndroidX Biometric Library supports various biometric methods.
- Offers backward compatibility down to API level 23, while the AndroidX Biometric Library works from API level 29.
- It is a bit more outdated compared to the modern approach provided by the AndroidX Biometric Library.
Reference
Additional Libraries
Here are some additional Android libraries that you can explore for biometric authentication:
- Android Biometric Library by valartech: A simple library that wraps the BiometricPrompt API and provides a simplified usage.
- BiometricPromptCompat by veggie686: A compatibility library that allows you to use the BiometricPrompt API on devices running older versions of Android.
- FingerprintAuth by Jaely: A lightweight library for fingerprint authentication with support for both the FingerprintManager API and the BiometricPrompt API.