2012-09-08 209 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捆綁在一起,但不必添加排除。 –

相關問題