Maven and apklib: Duplicated file error

The maven build error occurred when I had two Android projects: the one is apklib and the second one that is the actual application (the apk file) that uses apklib.

The error is saying that apk project contains duplicated R.java and BuildConfig.java files under target directory. The first file comes from apk project, and the second one from apklib.

There are two solutions for this problem:

Solution 1: Update android-maven-plugin

Update android-maven-plugin to the newest version (currently it is 3.8.2 or 3.9.0 release candidate to be checked here) where this bug is fixed.

The problem is that for example the 3.8.2 release is supporting only maven in version 3.1.1. no lower nor higher version will work. This would require downgrading maven on few machines so I decided to use another solution.

Solution 2: Remove conflicting files during apklib build with maven ant plugin

I’ve found solution on the Internet to remove duplicated files from the apklib target with ant plugin during build. The maven-antrun-plugin can do it. I added these lines to apklib pom.xml:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-antrun-plugin</artifactId>
	<executions>
		<execution>
			<phase>process-classes</phase>
			<goals>
				<goal>run</goal>
			</goals>
			<configuration>
				<tasks>
					<delete dir="${project.basedir}/gen" />
				</tasks>
			</configuration>
		</execution>
	</executions>
</plugin>

Then rebuild the apklib project and apk after that. This works!

Eclipse problems with ant plugin

Ant plugin is not recognized by eclipse maven m2e connector and eclipse displays errors. the project still can be built with either maven or eclipse build, but the error is visible on the pom.xml.

Then with plugin management in apklib you can force eclipse to ignore the ant plugin command. It does not affect maven build, just the eclipse compilation.

You can do it by eclipse quick fix or by adding manually plugin management directive to apklib’s pom.xml:

<pluginManagement>
<plugins>
	<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
	<plugin>
		<groupId>org.eclipse.m2e</groupId>
		<artifactId>lifecycle-mapping</artifactId>
		<version>1.0.0</version>
		<configuration>
			<lifecycleMappingMetadata>
				<pluginExecutions>
				<pluginExecution>
					<pluginExecutionFilter>
						<groupId>
							org.apache.maven.plugins
						</groupId>
						<artifactId>
							maven-antrun-plugin
						</artifactId>
						<versionRange>
							[1.3,)
						</versionRange>
						<goals>
							<goal>run</goal>
						</goals>
					</pluginExecutionFilter>
					<action>
						<ignore></ignore>
					</action>
				</pluginExecution>
				</pluginExecutions>
			</lifecycleMappingMetadata>
		</configuration>
	</plugin>
</plugins>
</pluginManagement>

Give Your feedback: