在Android Maven项目中使用纯Java库

时间:2011-08-06 13:36:26

标签: java android eclipse maven dependencies

我有一个Android Maven项目,它使用的是纯Java库,在过去的几天里我一直在努力工作几个小时,无论我做什么,我总是会遇到以下错误:

[INFO] d:\Development-Android\SDKs\Android\platform-tools\aapt.exe [package, -f, -M, D:\Development-Android\Workspaces\Android-Workspace\S-O-D\AndroidManifest.xml, -S, D:\Development-Android\Workspaces\Android-Workspace\S-O-D\res, --auto-add-overlay, -A, D:\Development-Android\Workspaces\Android-Workspace\S-O-D\target\generated-sources\combined-assets\assets, -I, d:\Development-Android\SDKs\Android\platforms\android-7\android.jar, -F, D:\Development-Android\Workspaces\Android-Workspace\S-O-D\target\s-o-d-1.0.0.ap_]
[ERROR] Cannot add source folder
com.android.sdklib.build.DuplicateFileException: Duplicate files at the same path inside the APK
    at com.android.sdklib.build.ApkBuilder.doAddFile(ApkBuilder.java:716)
    at com.android.sdklib.build.ApkBuilder.processFileForResource(ApkBuilder.java:762)
    at com.android.sdklib.build.ApkBuilder.addSourceFolder(ApkBuilder.java:587)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.jayway.maven.plugins.android.phase09package.ApkBuilder.addSourceFolder(ApkBuilder.java:244)
    at com.jayway.maven.plugins.android.phase09package.ApkMojo.doAPKWithAPKBuilder(ApkMojo.java:341)
    at com.jayway.maven.plugins.android.phase09package.ApkMojo.createApkFile(ApkMojo.java:249)
    at com.jayway.maven.plugins.android.phase09package.ApkMojo.execute(ApkMojo.java:207)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:107)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:534)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)

经过一段时间的尝试,我能够理解所提到的重复项,是输出目录中的重复文件,Android android-classes 文件夹之一,以及maven输出 classes 文件夹。

所以在我继续讨论之前,我试图找出(但我无法找到答案)Android Maven插件是否支持导入纯Java jar?

何,是的,非常重要,请干净安装,不仅要安装!!!

提前致谢,

Adam Zehavi。

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:2)

好吧,我花了很长时间才完全理解这个错误来解决它,并且还从Eclipse快捷方式启用调试。

所以这里没有任何保证是一个通用的解决方案,到目前为止对我很有用,到目前为止我没有遇到任何问题:

我有父母pom:

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.nu.art.software.android</groupId>
    <artifactId>android-maven-parent</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <name>android-maven-parent</name>
    <url>http://maven.apache.org</url>

    <properties>
        <android.sdk>7</android.sdk>
        <android.emulator.name>emulator1</android.emulator.name>
        <android.version>2.1_r1</android.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>${android.version}</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <outputDirectory>./target/android-classes</outputDirectory>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>maven-android-plugin</artifactId>
                <version>3.0.0-alpha-2</version>
                <configuration>
                    <sdk>
                        <platform>${android.sdk}</platform>
                    </sdk>
                    <emulator>
                        <avd>${android.emulator.name}</avd>
                    </emulator>
                    <deleteConflictingFiles>true</deleteConflictingFiles>
                    <undeployBeforeDeploy>true</undeployBeforeDeploy>
                </configuration>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <excludeScope>provided</excludeScope>
                            <skip>true</skip>
                            <includes>**/*.class</includes>
                            <outputDirectory>${project.basedir}/bin</outputDirectory>
                            <overWriteReleases>true</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <id>Make-classes-output</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <inherited>true</inherited>
                        <configuration>
                            <tasks>
                                <echo>Creating '${project.basedir}/target/classes' Directory</echo>
                                <mkdir
                                    dir="${project.basedir}/target/classes" />
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

插件中缺少所有逻辑,以启用Eclipse的启动和调试,并解决了我的重复问题。 (我第一次使用res文件夹,作为源文件夹,搞砸了事情,后来在依赖项中重复了

接下来每个最终项目都有自己的pom,它可以将依赖项复制到Eclipse输出文件夹:

<?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/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>android-maven-parent</artifactId>
        <groupId>com.nu.art.software.android</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.nu.art.software.sod</groupId>
    <artifactId>s-o-d</artifactId>
    <version>1.0.0</version>
    <packaging>apk</packaging>
    <name>S-O-D</name>
    <dependencies>
        <dependency>
            <groupId>com.nu.art.software</groupId>
            <artifactId>rexml</artifactId>
            <version>0.0.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.nu.art.software</groupId>
            <artifactId>nu-art-module-manager</artifactId>
            <version>0.0.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.nu.art.software</groupId>
            <artifactId>nu-art-generator</artifactId>
            <version>0.0.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.nu.art.software</groupId>
            <artifactId>nu-art-reflection</artifactId>
            <version>0.0.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <configuration>
                            <skip>false</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

最后一句话,在安装最终项目时,这个技巧将获取所有依赖项,将它们提取到Eclipse输出文件夹中,该文件夹被定义为'./bin'(默认ADT插件输出文件夹,如果我不是错误)。 之后,Eclipse使用数据构建APK,它在其输出文件夹中有wallah,你有和apk,F11可以用它来启动调试器。

这再次对我有用......我希望这对你也有帮助。

分享你的想法...... 亚当。

答案 2 :(得分:1)

里卡多的链接很好。

添加

<extractDuplicates>true</extractDuplicates>

到Maven插件的配置将确保Jars中的重复文件不会导致崩溃。否则,包含文件的多个Jars就像根包中的类似名称许可文件一样是个问题(例如jackson-2.2.0)