11

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 Eclipse and Android and tried to run the application as "Android Application" by selecting run as->Android application, but I always get this error:

UNEXPECTED TOP-LEVEL EXCEPTION: java.lang.IllegalArgumentException: already added: Lorg/hamcrest/BaseDescription;
. . .

[2012-09-08 19:50:41 - net.mydomain.project-TRUNK] Conversion to Dalvik format failed with error 1

It's the problem described here in Tools R14 section. First of all, this cannot be fixed because I have this issue in ADT 20.0.3. Secondly, I don't have these so called "_src" folders. I've never seen them in a Maven project before, so I don't know what I should do now. I don't even have any libraries linked twice. At least I don't see some in my project. Any ideas how to get this working?

Here is my pom.xml if this helps:

<?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven- v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.devgems.android</groupId>
    <artifactId>kurzparkzonewien</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>kurzparkzonewien</name>

    <properties>
        <platform.version>1.6_r2</platform.version>
        <android.sdk.path>/opt/android-sdk-linux</android.sdk.path>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>${platform.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!-- Make sure this is below the android dependencies -->
        <dependency>
            <groupId>com.pivotallabs</groupId>
            <artifactId>robolectric</artifactId>
            <version>1.0-RC1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <outputDirectory>target/classes</outputDirectory>
        <testOutputDirectory>target/test-classes</testOutputDirectory>

        <plugins>
            <plugin>
                 <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
                    <assetsDirectory>${project.basedir}/assets</assetsDirectory>
                    <resourceDirectory>${project.basedir}/res</resourceDirectory>
                    <nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
                    <sdk>
                        <platform>4</platform>
                        <path>${android.sdk.path}</path>
                    </sdk>
                    <undeployBeforeDeploy>true</undeployBeforeDeploy>
                </configuration>
                <extensions>true</extensions>
            </plugin>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
   </project>

I'm using Eclipse Juno, ADT 20.0.3, m2e 1.1.0.

yatul
  • 1,103
  • 12
  • 27
Bevor
  • 8,396
  • 15
  • 77
  • 141

3 Answers3

13

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.

Yashima
  • 2,248
  • 2
  • 23
  • 39
  • The only one solution that fit my needs. I've visited every link on Google and nothing. Thanks. – tomrozb Jun 12 '13 at 19:52
  • 1
    Per https://code.google.com/p/mockito/wiki/DeclaringMockitoDependency#mockito-core_VS_mockito-all I just switched the dependency from mockito-all to mockito-core, which effectively did the same thing of removing hamcrest-core from being bundled with mockito, but without having to add the exclusion. – Jon Adams Nov 10 '13 at 22:46
12

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.

Bevor
  • 8,396
  • 15
  • 77
  • 141
  • 2
    Spot on! In my case I was trying to use ActionBarSherlock 4.2.0 as a library project, and that was bringing JUnit 4.10 as a dependency to my project! Changing JUnit from 4.10 to 4.8.1 in ActionBarSherlock Library pom.xml fixed it. Thanks a lot! – Dan Dar3 Oct 16 '12 at 01:17
  • 2
    If you wish to continue using 4.10 then you can explicitly exclude the hamcrest jar as a transitive dependency of JUnit. This is how to do it in Gradle: instrumentTestCompile('junit:junit:4.10') { exclude group: 'org.hamcrest' } See here for the Maven equivalent: http://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html – Brendan Aug 22 '13 at 16:16
1

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.

  1. Include the JUnit dep in the default profile and create an empty 'no-junit' profile (see example pom below)
  2. Eclipse project properties > Maven > Active Maven Profiles: no-junit
  3. Manually add the junit library to the eclipse Build path of your project

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>
Bart Swennenhuis
  • 324
  • 2
  • 11
  • 1
    Point 3 is a very bad idea. This is a Maven project, so Maven has to adminsiter the project itself. If you begin to add libraries manually, nobody can eventually reproduce what you have done. – Bevor Oct 27 '12 at 09:21