How to Use Unsupported Exception for Lower Platform Version How to Use Unsupported Exception for Lower Platform Version android android

How to Use Unsupported Exception for Lower Platform Version


My guess is that either FingerprintCallback.Callback extends an API Level 23+ interface or that LoginFragment has fields that reference API Level 23+ stuff.

Your rule about being able to call API Level 23+ methods safely inside the version guard block is correct. However, you cannot:

  • inherit from classes that do not exist on the device
  • implement interfaces that do not exist on the device
  • have fields whose types do not exist on the device
  • accept constructor or method parameters whose types do not exist on the device (where we actually call these)
  • have method return values whose types do not exist on the device (where we actually call these)

In many cases, we don't need any of that, in which case just checking Build.VERSION.SDK_INT before calling API Level 23+ methods is sufficient.

If you need to do some of the things in the bulleted list, that's fine, but then you need to isolate those into classes that you only use on API Level 23+ devices.

So, for example, let's pretend that the problem is that FingerprintCallback.Callback extends some API Level 23+ interface. Rather than implementing FingerprintCallback.Callback on the LoginFragment, you might implement that as an anonymous inner class, and only execute the code creating that anonymous inner class instance if Build.VERSION.SDK_INT is high enough. Then, you only are referencing FingerprintCallback.Callback on the newer devices, and you should be safe.


I had the same error and solved it the following way:

catch (Exception e) {    if (e instanceof KeyPermanentlyInvalidatedException) {        //your error handling goes here    }

It isn't vey nice, but it works


As you said the problem is with that catch block

catch (KeyPermanentlyInvalidatedException exception) {    Timber.w(exception, "A new fingerprint was added to the device");    handleKeyPermanentlyInvalidated();    return false;}

Because that exception is added on API LEVEL 23, but I don't know why the verify error is thrown at initialization time itself.

Anyway you can catch the exception using

catch (InvalidKeyExceptionexception) {    ....    return false;}

since KeyPermanentlyInvalidatedException extends InvalidKeyExceptionexception