2012-09-08 55 views
11

首先,至少有2个帖子存在相同的问题,但这些解决方案不再工作,至少在我的安装中不起作用。java.lang.IllegalArgumentException:已添加:Lorg/hamcrest/BaseDescription;转换为Dalvik格式失败,出现错误1

我使用的Eclipse和Android M2E,并试图通过选择Run AS-> Android应用程序来运行该应用程序为“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

这是问题描述为here in Tools R14 section。首先,这是无法修复的,因为我在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。

回答

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作为依赖项引入到我的项目中!将JUnit从4.10更改为4.8。1在ActionBarSherlock Library pom.xml中修复了它。非常感谢! –

+2

如果您希望继续使用4.10,那么您可以明确排除Hamcrest jar作为JUnit的传递依赖项。这是如何做到这一点的摇篮: instrumentTestCompile( '的JUnit:JUnit的:4.10'){ 排除组: 'org.hamcrest' } 看到这里Maven的等效: HTTP://行家。 apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html – Brendan

1

不幸的是Eclipse不理解maven测试范围。它使用JUnit 4.10将粗糙的类拖入构建中。使用JUnit 4.8.1或者,如果你确实需要使用JUnit的4.10+版本,那么你可以在eclipse中使用没有junit dep的maven配置文件。

  1. 在默认的配置文件中的JUnit DEP和创建一个空的“无JUnit的个人资料(见下面的例子POM)
  2. Eclipse项目的属性>的Maven>活动的Maven简介:无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

第3点是一个非常糟糕的主意。这是一个Maven项目,所以Maven必须管理项目本身。如果你开始手动添加库,没有人可以最终重现你所做的。 – Bevor

13

我尝试了上述解决方案,但仍然出现错误。只有经过一些更多的尝试和错误,我发现hamcrest类也包含在另一个罐子里:mockito(我还没有意识到mockito不能用于我的仪器测试)

所以我解决了我的问题,的Mockito-all.jar在从我的依赖和排除hamcrest从JUnit的像这样的传递依赖:

<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建设者会抗议老班。

+0

感谢兄弟 - 这是为我做的! :) – martyglaubitz

+0

唯一符合我需求的解决方案。我访问过Google上的每一个链接,但没有任何链接谢谢。 – tomrozb

+1

根据https://code.google.com/p/mockito/wiki/DeclaringMockitoDependency#mockito-core_VS_mockito-all我只是将依赖关系从mockito-all切换到mockito-core,这有效地完成了删除hamcrest-core从与mockito捆绑在一起,但不必添加排除。 –

相关问题