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

Bev*_*vor 11 eclipse adt maven m2e eclipse-juno

首先,至少有2个帖子具有相同的问题,但这些解决方案不再起作用,至少在我的安装中不起作用.

我正在使用m2e与Eclipse和Android并尝试通过选择run as-> Android应用程序将该应用程序作为"Android应用程序"运行,但我总是收到此错误:

意外的顶级异常:java.lang.IllegalArgumentException:已添加:Lorg/hamcrest/BaseDescription;
...

[2012-09-08 19:50:41 - net.mydomain.project-TRUNK]转换为Dalvik格式失败,错误1

这是工具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>
Run Code Online (Sandbox Code Playgroud)

我正在使用Eclipse Juno,ADT 20.0.3,m2e 1.1.0.

Yas*_*ima 13

我尝试了上述解决方案仍然出错.经过一些尝试和错误之后,我发现hamcrest类也包含在另一个jar中:mockito(我还没知道mockito不能用于我的仪器测试)

所以我通过从依赖项中删除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>
Run Code Online (Sandbox Code Playgroud)

对于公共日志记录(截至撰写之日),也可能需要此排除,因为否则apk构建器将抗议旧类.


Bev*_*vor 12

我找到了解决方案.它取决于JUnit版本,因为JUnit 4.10添加了JUnit库和hamcrest jar库,虽然JUnit 4.10已经包含了所有的hamcrest类,所以hamcrest存在两次.如果我切换回JUnit 4.8.1,它不会将hamcrest添加为库,并且错误消失了.

此解决方案实际上是一种解决方法.通常Eclipse Maven插件应该处理这个问题,但Hamcrest/JUnit是一个特殊问题,因为JUnit包含Hamcrest,而不是依赖,而是代码.

  • 发现!在我的情况下,我试图使用ActionBarSherlock 4.2.0作为一个库项目,这将JUnit 4.10作为我的项目的依赖项!在ActionBarSherlock库中将JUnit从4.10更改为4.8.1修复了它.非常感谢! (2认同)
  • 如果您希望继续使用4.10,那么您可以明确地将hamcrest jar作为JUnit的传递依赖项排除.这是在Gradle中的方法:instrumentTestCompile('junit:junit:4.10'){exclude group:'org.hamcrest'}在这里查看Maven等价物:http://maven.apache.org/guides/introduction/介绍到可选-和排除,dependencies.html (2认同)