4

Android P introduced changes to the Biometrics API.
Now we should use BiometricPrompt class to integrate biometric authentication in our apps (FingerprintManager is deprecated).

The issue is that this class is only available on API 28.

Biometrics documentation says:

A support library is also provided for devices running Android O and earlier, allowing applications to utilize the advantages of this API across more devices .

But I can't find that support library.
Does it exist? Or will be added in future implementations?

Juan Cruz Soler
  • 8,172
  • 5
  • 41
  • 44
  • Don't know if you found it already, but `1.0.0-alpha01` was released in September. Latest version is `1.0.0-alpha02`. Add this to your build.gradle: `implementation 'androidx.biometric:biometric:1.0.0-alpha02'`. ([Maven](https://mvnrepository.com/artifact/androidx.biometric/biometric)) – JayShortway Oct 05 '18 at 14:48
  • @JayShortway Thanks! Add it as an answer please, so I can accept it – Juan Cruz Soler Oct 05 '18 at 19:53
  • Happy to help :) I just added it as an answer, thanks! – JayShortway Oct 06 '18 at 09:13

2 Answers2

8

I think at the time of your question, it was missing. 1.0.0-alpha01 was released in September. Latest version as of now is 1.0.0-alpha02.
Add this to your build.gradle:

implementation 'androidx.biometric:biometric:1.0.0-alpha02'

(Maven Repository)

JayShortway
  • 266
  • 1
  • 3
  • 8
  • when i add this in my build.gradle file , i always have an error : Could not find method implementation() for arguments [androidx.biometric:biometric:1.0.0-alpha03] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. I have gradle version 4.10. But i am using Cordova android ( version 5.1.0) ... I am fed up with the issue .. please help – YogiAR Mar 07 '19 at 20:22
  • @Rakesh Which version of Android Studio are you using? You could try replacing 'implementation' with 'compile' to see if it works, but 'compile' is actually deprecated. – JayShortway Mar 08 '19 at 18:56
4

As @JayShortway answered the backwards-compatible dependency is:

implementation 'androidx.biometric:biometric:1.0.0-alpha02'

But the implementation is different from the android.hardware.biometrics.BiometricPrompt implementation:

class BiometricsManagerImpl {

    private val executor = MainThreadExecutor()

    override fun authenticate(activity: FragmentActivity) {
        val prompt = BiometricPrompt(
            activity,
            executor,
            object : BiometricPrompt.AuthenticationCallback() {
                override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {                        
                    super.onAuthenticationError(errorCode, errString)
                    // Handle authentication errors
                }

                override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
                    super.onAuthenticationSucceeded(result)
                    // Authentication Succeeded
                }
            })

        val promptInfo = BiometricPrompt.PromptInfo.Builder()
            .setTitle("Biometric Authentication")
            .setDescription("Please authenticate in order to verify your identity")
            .setNegativeButtonText("Cancel")
            .build()

        prompt.authenticate(promptInfo)
    }

    inner class MainThreadExecutor : Executor {
        private val handler = Handler(Looper.getMainLooper())

        override fun execute(runnable: Runnable) {
            handler.post(runnable)
        }
    }
}
Juan Cruz Soler
  • 8,172
  • 5
  • 41
  • 44