Android java.lang.IllegalAccessError:预验证类中的类引用在运行测试项目时解析为意外实现?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11155340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation getting while running test project?
提问by ADIT
I have implemented project by using third party library(zxing) after implementation project is working fine then after I have written one test project to unit test my project.After running the test project ,the main project ,classes and it's methods are not giving any errors but if any zxing framework class is utilyzed within that method of the main project there getting the above error at run time not yet compile time.Please tell me how to resolve this issue?
我已经使用第三方库(zxing)实现了项目,在实现项目工作正常之后,我写了一个测试项目来对我的项目进行单元测试。运行测试项目后,主项目、类和它的方法没有给出任何错误,但如果在主项目的该方法中使用了任何 zxing 框架类,则会在运行时尚未编译时出现上述错误。请告诉我如何解决此问题?
回答by Dasari
You are getting this error because of third party library reference added two times. You have added the application path in the build path of test project. so the library reference automatically added to test project". Remove any library reference in the test project under properties->android.
由于第三方库引用添加了两次,您会收到此错误。您已经在测试项目的构建路径中添加了应用程序路径。所以库引用自动添加到测试项目中”。在属性->android 下删除测试项目中的任何库引用。
FYI, click here for detail explanation.
仅供参考, 请单击此处查看详细说明。
回答by Hossein Shahdoost
It is because zxing jar files are being loaded twice, You must set the zxing library as "Provided" (if you are compiling your code using Maven) in compile time, so it does not add the library to your bytecode. that way you wouldn't get the error
这是因为 zxing jar 文件被加载两次,您必须在编译时将 zxing 库设置为“提供”(如果您使用 Maven 编译代码),因此它不会将该库添加到您的字节码中。这样你就不会得到错误
回答by Jesús Castro
Unfortunately, the best solution which i have seen, it's to use a script with these code lines and using Espresso v2.0:
不幸的是,我见过的最好的解决方案是使用带有这些代码行的脚本并使用 Espresso v2.0:
adb shell setprop dalvik.vm.dexopt-flags v=n,o=v
adb shell stop installd
adb shell start installd
Execute it before you begin to test. It's only necessary to do it once time.
在开始测试之前执行它。只需要做一次。
回答by Fedor
I changed test project setting in Intelij Idea. Go to Modules -> Dependencies, then set scope of the tested project to 'Provided'.
我在 Intelij Idea 中更改了测试项目设置。转到 Modules -> Dependencies,然后将测试项目的范围设置为“提供”。
回答by Janusz
I got this error because I was working with Guava and Espresso also contains Guava.
我收到此错误是因为我正在使用 Guava,而 Espresso 也包含 Guava。
If you use Gradle and Android Studio you can exclude packages from the dependency like this:
如果您使用 Gradle 和 Android Studio,您可以像这样从依赖项中排除包:
androidTestCompile('com.jakewharton.espresso:espresso:1.1-r3') {
exclude group: 'com.google.guava'
}
回答by user3167973
Uncheck the jars (in my case maps and google play services jar) from "Order and Export" tab under Project's Java build path in Eclipse.But let them be added in "Libraries" tab. Click OK, Clean projects and build now. It works.
从 Eclipse 中 Project 的 Java 构建路径下的“Order and Export”选项卡中取消选中 jars(在我的例子中是地图和 google play services jar)。但是让它们添加到“Libraries”选项卡中。单击确定,清理项目并立即构建。有用。
回答by Volodymyr Machekhin
I have this error sometimes. Just the build->clean project helped me
我有时会遇到这个错误。只是 build->clean 项目帮助了我
回答by azizbekian
If you do not specifically need a device with low API version (<=19), you can leave transitive dependencies and create an emulator with higher API version.
如果您不是特别需要低 API 版本 (<=19) 的设备,您可以保留可传递的依赖关系并创建一个具有更高 API 版本的模拟器。
This happens only on devices which API is lower than Lollipop (API 21). Running tests on emulators higher/equal to API 21 runs normally, without issues.
这只发生在 API 低于 Lollipop (API 21) 的设备上。在高于/等于 API 21 的模拟器上运行测试正常运行,没有问题。
回答by Cüneyt
I have this error on api level lower than 21. Just the build->rebuild project helped me.
我在低于 21 的 api 级别上有这个错误。只是 build->rebuild 项目帮助了我。