Skip to content

Instantly share code, notes, and snippets.

@cocoahero
Created July 12, 2012 17:05
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cocoahero/3099347 to your computer and use it in GitHub Desktop.
Save cocoahero/3099347 to your computer and use it in GitHub Desktop.
Android Auto-Versioning w/ Git

It is common practice to make the android:versionCode in the AndroidManifest.xml the build number of the application. An easy build number to calculate is the number of git commits in the repo.

Instead of having to edit the manifest file manually and update the android:versionCode attribute with the build number, below is a git pre-commit hook that does it for you.

#!/usr/bin/env bash

MANIFEST="AndroidManifest.xml"

if [ -f $MANIFEST ]
then
    COMMITNUM=`git log --pretty=format:'' | wc -l | tr -d ' '`
    INCREMENTED=$(($COMMITNUM+1))
    sed "s/android:versionCode=\"[0-9]*\"/android:versionCode=\"${INCREMENTED}\"/" $MANIFEST > $MANIFEST.tmp && mv $MANIFEST.tmp $MANIFEST
    git add $MANIFEST
    echo "Updated Build Number To ${INCREMENTED} In ${MANIFEST}!";
fi

Copy and paste the above code into .git/hooks/pre-commit, creating the file if necessary. Also ensure the hook is executable by running the following:

chmod +x .git/hooks/pre-commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment