Tuesday, August 02, 2011

JUnit "java.lang.VerifyError" failure in Android and Maven

JUnit test suites in Android constitute a separate Android project that is deployed alongside the application under testing [1]. Users often make the mistake of adding external libraries as dependencies to both projects which leads to java.lang.VerifyError failure caused by the conflicting external library. In case of Eclipse, the standard solution is to add the external library as dependency to only the main project and then export it to the test project [2]. But unfortunately, this solution is not applicable when Maven is used to run the test suite [3].

In case of Maven, the user should add the external library as dependency to the pom.xml of both projects but with different scope options.
For example, if the external dependency is Google Protobufs, this should be added to the main project pom.xml:

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.2.0</version>
<scope>compile</scope>
</dependency>

while to the test project pom.xml add:

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.2.0</version>
<scope>provided</scope>
</dependency>

A compile scope dependency is propagated to all classpaths of the project and at runtime is available to dependent projects which list the dependency scope as provided.

1 comment:

  1. Thank you very mush. This post is helped for me alot

    ReplyDelete