8

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

Community
  • 1
  • 1
Mohamed Heiba
  • 1,813
  • 4
  • 38
  • 67
  • 2
    Hmm, well, I guess it's convenient to auto-increment versionCode (and possibly also name) when doing an Export, but I also think you should ask yourself how often you do this and if it's worth the effort required to dig into the inner workings of Eclipse to get it to do what you want. Besides, I find building for the commandline a whole lot more flexible, especially when it comes to building release versions of my apps. It's super easy to increment the version using Ant and since the whole command-line build is based on Ant it can become fully automated. – britzl May 16 '13 at 16:59
  • 1
    Just a note: with the announcement of Android Studio --- http://android-developers.blogspot.kr/2013/05/android-studio-ide-built-for-android.html --- in IO13, Android project build in Eclipse will have minimum to no support and so you may consider dropping this at all. – ozbek May 17 '13 at 16:09

2 Answers2

8

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?

Community
  • 1
  • 1
1''
  • 26,823
  • 32
  • 143
  • 200
  • 1
    LOL thanks a lot for your answer and that xkcd, according to it, I can spend 1 day messing around with that export button so as to have actually saved myself time :D – Mohamed Heiba May 13 '13 at 14:24
  • 1
    I found this to be very useful http://www.vogella.com/articles/EclipseExtensionPoint/article.html – Mohamed Heiba May 13 '13 at 14:37
  • 4
    Whilst the xkcd comic is good, it fails to take into account the annoyance factor. If you've got 20 little tasks that need doing for a release you can be sure you'll end up forgetting one. Its better to automate for your own sanity. – Phil Hannent May 19 '13 at 09:28
  • 2
    And it also doesn't take into account what happens if you forget to do this task (anyone forget http -> https for production?) – Paul de Lange Jul 26 '13 at 13:00
4

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="@versionCode@" . 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. :(

Delyan
  • 8,881
  • 4
  • 37
  • 42