Home » Android » How to Auto Increment versionCode when exporting Android apps in Eclipse?

How to Auto Increment versionCode when exporting Android apps in Eclipse?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I want to auto-increment the versionCode and versionName of the manifest file of the android app when I click on Export (to export a new version of the app)

I found the second answer here (Auto increment version code in Android app) extremely useful (comment include .exe programs to auto increment Android versionCode) , however, they made it run on Build, I want it to run when I click on File -> Export , see image please

enter image description here

How to&Answers:

You might consider modifying the File -> Export button to execute a builder to increment the version code (as shown in the post you mentioned) in addition to that button’s usual functionality. The org.eclipse.ui.menus extension point seems like a good starting point. (Disclaimer: I haven’t tried this.)

However, it might be good to keep the following xkcd in mind here:

XKCD: Is It Worth the Time?

Answer:

I have done this with a ton of custom ant steps.

First, you need to extract the build number into a properties file. Let’s call it version.properties

Then, the step to increment the version code is:

<target name="increment-version-code">
    <propertyfile file="version.properties">
        <entry key="versionCode" type="int" default="0" operation="+" value="1" />
    </propertyfile>
</target>

This will read the entry versionCode, increment it and save it back to version.properties.

So far, so good. The last step is to get that versionCode into the AndroidManifest.xml file and unfortunately now it gets messy.

The way I solved it was to regenerate the manifest from a template on every build using the filtering functionality in ant.

<target name="generate-manifest">
    <filter filtersfile="version.properties"/>
    <copy todir="${source.dir}" overwrite="true" verbose="true" filtering="true">
        <fileset dir="${source.dir}" casesensitive="false">   
            <include name="**/*.base"/>
        </fileset>
        <mapper type="glob" from="*.base" to="*" />
    </copy>
</target>

Then, all you have left to do is move your AndroidManifest.xml to AndroidManifest.xml.base and replace your versionCode attribute to read android:versionCode="@[email protected]" . You can then run the increment-version-code target to bump the versionCode in version.properties and the generate-manifest target to convert the .base template into the final manifest.

These can then be easily added as build steps to your Eclipse project or as dependencies to exported ant builds. Unfortunately, there’s no easy hook for the Export functionality (though, there is for “After Clean”, which I find good enough).

Of course, with Gradle on the horizon, all of this would have to be rewritten soon. :(