31

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?

ADIT
  • 2,388
  • 9
  • 34
  • 46

9 Answers9

33

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.

FYI, click here for detail explanation.

Juri
  • 32,424
  • 20
  • 102
  • 136
Dasari
  • 438
  • 4
  • 11
11

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

Hossein Shahdoost
  • 1,692
  • 18
  • 32
9

Unfortunately, the best solution which i have seen, it's to use a script with these code lines and using 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.

Jesús Castro
  • 2,061
  • 1
  • 22
  • 26
6

I changed test project setting in Intelij Idea. Go to Modules -> Dependencies, then set scope of the tested project to 'Provided'.

Fedor
  • 43,261
  • 10
  • 79
  • 89
4

I got this error because I was working with Guava and Espresso also contains Guava.

If you use Gradle and Android Studio you can exclude packages from the dependency like this:

androidTestCompile('com.jakewharton.espresso:espresso:1.1-r3') {
   exclude group: 'com.google.guava'
}
Janusz
  • 187,060
  • 113
  • 301
  • 369
3

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.

user3167973
  • 89
  • 2
  • 7
2

I have this error sometimes. Just the build->clean project helped me

2

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.

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.

azizbekian
  • 60,783
  • 13
  • 169
  • 249
0

I have this error on api level lower than 21. Just the build->rebuild project helped me.

Cüneyt
  • 2,565
  • 25
  • 31