Android java.lang.VerifyError?
在我的 Android 应用程序中,我总是收到VerifyErrors! 我不明白为什么。 每当我包含外部 JAR 时,当我尝试启动我的应用程序时,我总是会遇到VerifyErrors(除了一次,当我包含 Apache Log4j 时)。
我通常通过获取库的源代码并将其添加到我的项目中来解决这个问题,但是我正在尝试放置 GData 客户端库。
我可以在源代码中获取它,但它的依赖项(mail.jar、activation.jar、servlet-api.jar)我不能,所以我收到验证错误。 我想一劳永逸地找出这个问题的根源。 我上网查了一下,好像都是说类文件不完整? 我不知道。
In my Android app, I always get VerifyErrors! And I cannot figure out why. Whenever I include a external JAR, I always get VerifyErrors when I try to launch my app (except for once, when I included Apache Log4j.)
I usually get around this by taking the source of the library and adding it to my project, but I am trying to put the GData client library.
I can get this in source, but it's dependencies (mail.jar, activation.jar, servlet-api.jar) I cannot, so I get verify errors. I would like to get to the root of this problem once and for all. I looked on the internet, but they all seem to talk about incomplete class files? which I do not know of.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
查看 LogCat 并查看导致验证错误的原因。 您正在使用的 Android SDK 级别可能不支持 java.lang 类中的某些方法(例如,String.isEmpty())。
Look at LogCat and see what's causing the verifyerror. It's probably some method in a java.lang class that is not supported on the android SDK level you are using (for instance, String.isEmpty()).
来自 android-developers:
“adb logcat 的输出" 表示不能的类
找到以及具有错误引用的类。 那个地点
被识别到特定的 Dalvik 指令。 诀窍是
查看异常上方的日志。
From android-developers:
The output from "adb logcat" indicates the class that could not be
found as well as the class that has the bad reference. The location
is identified down to the specific Dalvik instruction. The trick is
to look in the logs above the exception.
Android 使用不同的类文件格式。 您是否通过 Android SDK 附带的“dx”工具运行第 3 方 JAR 文件?
Android uses a different class file format. Are you running the 3rd party JAR files through the "dx" tool that ships with the Android SDK?
为了使其工作,您需要将库的 jar 添加到源文件夹之一(即使您已经将其添加为 eclipse 库,您仍然需要将其添加为源)。
(例如“libs”)并放入库jar
那里。
路径通过(单击右键
文件夹并选择“构建路径”->“使用
作为源文件夹”)。
To make it work you need to add jar of the library to one of the source folders (even if you have already added it as eclipse library, you still need to add it as source).
(e.x. "libs") and put library jar
there.
path by (click right button on the
folder and select "Build path"->"Use
as source folder").
这件事现在就发生在我身上。
导致该错误的原因是我使用了设备所具有的较新 SDK 中的方法。
Android 1.5 设备使用以下命令安装了 apk:
It happened to me right now.
The error was caused because I was using methods from a newer SDK that my device had.
Android 1.5 device installed an apk using this:
我发现了一个有趣的案例。 我使用:
所以一些新的 Android 4 功能在 Android 2.3 中并未实现,例如
ImageView.setLayerType
。 为了简单地避免运行时错误:这种方法也应该与异常处理一起使用:
NetworkOnMainThreadException
在Android 2.3中没有实现,所以当类加载时(而不是之前!)发生异常java.lang.VerifyError
。I found an interesting case. I use:
So some of new Android 4 capabilities are not implenented in Android 2.3 like
ImageView.setLayerType
. To avoid runtime error simply:This approach should be used also with exceptions handling:
NetworkOnMainThreadException
is not implemented in Android 2.3 so when the class is loaded (and not before!) the exceptionjava.lang.VerifyError
occurs.如果您使用的是 Retrolambda,您可能已向接口添加了静态方法(仅在 Java 8 中允许)。
If you're using Retrolambda you might have added a static method to an interface (which is only allowed in Java 8).
这也可能是由于 Lollypop 以下版本的引用限制错误而发生,其中最大大小限制为 65K 以上
问题的可能解决方案
第 1 步:
将 android-support-multidex.jar 添加到您的项目中。 该 jar 可以在您的 Android SDK 文件夹 /sdk/extras/android/support/multidex/library/libs
中找到。步骤 2:使用 MultiDexApplication 扩展您的应用程序,例如,
步骤 3:覆盖 AttachBaseContext
步骤 4:
下一步是将以下内容添加到应用程序 build.gradle 的 android 部分
Step5:
最后,按照您的应用程序 build.gradle 的一般部分
了解详细信息,请查看
https:// /developer.android.com/tools/building/multidex.html
This can also occur because of referencing limit error on Lollypop below versions, where it is limited upto max 65K size
Possible solution for above issue
Step1:
Add android-support-multidex.jar to your project. The jar can be found in your Android SDK folder /sdk/extras/android/support/multidex/library/libs
Step2: Extend your application with MultiDexApplication, for e.g.
Step3: Override attachBaseContext
Step4:
The next step is to add the following to the android part of your apps build.gradle
Step5:
Lastly, following to the general part of your apps build.gradle
For details, please checkout
https://developer.android.com/tools/building/multidex.html
就我而言,当我从 Eclipse Indigo 更新到 Eclipse Juno 时,就发生了这种情况:我不确定真正的原因是什么,但是,我长期从事的 Android 项目由于该异常而停止了工作。
经过许多小时的尝试解决该问题后,我找到了适合我的解决方案。
在我的 Android 项目中,我使用同一工作区中的其他项目(例如“MyUtils”)。 因此,我需要执行以下操作:
右键单击 Android 项目 -> 构建路径-> 配置构建路径
现在,转到“Order and Export”选项卡并选中“MyUtils”。 就是这样:我摆脱了这个恼人的异常。
In my case, it happened when I updated from Eclipse Indigo to Eclipse Juno: I'm not sure what is the true reason, but, my Android project that I'm working on for a long time stopped work because of that exception.
After many hours of trying to fix that, I found the solution for me.
In my Android project, I use other project (say, "MyUtils") that is in the same workspace. So, I needed to do the following:
Right click on Android project -> Build path -> Configure build path
Now, go to tab "Order and Export" and make "MyUtils" checked. That's it: I got rid of this annoying exception.
我将 gradle 版本从 2.0.0-alpha2 降级到 1.5.0 解决了这个问题。
I downgrade gradle version from 2.0.0-alpha2 to 1.5.0 that solved this problem.
该问题也可能是由两个 Android 项目之间不匹配引起的。 例如,如果您使用包“com.yourcompany”开发了一个 android 库,那么您的主应用程序项目将使用与基础包相同的包。 然后假设您想要更改主应用程序的版本,因此您更改了清单文件的值:版本代码和版本名称。 如果您运行应用程序而不更改库的这些值,则对库中对象的任何方法调用都会收到验证错误。
The problem could also be caused by a mismatch between two androids projects. For example if you have developed an android library using the package "com.yourcompany", Then you have the main application's project using the same package as base package. Then let say you want to change the version of your main app, so you change the manifest file's values: Version Code and Version name. If you run your app without changing those values for the library, you would get a verify error on any call of a method on a object from the library.
我遇到过同样的问题。 我正在使用 2.1 r1 进行构建,并使用新的 adt 17 更新到 2.1 r3。我在 javamail 的 mail.jar 上遇到了验证错误,这让我发疯。 这是我解决问题的方法:
我尝试重建但失败。 我删除了 libs/ 目录作为源文件夹,并删除了对构建路径中 3 个 jar 文件的引用。 然后我再次添加了 libs/ 文件夹,并将 libs/ 文件夹中的每个 jar 添加到构建路径。 现在它按预期工作了。 这是一个奇怪的解决方法,但它对我有用。
I had the same issue. I was building with 2.1 r1 and updated to 2.1 r3 with the new adt 17. I had verify errors on javamail's mail.jar and it was driving me crazy. Here is how i solved the issue:
i tried a rebuild and it failed. I removed the libs/ directory as a source folder and removed refs to the 3 jar files in the build path. Then i added the libs/ folder again, and added each jar in the libs/ folder to the build path. Now it works as expected. This is a weird workaround but it worked for me.
在 Eclipse 4.x 中,如果遇到此问题,请尝试以下操作:
In
Eclipse 4.x
, if you encounter this problem, try below:我在更新 SDK 后遇到了这个问题。 编译器对我的外部库有问题。 我这样做了:右键单击项目,然后“android Tools > add support library...”,此安装在我的项目库“android-support-v4.jar”上。
I have this issue after a SDK update. The compiler had problems with my external librarys. I did this: right click on project, then "android Tools > add suport library..." this install on my project library "android-support-v4.jar".
java.lang.VerifyError
表示您编译的字节码引用了 Android 在运行时找不到的内容。 即使我在两个设备中运行相同的版本,此 verifyError 仅向我发出 kitkat4.4 和较低版本,而不是上述版本。 当我使用旧版本的 jackson json 解析器时,它显示java.lang.VerifyError
然后我已将依赖项更改为 最新版本 2.2 到 2.7,没有 核心库(当我包含 core2.7 时,它给出了 verifyError),然后它就可以工作了。 这意味着core的方法和其他内容已迁移到最新版本的Databind2.7。 这解决了我的问题。
java.lang.VerifyError
means your compiled bytecode is referring to something that Android cannot find at runtime. This verifyError Issues me only with kitkat4.4 and lesser version not in above version of that even I ran the same build in both Devices. when I used jackson json parser of older version it showsjava.lang.VerifyError
Then I have changed the Dependancy to the latest version 2.2 to 2.7 without the core library(when I include core2.7 it gives the verifyError), then it works. which means the Methods and other contents of core is migrated to the latest version of Databind2.7. This fix my Issues.
我也得到了 VerfiyError...找不到真正的原因。 它有助于将新代码行包装到方法中(Eclipse,“提取方法...”)。 所以就我而言,原因不是不受支持的方法。
I get the VerfiyError as well... can't find a real reason. It helps to wrap the new lines of code into a method (Eclipse, 'Extract Method...'). So in my case the reason is not an unsupported method.
我有非常类似的问题。 我添加了 Apache POI jar,当我更新到 android SDK 22.3 时出现问题。
我检查了 Android 私有库,因此这不是 android SDK 的常见问题。 我取消选中所有 Apache POI jar 并一一添加。 我发现 poi-3.9-20121203.jar 应该在 poi-ooxml-3.9-20121203.jar 之前。 否则它将无法工作。
I had very similar problem. I had added Apache POI jars and problem appeared when I updated to android SDK 22.3.
I had Android Private Libraries checked so this was not the common problem with android SDK. I unchecked all Apache POI jars and added one by one. I found that poi-3.9-20121203.jar should be before poi-ooxml-3.9-20121203.jar. Otherwise it will not work.
如果您有测试,请尝试从
build.grade
文件中注释掉这一行:对我来说,这会导致使用 Java 1.7 功能的类(尤其是字符串 switch 语句)出现VerifyError 异常。
If you have tests, try commenting out this line from your
build.grade
file:For me this caused VerifyError exceptions on classes which use Java 1.7 features, particularly string switch statements.
进行 git pull 后我遇到了同样的问题。
解决方案:构建-> 清洁项目。
希望这可以帮助。
I had the same problem after making a git pull.
Solution: Build -> Clean Project.
Hope this helps.
我还发现了另外一个案例。
条件:
结果是繁荣! 尝试访问使用该接口的类时出现 java.lang.VerifyError 错误。 看起来 Android(在我的例子中是 4.4.*)不喜欢接口中的静态方法。 从接口中删除静态方法会使VerifyError消失。
I have found another case.
Conditions:
And the result is boom! java.lang.VerifyError when trying to access the class that uses that interface. Looks like Android (4.4.* in my case) doesn't like static methods in interfaces. Removing the static method from interface makes VerifyError go away.
我也遇到了这个问题,就像我的 jars 在用户库中一样...
我解决这个问题的方法是将它们添加到 lib 文件夹中,然后将它们添加到 eclipse 中的构建属性中...
我第一次这样做时不起作用,但后来我删除了它们并再次阅读它们,它开始工作......
有点奇怪! 但现在一直在工作。
祝你好运
I also had this problem, as had my jars in a user library...
The way I solved this was to add them to the lib folder and then add them in the build properties in eclipse...
The first time i did this it did not work, but then i removed them and readded them again and it started working...
bit of a strange one! but now working all the time.
Good Luck
我已经编写了 SDK 2.1 中的 Android API 方法/类,并尝试在 Android 1.6 模拟器上运行它。 所以我得到了那个错误。
解决方案:
将其更改为正确的模拟器版本。
这对我有用..谢谢。
I have coded Android API methods/class that are in SDK 2.1, and was trying to run it on Android 1.6 emulator. So i got that error.
SOLUTION:
Changed it to correct emulator version.
THIS WORKED FOR ME.. Thanks.
对于后人来说,我刚刚收到此错误是因为我使用的是
Arrays.copyOf()
这不是 Java 1.5 支持的方法,它对应于 Android Level 4。因为我正在运行包括在 1.6 下开发的库,所以它们编译得很好。 当我将有问题的类移至我的 Android 项目时,我才发现了问题——然后错误被突出显示。在那一行上,我试图执行一个
new DaoConfigArray
,该类有以下行:使它变得更加复杂的是第 71 行指向一个
ThreadLocal
初始化我最初认为这是问题的原因。For posterity, I just got this error because I was using
Arrays.copyOf()
which is not a method supported by Java 1.5 which corresponds to Android Level 4. Because I was running including libraries developed under 1.6 they compiled fine. I only saw the problems when I moved the class in question over to my Android project -- then the error was highlighted.On that line I was trying to do a
new DaoConfigArray
and that class had the following line:What made it even more complicated is that line 71 was pointing to a
ThreadLocal
initialization which I thought was the reason for the problem initially.我必须删除依赖项目,并将依赖项目编译为 jar 并将它们包含在 libs 文件夹中。
I had to remove dependent projects and instead compile dependent projects are jar's and include them in the libs folder.
我确信我的原因与您的不同,但由于这是搜索“Android java.lang.VerifyError”时的热门搜索之一,我想我应该将其记录在这里以供后代使用。
我有一些类:
和一个方法:
只要文件中存在此代码,第一次加载包含此方法的类时,我就会得到一个VerifyError。 将其分成两个单独的方法(一种仅处理 B 的方法,另一种仅处理 C 的方法)解决了该问题。
I'm sure that my cause was different than yours, but since this is one of the top hits when searching for "Android java.lang.VerifyError", I thought I'd record it here for posterity.
I had some classes along the lines of:
And a method that did:
As long as this code was present in the file, I would get a VerifyError the first time the class containing this method was loaded. Splitting it out into two separate methods (one that dealt only with B's, and one that dealt only with C's) fixed the problem.
就我而言,出现此错误是因为我的 google-play-service 不是最新的。
如果您的项目不支持 .jar 中的某些类,则会出现此错误(例如 ImageView.setLayerType、AdvertisingIdClient 等)。
In my case, this error occur because my google-play-service is not the newest.
If your project does not support some class in the .jar, this error occurs(ex. ImageView.setLayerType, AdvertisingIdClient, etc.).
我刚刚发现了它发生的另一种情况,不仅仅是由于库未dx'ed。
我有一个带有很长 doInBackground 方法的 AsyncTask。 由于某种原因,这个超过 145 行的方法开始崩溃。
它发生在 2.3 应用程序上。
当我只是将一些部分封装到方法中时,它工作得很好。
因此,对于那些无法找到未正确 dx 的类的人,请尝试减少方法的长度。
I just identified another situation that it occurs, not only due to libs not dx'ed.
I have a AsyncTask with a very long doInBackground mehtod. For some reason this method with more than 145 lines started to break.
It happened on a 2.3 app.
When I just encapsulated some parts into methods, it worked fine.
So for those that could not find the class that was not correctly dx'ed, try reducing the length of your method.
对我来说,问题实际上是我在类中的某个地方使用了 multi-catch 子句,这是 Java 7 功能(和 API 19+)。 因此,在所有 19 之前的设备上,它都会因
VerifyError
崩溃。For me, the issue ended up actually being that I was using multi-catch clause somewhere in the class which is a Java 7 feature (and API 19+). So it would crash with
VerifyError
on all pre-19 devices.对我来说,compileSdkVersion 和 buildToolsVersion 之间存在相关性。 我有:
我将其更改为:
For me it was in correlation between compileSdkVersion and buildToolsVersion. I had:
I changed it to:
对我来说,是compileSdkVersion的问题。 当我在特定的 Android 应用程序中使用 API 级别 21 时(https://github.com/android10 /Android-AOPExample):
发生了 java.lang.verify 错误。 所以我将compileSdkVersion更改为19,
效果很好。 我认为可能是SDK buildTools的问题,当API level < > 时似乎还可以。 21.
For me, it is the problem of compileSdkVersion. When I used the API level 21 in a specific android application (https://github.com/android10/Android-AOPExample):
the java.lang.verifyerror happened. So I changed the compileSdkVersion to 19
It worked well. I think that it might be the problem of SDK buildTools, and it seems OK when API level < 21.