A git pre-commit hook to update AndroidManifest.xml versionCode (updated)

Whenever you redistribute an Android application you need to update the internal android:versionCode in the app’s AndroidManifest.xml to indicate a new version. To keep builds properly identified, I like to increase this number every time I commit to the project’s repository. That way the if one of our developers builds the application and finds a bug we can easily track down exactly which version of the code she’s using.

I was doing this manually but I often forgot and it got tiresome really fast. To alleviate my commit process, I created a git pre-commit hook to do it for me. Here it is:

# Configuration
# -------------
# The Manifest path is relative to THIS script.
# If you copy-and-paste this into your pre-commit hook
# and your AndroidManifest.xml is in the root of your repo
# then the path would be:
# MANIFEST="../../AndroidManifest.xml"

# If this is in the same directory then it's just:
MANIFEST="AndroidManifest.xml"

# Script DO NOT MODIFY (unless you want to)

declare -x SCRIPTPATH="${0}"

FULLPATH=${SCRIPTPATH%/*}/$MANIFEST

if [ -f $FULLPATH ]
then
	LINE=$(grep -o ${FULLPATH} -e 'android:versionCode="[0-9]*"');
	declare -a LINE;
	LINE=(`echo $LINE | tr "\"" " "`);
	VERSION=(${LINE[1]});
	INCREMENTED=$(($VERSION+1))
	sed "s/android:versionCode=\"[0-9]*\"/android:versionCode=\"${INCREMENTED}\"/" $FULLPATH > $FULLPATH.tmp && mv $FULLPATH.tmp $FULLPATH
	echo "Updated android:versionCode to ${INCREMENTED} in ${FULLPATH}";
fi;

To install it, just copy and paste the above script into a file in your project. I called mine precommit-hook and I put it in the root of my project alongside the AndroidManifest.xml file. Next, create a pre-commit file in your .git/hooks folder for your local repo (if you don’t already have one). Then add an absolute reference to the copy-and-paste file you created.

For example I called this file precommit-hook in the top level of my repo so I opened:

/Users/jsambells/development/MyProject/.git/hooks/pre-commit

and added:

# run my special hooks
/Users/jsambells/development/MyProject/precommit-hook

now when I commit I get this:

% git commit -am "My super awesome commit message."
Updated android:versionCode to 1109 in Users/jsambells/development/MyProject/AndroidManifest.xml
[master a97a21e] My super awesome commit message.
1 files changed, 18 insertions(+), 6 deletions(-)

You could extend this script to do lots of other neat stuff, I’ll leave that up to you.

UPDATE

If you were paying attention you may have noticed there’s a slight flaw in my idea here. Since the pre-commit hook actually modifies the source–without committing–the new number doesn’t actually become part of the commit. That’s actually fine since all I really wanted to do was automatically increment the number when commits happen so this would be better as a post-commit hook that increments the number after the commit, preparing the source for the next bit of development. That, or I guess you could commit from your commit but that’s just going to create some messy recursive logic that wouldn’t want to deal with.