is there a way to auto-increment the version code each time you build an Android application in Eclipse?
According to http://developer.android.com/guide/publishing/versioning.html, you have to manually increment your version code in AndroidManifest.xml
.
I understand, you have to run a script before each build which would, e.g. parse AndroidManifest.xml file, find the version number, increment it and save the file before the build itself starts. However, i couldn't find out how and if Eclipse supports runnings scripts before/after builds.
I have found this article about configuring ant builder, but this is not exactly about Android and I fear this will mess up too much the predefined building steps for Android?
Should be a common problem, how did you solve it?
Well, one can do this manually, but as soon as you forget to do this chore, you get different versions with the same number and the whole versioning makes little sense.
This shell script, suitable for *nix systems, sets the versionCode and the last component of versionName to the current subversion revision. I'm using Netbeans with NBAndroid and I call this script from the target -pre-compile in custom_rules.xml.
Save this script in a file called incVersion in the same directory as AndroidManifest.xml, make it executable:
chmod +x incVersion
Create or edit custom_rules.xml and add this:
So if my current svn revision is 82, I end up with this in AndroidManifest.xml:
When I want to release a new version I'll typically update the first parts of versionName, but even if I forget, the last part of versionName (which is exposed in my About activity) will always tell me what svn revision it was built from. Also, if I have not checked in changes, the revision number will be 82M and versionName will be something like 2.1.82M.
The advantage over simply incrementing the version number each time a build is done is that the number stays under control, and can be directly related to a specific svn revision. Very helpful when investigating bugs in other than the latest release.
If you want to update the AndroidManifest.xml to use a specific version number, perhaps from a build system, then you can use the project I just pushed to GitHub: https://github.com/bluebirdtech/AndroidManifestVersioner
It's a basic .NET command line app, usage:
Thanks to other posters for their code.
I was able to work out my own solution from the information given. In case it is useful for someone here is my bash script for updating the versionCode and versionName attributes when using the GIT VCS on Linux.
My script to edit the AndroidManifest.xml file looks like this:
It parses the template file (AndroidManifest.template.xml) and replaces the strings "__VERSION__" and "__CODE__" with more appropriate values:
By a "clean" build I mean one where all the components are under version control and their is latest commit is tagged. "git describe --dirty" will report a version number based upon the last reachable annotated tag in your latest commit on the current branch. If there are commits since that tag a count of those commits is reported as is the abbreviated object name of your last commit. The "--dirty" option will append "-dirty" to the above information if any files are modified that are under version control have been modified.
So AndroidManifest.xml should not be under version control any more, and you should only edit the AndroidManifest.template.xml file. The start of your AndroidManifest.template.xml file looks something like this:
Hope this is useful to someone
I accomplished this. And here's how I did it for the next guy (using Eclipse):
1) Create an external console executable that is going to write a new version code to the AndroidManifest.xml: (mine is in C#)
aside: any c-sharp compiler can build this app, you don't need Visual Studio or even Windows
*.cs
file (i named mine:AndroidAutoIncrementVersionCode.cs
)*.cs
filec:\Windows\Microsoft.NET\Framework\v2.0.50727\csc AndroidAutoIncrementVersionCode.cs
(see: .NET or Mono for more info)congrats, you just built a C# app without any tools, it should have generated
AndroidAutoIncrementVersionCode.exe
in the same directory automatically**mileage may vary, paths might be different, no purchase required, void where prohibited, i added this because C# is awesome, and people mistakenly think it has MS lock-in, you could just as easily translate this to another language (but i'm not going to do that for you ;). incidentally any version of any .NET compiler will work, i adapted the code for the least common denominator...*
end aside
2) Run the executable during the build process: a) Go to the project properties
b) In the properties, Go to "Builders" -> "New..."
c) Choose "Program"
d) In the "Main" tab select the program location (I also set the working directory to be safe) and give it a name if you wish.
e) In the "Refresh" tab select the "Refresh resources upon completion" and "The selected resource" option - this will refresh the manifest after we write it.
f) In the "Build Options" tab you can turn off "Allocate Console" as you have no input and output and then select only "During manual builds" and "During auto builds" deselect "After a Clean" if it is checked. Then select "Specify a working set of relevant resources" and click the "Specify Resources..." button. In the "Edit Working Set" dialog, locate your "AndroidManifest.xml" file in the dialog and check it, then hit "Finish"
f) Now hit "OK" inside the "Edit Configuration Dialog" and in the properties for your App, select the newly created builder, and keep clicking "Up" until it is at the top of the list, this way the auto increment runs first, and doesn't trigger accidental out-of-sync states or rebuilds. Once the new builder you made is at the top of the list, click "OK" and you're finished.
Receipe:
To automatically have the android:versionCode attribute of manifest element in AndroidManifest.xml set to the current time (from epoch in seconds, obtained from unix shell) everytime you run a build, add this to your -pre-build target in custom_rules.xml Android file.
Confirmation Test:
Obtain the versionCode attribute of the generated apk file, using the following shell command from your Android project directory :
and compare it to the current date returned from the shell command:
date +%s
The difference should equal the period of time in seconds between the two confirmation steps above.Advantages of this approach:
So, I see it like this:
Depending on article that you present, use ant for this tasks (targets?).
But im my case I usually fill this field by value based on Tag's revision when I deploy or distribute application.