First of all, there are at least 2 postings with the same problem but these solutions don\'t work anymore, at least not in my installation.
I\'m using m2e with Eclip
I found the solution. It depends on the JUnit version, because JUnit 4.10 adds JUnit library and hamcrest jar library, although JUnit 4.10 already contains all the hamcrest classes, so hamcrest exists twice then. If I switch back to JUnit 4.8.1, it doesn't add hamcrest as library and the error is gone.
This solution is actually a workaround. Usually the Eclipse Maven plugin should handle this, but Hamcrest/JUnit is a special problem, because JUnit includes Hamcrest, not as dependency, but in code.
Unfortunately Eclipse does not understand the maven test scope. It pulls hamcrest classes into the build with JUnit 4.10. Use JUnit 4.8.1 or, if you really need to use 4.10+ version of JUnit, you can use a maven profile without the junit dep in eclipse.
Here is the relevant part of the pom.xml:
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>no-junit</id>
</profile>
</profiles>
I tried the above solutions and still got the error. Only after some more try and error I found out that the hamcrest classes are also contained in another jar: mockito (I was not yet aware that mockito won't work with my instrumentation tests)
So I solved my problem by removing mockito-all.jar from my dependencies and excluded hamcrest from the transitive dependencies of junit like this:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>4.10</version>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
</dependency>
This exclusion may also be needed for commons-logging (as of the date of writing) because otherwise the apk builder will protest about old classes.