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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


4 + = five



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close