简体   繁体   English

在Android Maven项目中使用纯Java库

[英]Using pure Java libs, in a Android Maven project

I have an Android Maven project, which uses pure Java libs, I've struggled with it for hours, the last few days, and no matter what I do, I constantly end up getting the following error: 我有一个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)

After long while of trying, I was able to understand that the duplicates referred to, are the duplicate files in the output directories, one of the Android android-classes folder, and maven output classes folder. 经过长时间的尝试,我能够理解所指的重复项是输出目录,Android android-classes文件夹和maven输出类别文件夹之一中的重复文件。

So before I even go on about it, I've tried to find out (but I was unable to find an answer) does the Android Maven plugin, support the importing of pure Java jars? 因此,在我继续研究之前,我已经尝试找出(但我找不到答案)Android Maven插件是否支持纯Java jar的导入?

Ho, yes, VERY IMPORTANT, do clean install, and not only install!!! ,,是的,非常重要,请执行全新安装,而不仅仅是安装!!!

Thanks in advance, 提前致谢,

Adam Zehavi. 亚当·泽哈维(Adam Zehavi)。

Well, it took me about forever to fully understand this error to work around it, and also to enable debugging from Eclipse shortcut. 好吧,我花了很长时间来完全理解该错误,以解决该错误,并启用Eclipse快捷方式进行调试。

So without any guarantees here is a general solution, which so far works for me very nicely, and I didn't encounter any problems so far: 因此,这里没有任何保证,这是一个通用的解决方案,到目前为止,它对我来说非常好用,到目前为止,我还没有遇到任何问题:

I have a parent pom: 我有一个父母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>

Which does all the logic missing in the plugin to enable the launching and debugging from Eclipse, plus solves my duplicates issues. 该插件执行了插件中缺少的所有逻辑以启用从Eclipse的启动和调试功能,并解决了我的重复问题。 ( The first time I had the res folder, as source folder, which messed things up, and later it was duplicates within the dependencies ) 我第一次把res文件夹作为源文件夹,搞砸了,后来又在依赖项中重复了

Next each final project has its own pom of course, which enables the dependency copy to the Eclipse output folder: 接下来,每个最终项目当然都有其自己的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>

And some final words, upon installing the final project, this trick takes all the dependencies, extract them into Eclipse output folder, which is defined to be './bin' (Default ADT plugin output folder, if I'm not mistaken). 最后的几句话,在安装最终项目时,此技巧将获取所有依赖项,并将它们解压缩到Eclipse输出文件夹中,该文件夹定义为“ ./bin”(如果没有记错,则为默认ADT插件输出文件夹)。 Later, Eclipse build the APK using the data, it has in its output folder, wallah, you have and apk which F11 can launch a debugger with. 后来,Eclipse使用数据构建了APK,它在其输出文件夹wallah中拥有您以及apk,F11可以使用该APK启动调试器。

Again this works for me... and I hope this would help you too. 再次对我有用...我希望这对您也有帮助。

Share your thought... Adam. 分享您的想法...亚当。

The link from Ricardo is good. 来自里卡多的链接很好。

Adding 新增中

<extractDuplicates>true</extractDuplicates>

to the config for the Maven plugin will ensure that duplicate files in the Jars don't cause aapt to crash. Maven插件的配置将确保Jars中的重复文件不会导致Aapt崩溃。 Otherwise multiple Jars that contain files just as similarly named license files in the root package are a problem (eg jackson-2.2.0) 否则,多个Jar包含与根软件包中名称相似的许可证文件相同的文件(例如jackson-2.2.0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM