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.

Related Whitepaper:

Rapid Android Development: Build Rich, Sensor-Based Applications with Processing

Create mobile apps for Android phones and tablets faster and more easily than you ever imagined

Use 'Processing', the free, award-winning, graphics-savvy language and development environment, to work with the touchscreens, hardware sensors, cameras, network transceivers, and other devices and software in the latest Android phones and tablets.

Get it Now!  

Leave a Reply


3 × seven =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
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.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books