how do I change my AndroidManifest as it's being packaged by maven

danb picture danb · May 29, 2012 · Viewed 10.4k times · Source

I have the maven goal update-manifest working in my project and updating the attributes I want changed based on the profile I'm packaging with; Unfortunately it changes the manifest that is in source control. I would like it to change the manifest that is packaged, but leave the master version alone. I don't see a version of AndroidManifest under target/ that I can point it at. I am using the android-maven plugin version 3.2

Answer

yorkw picture yorkw · May 30, 2012

If you use configurations like manifestVersionCode or manifestVersionName to update manifest, this is how it is designed and supposed to work, it writes the changes to the original AndroidManifest.xml.

It seems what you really need is filter manifest rather than update manifest. Resource filtering is another use scenario supported by android-maven-plugin and frequently used.

Sample AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.myapp"
    android:versionCode="${app.version.code}"
    android:versionName="${app.version.name}">
... ...

Sample pom.xml:

<!-- value to be substituted in manifest -->
<properties>
  <app.version.code>1</app.version.code>
  <app.version.name>1.0.0-SNAPSHOT</app.version.name>
</properties>

... ...

<build>
  <resources>
    <!-- filter manifest and put filtered file in target/filtered-manifest/ -->
    <resource>
      <directory>${project.basedir}</directory>
      <filtering>true</filtering>
      <targetPath>${project.build.directory}/filtered-manifest</targetPath>
      <includes>
        <include>AndroidManifest.xml</include>
      </includes>
    </resource>
  </resources>

  ... ...

  <plugins>
    <plugin>
      <groupId>com.jayway.maven.plugins.android.generation2</groupId>
      <artifactId>android-maven-plugin</artifactId>
      <extensions>true</extensions>
      <configuration>
        <undeployBeforeDeploy>true</undeployBeforeDeploy>
        <!-- tell build process to use filtered manifest -->
        <androidManifestFile>${project.build.directory}/filtered-manifest/AndroidManifest.xml</androidManifestFile>
      </configuration>
    </plugin>

  ... ...

Now android-maven-plugin will use the filtered AndroidManifest.xml and leave the original AndroidManifest.xml unchanged.

Hope this helps.

Update:

you might need this too:

<plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.5</version>
        <executions>
            <execution>
                <phase>initialize</phase>
                <goals>
                    <goal>resources</goal>
                </goals>
            </execution>
        </executions>
    </plugin>