11

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

我将 m2e 与 Eclipse 和 Android 一起使用,并尝试通过选择 run as-> Android application 将应用程序作为“Android Application”运行,但我总是收到此错误:

意外的顶级异常: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>

我正在使用 Eclipse Juno、ADT 20.0.3、m2e 1.1.0。

4

3 回答 3

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>

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

于 2012-11-07T13:39:54.203 回答
12

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

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

于 2012-09-09T08:21:05.443 回答
1

不幸的是,Eclipse 不了解 Maven 测试范围。它使用 JUnit 4.10 将 hamcrest 类拉入构建中。使用 JUnit 4.8.1,或者,如果你真的需要使用 4.10+ 版本的 JUnit,你可以在 eclipse 中使用没有 junit dep 的 maven 配置文件。

  1. 在默认配置文件中包含 JUnit dep 并创建一个空的“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>
于 2012-10-26T19:49:43.257 回答