拒绝 class 因为编译时验证失败 Android

Rejecting class because it failed compile-time verification Android

我的一个应用程序在启动时突然失败,并显示以下错误消息:

java.lang.VerifyError: Rejecting class com.sample.BufferManagerImpl because it failed compile-time verification (declaration of 'com.sample.BufferManagerImpl' appears in /data/app/com.sample.myapp-1/base.apk)

它只在使用 ART 虚拟机的设备上失败,但在 Dalvik 上不会

问题是由于 synchronized 块位于 try-catch 块内,例如:

try {
    synchronized (mLock) {
        updateState();
    }
} catch (IllegalStateException e) {
}

显然这不是一个好的做法,但只要我这样改变它,它就会起作用:

synchronized(mLock) {
    try {
        updateState();
    } catch (IllegalStateException e) {
    }
}

我在 android 5 遇到了这个问题。我的应用程序在 4 台或以下设备上运行正常,但在 android 5 台设备上我崩溃了。

我用多个 Threads 破坏了我的代码并且它修复了。 如果您的代码想要更改 UI 使用 handler .

 Thread Thread = new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            handler.post(new Runnable() {

                @Override
                public void run() {
                    use this if your codes will change the Ui

。 . . . .

在android studio 2.1中,instant 运行会导致这个问题,关闭instant 运行功能后运行即可。

文件 -> 首选项 > 构建执行 -> 部署 -> 即时 运行

禁用第一个复选框: 启用即时 运行 热插拔.....

如果您使用 Jack 进行构建,请确保它已从 build.gradle

中关闭
defaultConfig {
    ...
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    jackOptions {
        enabled false
    }
}

由于在 Android InstrumentationTest 中使用 Mockito,也可能会发生此错误。如果错误出现在模拟对象上,那么您必须将此行添加到 gradle-file:

androidTestCompile "com.crittercism.dexmaker:dexmaker:1.4"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.4"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.4"

这适用于 Mockito 1.10.15 和 1.10.19。