Android Core

Update the AndroidManifest Version Number automatically

Recently I needed to automate the update of the versionName element in the AndroidManifest.xml file. This needed to be timed stamped and updated with the correct version number when a build was run. We use maven as part of the build, a few plugins come in handy. We want to use POM’s version number so that tools like the maven-release-plugin or maven versions plugin can be used. When these are run the new version number should reflect in the built APKs.

The following magic added to the build portion of the pom handles this.
 
 
 

<plugins>
   <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <version>1.8</version>
      <executions>
         <execution>
            <id>parse-version</id>
               <goals>
                  <goal>parse-version</goal>
               </goals>
               <phase>validate</phase>
          </execution>
       </executions>
    </plugin>
    <plugin>
       <groupId>com.google.code.maven-replacer-plugin</groupId>
       <artifactId>maven-replacer-plugin</artifactId>
       <version>1.4.1</version>
       <executions>
         <execution>
            <id>replace-version</id>
            <phase>validate</phase>
            <goals>
               <goal>replace</goal>
            </goals>
         </execution>
       </executions>
       <configuration>
          <file>AndroidManifest.xml</file>
          <replacements>
              <replacement>
                 <token>0.0.0</token>                                   <value>${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}.${maven.build.timestamp}</value>
              </replacement>
          </replacements>
          <quiet>false</quiet>
       </configuration>
   </plugin>
</plugins>

You can add this as part of a profile, and only enable it when you are building say on Jenkins or when you are doing a release. Your AndroidManifest.xml should have the versionName value set to 0.0.0, and when the build runs with the profile enabled it’ll replace this with the pom’s version and also add a time stamp.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button