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:
[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.LifecycleModul开发者_如何转开发eBuilder.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.
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?
Ho, yes, VERY IMPORTANT, do clean install, and not only install!!!
Thanks in advance,
Adam Zehavi.
This might be of help:
https://groups.google.com/forum/#!topic/maven-android-developers/QjbzoaC1Q3k
Well, it took me about forever to fully understand this error to work around it, and also to enable debugging from Eclipse shortcut.
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:
<?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. (The first time I had the res folder, as source folder, which messed things up, and later it was duplicates within the dependencies)
Next each final project has its own pom of course, which enables the dependency copy to the Eclipse output folder:
<?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). 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.
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. Otherwise multiple Jars that contain files just as similarly named license files in the root package are a problem (eg jackson-2.2.0)
精彩评论