【问题标题】:Android VerifyError exceptionAndroid VerifyError 异常
【发布时间】:2017-06-20 18:19:44
【问题描述】:

我在我的应用程序中使用指纹传感器。 我知道 api 可用于棉花糖及以上操作系统。因此,在我的课堂上运行时,我正在动态检查 sdk 版本。

即使指纹代码不执行我在 4.0 android os 上也面临以下异常,而在 5.0 及更高版本上执行相同。

**java.lang.VerifyError: com/cloudzon/gratzeez1/GiveGratuityActivity
                                                                        at java.lang.Class.newInstanceImpl(Native Method)
                                                                        at java.lang.Class.newInstance(Class.java:1215)
                                                                        at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2265)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                        at android.os.Looper.loop(Looper.java:193)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5322)
                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:515)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
                                                                        at dalvik.system.NativeStart.main(Native Method)**

我发现由于我班级中的以下代码,我遇到了这个问题。

public boolean cipherInit() {
    try {
        cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        throw new RuntimeException("Failed to get Cipher", e);
    }

    try {
        keyStore.load(null);
        SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME,
                null);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return true;
    } catch (KeyPermanentlyInvalidatedException e) {
        return false;
    } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
        throw new RuntimeException("Failed to init Cipher", e);
    }
}

【问题讨论】:

  • 您确定相同的代码在 5.0 中也可以使用吗?
  • 是的,它在 5.0 中工作正常

标签: android android-fingerprint-api


【解决方案1】:

我也遇到了这个问题,另一个答案让我走上了正轨; How to Use Unsupported Exception for Lower Platform Version

我把那个方法改成;

public boolean cipherInit() {
    try {
        cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        throw new RuntimeException("Failed to get Cipher", e);
    }

    try {
        keyStore.load(null);
        SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, null);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return true;
    } catch (Exception e) {
        if(e instanceof KeyPermanentlyInvalidatedException)
            return false;
        else if(e instanceof KeyStoreException|e instanceof CertificateException|e instanceof UnrecoverableKeyException|e instanceof IOException|e instanceof NoSuchAlgorithmException|e instanceof InvalidKeyException)
            throw new RuntimeException("Failed to init Cipher",e);
    }
    return false;
}

将 KeyPermanentlyInvalidatedException 的 catch 块从 (KeyPermanentlyInvalidatedException e) 更改为 (e instanceof KeyPermanentlyInvalidatedException) 似乎可以解决问题。

【讨论】:

  • 第一次遇到这个错误。似乎没有人在这里找到答案。我在创建类的新实例时将它放在我的应用程序模块中
  • 如果您的错误与将密码和/或指纹代码添加到您的 Android 项目无关,那么我建议您打开一个新问题。
【解决方案2】:

我只是通过更改 catch 异常块来解决它。 KeyPermanentlyInvalidatedException 是InvalidKeyException 的子类,如果我们使用KeyPermanentlyInvalidatedException,我们会得到VerifyError。

catch (KeyPermanentlyInvalidatedException e) {
(...)
}

改为使用 InvalidKeyException

catch (InvalidKeyException e) {
(...)
}

@opt05 回答帮助我解决了我的问题,但我不喜欢他使用instance of 的方法。

【讨论】:

  • +1 反对instanceof。它有用途,虽然 Android 已经是一团糟,但通常有更好的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多