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

2

8

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

Datura answered 29/5, 2012 at 16:27 Comment(0)
T
20

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>
Tedium answered 29/5, 2012 at 23:23 Comment(5)
doesn't seem to kick off the filtering before it gets to the stage when it needs the filtered manifest.. still debuggingDatura
needed this: <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <executions> <execution> <phase>initialize</phase> <goals> <goal>resources</goal> </goals> </execution> </executions> </plugin>Datura
I cant update version.code it gives an error. is it because of only it can be able to set integer? how do you solve it?Astraddle
@Tedium Say if I want the properties values to change based on the profile I choose while building, how do I do that. I put the properties tag in profiles and it did not work. If you want another questions to be opened for the same, I can do thatConvey
@Tedium raised a new question #22041259Convey
D
11

I recommend to use manifest-update goal of maven-android-plugin to accomplish this:

<plugins>
    <plugin>
        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
        <artifactId>android-maven-plugin</artifactId>
        <version>3.3.2</version>
        <!-- This is for maven update properties in AndroidManifest file. Working outside eclipse. -->
        <executions>
            <execution>
                <id>update-manifest</id>
                <phase>process-resources</phase>
                <goals>
                    <goal>manifest-update</goal>
                </goals>
                <configuration>
                    <manifest>
                        <versionCode>${project.codeVersion}</versionCode>
                        <versionName>${project.version}</versionName>
                    </manifest>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

You need to define also project.codeVersion:

<properties>
    <project.codeVersion>1</project.codeVersion>
</properties>

And maven will update your AndroidManifest.xml:

[INFO] --- android-maven-plugin:3.3.2:manifest-update (update-manifest) @ myapp ---
[INFO] Attempting to update manifest d:\dsv\project\MyApp\AndroidManifest.xml
[INFO] Setting android:versionName to 0.0.4
[INFO] Setting android:versionCode to 1
[INFO] Made changes to manifest file, updating d:\...\MyApp\AndroidManifest.xml

More information about android:manifest-update goal here, you can find some useful parameters there.

Important note: This solution only works from command line, plugin execution is not yet supported by m2e-android (see this issue).

Declination answered 11/9, 2012 at 9:13 Comment(2)
This is realy work for me even though this way modifies the original XML filePainty
It not necessarily wrong. In some cases modifying the original XML file is exactly what one wants to happen.Friel

© 2022 - 2024 — McMap. All rights reserved.