Java.lang.IllegalArgumentException: 已经添加:Lorg/hamcrest/BaseDescription; 转换为Dalvik格式失败,错误代码为1。

11

首先,至少有两篇帖子讨论了相同的问题,但这些解决方案不再起作用,至少在我的安装中是这样。

我正在使用带有Eclipse和Android的m2e,并尝试通过选择运行为->Android应用程序来运行该应用程序,但我总是会遇到这个错误:

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

这就是Tools R14部分中所描述的问题。首先,这无法修复,因为我在ADT 20.0.3中遇到了此问题。其次,我没有这些所谓的“_src”文件夹。我以前从未在Maven项目中见过它们,因此我不知道现在该怎么办。我甚至没有将任何库链接两次。至少我在我的项目中没有看到这种情况。有什么想法可以让这个工作起来吗?

以下是我的pom.xml,如果这有用的话:

<?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>

我使用的是Eclipse Juno,ADT 20.0.3,m2e 1.1.0。

3个回答

13
我尝试了上述的解决方案,但仍然遇到了错误。只有在更多的尝试和错误后,我发现 hamcrest 类库也包含在另一个 jar 包中:mockito(此时我还不知道 mockito 不能与我的 instrumentation 测试一起使用)。
因此,我通过从依赖关系中删除 mockito-all.jar,并像这样从 junit 的传递依赖中排除 hamcrest 来解决了我的问题:
 <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>

因为否则apk构建程序会因旧类而抗议, 所以在写作日期之前,也可能需要对commons-logging进行这种排除。


适合我的需求的唯一解决方案。我已经访问了谷歌上的每个链接,但都没有找到。谢谢。 - tomrozb
1
根据https://code.google.com/p/mockito/wiki/DeclaringMockitoDependency#mockito-core_VS_mockito-all,我刚刚将依赖从mockito-all切换到了mockito-core,这实际上做了同样的事情,即将hamcrest-core从mockito中移除,但无需添加排除。 - Jon Adams

12

我找到了解决方案。这取决于JUnit版本,因为JUnit 4.10添加了JUnit库和hamcrest jar库,尽管JUnit 4.10已经包含所有的hamcrest类,所以就存在了两个hamcrest库。如果我切换回JUnit 4.8.1,它不会作为库添加hamcrest,错误就消失了。

这种解决方案其实是一种变通方法。通常Eclipse Maven插件应该处理这个问题,但是Hamcrest/JUnit是一个特殊的问题,因为JUnit包括Hamcrest库,但不是作为依赖项而是在代码中。


2
非常准确!在我的情况下,我试图将ActionBarSherlock 4.2.0作为库项目使用,这会将JUnit 4.10作为依赖项带到我的项目中!在ActionBarSherlock Library pom.xml中将JUnit从4.10更改为4.8.1即可解决问题。非常感谢! - Dan Dar3
2
如果您希望继续使用4.10版本,则可以明确地将hamcrest jar排除为JUnit的传递依赖项。以下是在Gradle中执行此操作的方法:instrumentTestCompile('junit:junit:4.10') { exclude group: 'org.hamcrest' }有关Maven等效方法,请参见此处:http://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html - Brendan

1

很遗憾,Eclipse不理解Maven测试范围。它会在JUnit 4.10中将hamcrest类引入构建中。请使用JUnit 4.8.1或者,如果您确实需要使用4.10+版本的JUnit,则可以在Eclipse中使用没有junit依赖的Maven配置文件。

  1. 在默认配置文件中包含JUnit依赖项,并创建一个空的“no-junit”配置文件(参见下面的示例pom)
  2. Eclipse项目属性> Maven > 活动Maven配置文件:no-junit
  3. 手动将junit库添加到您的项目的Eclipse构建路径中

这是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>

1
第三点是一个非常糟糕的想法。这是一个Maven项目,因此Maven必须管理项目本身。如果您开始手动添加库,最终没有人能够复制您所做的事情。 - Bevor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接