问题
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.
回答1:
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?
回答2:
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()).
回答3:
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.
回答4:
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).
- Create a directory in your project (e.x. "libs") and put library jar there.
- Add the directory to the build class path by (click right button on the folder and select "Build path"->"Use as source folder").
- Rebuild your project.
回答5:
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:
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="4"/>
回答6:
I found an interesting case. I use:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="18" />
So some of new Android 4 capabilities are not implenented in Android 2.3 like ImageView.setLayerType
. To avoid runtime error simply:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
This approach should be used also with exceptions handling:
} catch (NetworkOnMainThreadException nomte) {
// log this exception
} catch (SocketTimeoutException socketTimeoutException) {
// log this exception
}
NetworkOnMainThreadException
is not implemented in Android 2.3 so when the class is loaded (and not before!) the exception java.lang.VerifyError
occurs.
回答7:
If you're using Retrolambda you might have added a static method to an interface (which is only allowed in Java 8).
回答8:
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.
public class MyApplication extends MultiDexApplication
Step3: Override attachBaseContext
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
Step4: The next step is to add the following to the android part of your apps build.gradle
dexOptions {
preDexLibraries = false
}
Step5: Lastly, following to the general part of your apps build.gradle
afterEvaluate {
tasks.matching {
it.name.startsWith('dex')
}.each { dx ->
if (dx.additionalParameters == null) {
dx.additionalParameters = ['--multi-dex']
} else {
dx.additionalParameters += '--multi-dex'
}
}
}
For details, please checkout
https://developer.android.com/tools/building/multidex.html
回答9:
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.
回答10:
I downgrade gradle version from 2.0.0-alpha2 to 1.5.0 that solved this problem.
回答11:
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.
回答12:
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:
- created a libs/ folder and added the jars.
- right click > add as source folder
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.
回答13:
In Eclipse 4.x
, if you encounter this problem, try below:
- migrate all included 3th-party jars into the User-Libaray
- move up the user lib before the android lib and check it in the Order and Export tab
- clean and rebuild to run
回答14:
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".
回答15:
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.
回答16:
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.
回答17:
If you have tests, try commenting out this line from your build.grade
file:
testCoverageEnabled = true
For me this caused VerifyError exceptions on classes which use Java 1.7 features, particularly string switch statements.
回答18:
I had the same problem after making a git pull.
Solution: Build -> Clean Project.
Hope this helps.
回答19:
I have found another case.
Conditions:
- Use Retrolambda (not sure if it's necessary);
- Make a static method in an interface.
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.
回答20:
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 shows java.lang.VerifyError
compile 'com.fasterxml.jackson.core:jackson-databind:2.2.+'
compile 'com.fasterxml.jackson.core:jackson-core:2.2.+'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.2.+'
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.
compile 'com.fasterxml.jackson.core:jackson-annotations:2.7.0-rc3'
compile 'com.fasterxml.jackson.core:jackson-databind:2.7.0-rc3'
回答21:
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
回答22:
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.
回答23:
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.
Uncaught handler: thread main exiting due to uncaught exception
java.lang.VerifyError: com.j256.ormlite.dao.BaseDaoImpl$DaoConfigArray
at com.j256.ormlite.dao.BaseDaoImpl$1.initialValue(BaseDaoImpl.java:71)
at com.j256.ormlite.dao.BaseDaoImpl$1.initialValue(BaseDaoImpl.java:1)
at java.lang.ThreadLocal$Values.getAfterMiss(ThreadLocal.java:429)
at java.lang.ThreadLocal.get(ThreadLocal.java:66)
On that line I was trying to do a new DaoConfigArray
and that class had the following line:
// copyOf is only supported in Java >= 1.6
doArray = Arrays.copyOf(daoArray, newLength);
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.
private static final ThreadLocal<DaoConfigArray> daoConfigLevelLocal
= new ThreadLocal<DaoConfigArray>() {
@Override
protected DaoConfigArray initialValue() {
return new DaoConfigArray();
}
};
回答24:
I had to remove dependent projects and instead compile dependent projects are jar's and include them in the libs folder.
回答25:
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:
public class A { ... }
public class B extends A { ... }
public class C extends A { ... }
And a method that did:
A[] result = null;
if (something)
result = new B[cursor.getCount()];
else
result = new C[cursor.getCount()];
// Fill result
...
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.
回答26:
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.).
回答27:
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.
回答28:
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.
回答29:
For me it was in correlation between compileSdkVersion and buildToolsVersion. I had:
compileSdkVersion 21
buildToolsVersion '19.1.0'
I changed it to:
compileSdkVersion 21
buildToolsVersion '21.1.2'
回答30:
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):
compileSdkVersion 21
the java.lang.verifyerror happened. So I changed the compileSdkVersion to 19
compileSdkVersion 19
It worked well. I think that it might be the problem of SDK buildTools, and it seems OK when API level < 21.
来源:https://stackoverflow.com/questions/668788/android-java-lang-verifyerror