5

I am trying to implement Biometric prompt API to authenticate user using fingerprint verification. I am successfully able to integrate Biometric prompt and it is working on andorid 9.0. But as the documentation suggests Biometric api is also backwards compatible, but when I build dialog using below code it shows API support warning.

Call requires API level 28 (current min is 15): new android.hardware.biometrics.BiometricPrompt.Builder less... (Ctrl+F1) This check scans through all the Android API calls in the application and warns about any calls that are not available on all versions targeted by this application (according to its minimum SDK attribute in the manifest)

mBiometricPrompt = new BiometricPrompt.Builder(this)
                        .setDescription("Description")
                        .setTitle("Title")
                        .setSubtitle("Subtitle")
                        .setNegativeButton("Cancel", getMainExecutor(), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Log.i(TAG, "Cancel button clicked");
                            }
                        })
                        .build();

What can I do to make this work on lower apis? Here is Screenshot. enter image description here

karan
  • 8,637
  • 3
  • 41
  • 78
  • Looks like keep using old way for below Android 9 – Rohit5k2 Dec 03 '18 at 08:31
  • but it is mentioned that there is support library available, but I am unable to find it anywhere. If using for both should I keep both libraries like newer api for andorid 9 and older fingerprint api for android 8.0 and lower? – karan Dec 03 '18 at 08:33
  • https://stackoverflow.com/questions/51558350/android-biometricprompt-compat-library – Rohit5k2 Dec 03 '18 at 08:34
  • Probably this would work https://mvnrepository.com/artifact/androidx.biometric/biometric – Rohit5k2 Dec 03 '18 at 08:39
  • no one has a clear answer to this .. as how to add dependancy .. please do not give one line answer and write a detailed answer about how to add dependancy for biometric in a 2 year old project . see my question for detailed queries : https://stackoverflow.com/questions/55052336/cant-add-androidx-biometric-dependency-to-app-level-build-gradle-file/55052566#55052566 – YogiAR Mar 07 '19 at 22:06

3 Answers3

4

Looks like Biometric Prompt API for older version is still in alpha. If you are ok with an alpha version you can use this in build.gradle

compile group: 'androidx.biometric', name: 'biometric', version: '1.0.0-alpha02'

Source: https://mvnrepository.com/artifact/androidx.biometric/biometric/1.0.0-alpha02

There are only two versions listed here

  • 1.0.0-alpha01
  • 1.0.0-alpha02

Source: https://mvnrepository.com/artifact/androidx.biometric/biometric

As per the library description, it says

The Biometric library is a static library that you can add to your Android application. It invokes BiometricPrompt on devices running P and greater, and on older devices will show a compat dialog. Compatible on devices running API 14 or later.

Which would mean that all you need is this compat library and it would work on all version of Android. No need to keep two different version for above Android 9 and below Android 9.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • 1
    I added the required dependency but it is not recognizing builder constructor. – karan Dec 03 '18 at 09:02
  • I was able to make builder but failing to attach authentication callback any ideas about this? – karan Dec 03 '18 at 09:08
  • Sorry, @KaranMer haven't got a chance to explore this yet. – Rohit5k2 Dec 03 '18 at 09:24
  • thanks anyways I will be working on it but due to very less documentation it is very difficult to implement. – karan Dec 03 '18 at 09:27
  • Will it still use Fingerprint API for >= Android 6 devices? – 最白目 Jan 24 '19 at 05:45
  • I am not able to add it at all into my cordova android project and this is frushtrating for me .. i even can not add the simple line : implementation 'androidx.biometric:biometric:1.0.0-alpha03' . -- gives error .. here is my question , the one who helps will be rewarded by 200 reputation point from me .. https://stackoverflow.com/questions/55052336/cant-add-androidx-biometric-dependency-to-app-level-build-gradle-file/55052566#55052566 – YogiAR Mar 07 '19 at 21:28
3

Here is my implementation for androidx.biometric, which Rohit5k2 suggested. It's Kotlin, but i'm sure it won't be a problem. Hope this helps

fun FragmentActivity.auth(successCallback: () -> Unit, cancelSignal: CancellationSignal = CancellationSignal()) {
    if (Build.VERSION.SDK_INT < 23) {
        successCallback()
        return
    }

    val biometricPrompt = BiometricPrompt(this, MainThreadExecutor(), object : BiometricPrompt.AuthenticationCallback() {
        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
            super.onAuthenticationSucceeded(result)
            successCallback()
        }

        override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
            super.onAuthenticationError(errorCode, errString)
            if (errorCode == ERROR_NEGATIVE_BUTTON) {
                cancelSignal.cancel()
            }
        }
    })

    val promptInfo = BiometricPrompt.PromptInfo.Builder()
            .setTitle(getString(R.string.title))
            .setSubtitle(getString(R.string.auth))
            .setDescription(getString(R.string.biometric_desc))
            .setNegativeButtonText(getString(R.string.biometric_negative))
            .build()

    biometricPrompt.authenticate(promptInfo)
}

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

    override fun execute(r: Runnable) {
        handler.post(r)
    }
}
Ruslan Aliev
  • 73
  • 1
  • 7
  • I am not able to add it at all into my cordova android project and this is frushtrating for me .. i even can not add the simple line : implementation 'androidx.biometric:biometric:1.0.0-alpha03' . -- gives error .. here is my question , the one who helps will be rewarded by 200 reputation point from me .. https://stackoverflow.com/questions/55052336/cant-add-androidx-biometric-dependency-to-app-level-build-gradle-file/55052566#55052566 – YogiAR Mar 07 '19 at 21:28
-1

If you have an old android project ( even if its a hybrid android app ) follow these detailed steps :

  1. So I had a Cordova android hybrid app old code . using Android studio 3.2 . Build gradle version was very old , 2.8 and gradle version was 3.3.

  2. I first of all upgraded Android studio to latest version 3.3.2

  3. Now i decided to migrate the whole project to androidX. Remember it wont even let me do that with the previous version of Android studio, i dont know why.

  4. When i clicked on Refactor -> Migrate to AndroidX. A pop up appeared saying "Upgrade the gradle version. So now I updated gradle version to 4.10.1 , it is still giving me error if i upgrade it to 5.2 ( i dont know why , I am still new to Android). Also updated build gradle to 3.3.2

5.My build.gradle (Module : App) looks like this :

apply plugin: 'com.android.application'

buildscript {
    repositories {
        jcenter{ url "http://jcenter.bintray.com/" }
        google()
    }

    // Switch the Android Gradle plugin version requirement depending on the
    // installed version of Gradle. This dependency is documented at
    // http://tools.android.com/tech-docs/new-build-system/version-compatibility
    // and https://issues.apache.org/jira/browse/CB-8143
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        classpath "com.google.gms:google-services:3.0.0" //FCM Config

    }
   }
  1. Now App is syncing fine , Build ok. I again tried Refactor -> Migrate to androidX. This time Android studio started refactoring the code and provided me 70 code change suggestions .

  2. These code changes are mainly the header file changes like : import "" . So I opened this link - https://developer.android.com/jetpack/androidx/migrate and changed every import statement to the equal androidx statment.

  3. After copy pasting all the changes I again compiled and synced the code . after 3 resources and code compilation error , I was able to build the code . This whole process took 1.2 hours .

  4. Finally i was able to import the biometric support API in build-extras.gradle (Module : app) , look at the file :

        dependencies {
            api 'androidx.appcompat:appcompat:1.0.2'
            api "com.squareup.picasso:picasso:2.4.0"
            api "com.google.android.material:material:1.1.0-alpha04"
            api "com.google.firebase:firebase-messaging:9.2.0" //FCM Config
            api 'com.rmtheis:tess-two:6.0.2'
            api 'com.github.bumptech.glide:glide:3.8.0'
            api 'androidx.legacy:legacy-support-v4:1.0.0'
    
            api "androidx.biometric:biometric:1.0.0-alpha03"
        }
    }
    
  5. Finally , I was able to build the complete code and sync it . So happy finally did it. Now i just have to use biometric API functions to integrate it into my code ( notice this code was written 3 years ago and given to me for integrating latest biometric API).

Yes I needed step by step answer like this one.

Still thanks to all who helped.

YogiAR
  • 2,207
  • 23
  • 44