Skip to content

Instantly share code, notes, and snippets.

@mridang
Last active August 29, 2015 14:04
Show Gist options
  • Save mridang/cfb4cc3ab07c8d60942f to your computer and use it in GitHub Desktop.
Save mridang/cfb4cc3ab07c8d60942f to your computer and use it in GitHub Desktop.
Automatically increment version-number in the Android Manifest XML file.
#!/usr/bin/env python
"""
This script is used to compare the version-number and version-code in the the
manifest XML file of the Android project, with the number of the current tag of
the git branch.
The idea is that when the version-code in the XML file is 0.3, the tag of the
current branch is also at 0.3.
In the event the tag is one version behind the version-code, the script can
automatically increment the version-code and the version number.
The version-code is a decimal beginning from 0.0 with each release adding a 0.1
to the version-code. The version number, one the other hand is an integer that
is the version-code multiplied by 10. So if the version-code is 0.3, the version-
number should be 3, and the tag of the current branch should be 0.3.
"""
import optparse
import os
import sys
from xml.dom import minidom
import subprocess
import re
def main():
parser = optparse.OptionParser()
parser.add_option("-u", "--upgrade",
action="store_true", dest="upgrade", default=False,
help="increment the version number of app")
options, arguments = parser.parse_args()
if not arguments:
if os.path.exists(os.path.join(os.getcwd(), 'AndroidManifest.xml')):
xmlfile = os.path.join(os.getcwd(), 'AndroidManifest.xml')
manifest = minidom.parse(xmlfile)
element = manifest.getElementsByTagName('manifest')[0]
counter = element.attributes['android:versionCode'].value.strip()
version = element.attributes['android:versionName'].value.strip()
current = os.popen('git describe --abbrev=0 --tags 2>&1').read().strip()
if current == 'fatal: No names found, cannot describe anything.':
current = '0.0'
if float(version) * 10 == int(counter):
if not float(current) > float(version):
if float(version) - float(current) == 0.0:
print "Upgrading the version number"
if options.upgrade:
with open(xmlfile, "rb") as manifest:
contents = manifest.read()
version = str(float(version) + 0.1)
counter = str(int(counter) + 1)
contents = re.sub(r"(versionCode\s*=\s*\").*?\"", r"\g<1>" + counter +"\"", contents)
contents = re.sub(r"(versionName\s*=\s*\").*?\"", r"\g<1>" + version +"\"", contents)
with open(xmlfile, "wb") as manifest:
manifest.write(contents)
else:
print "Manifest is at %s and is more than 0.1 ahead of the repository %s" % (version, current)
sys.exit(5)
else:
print "Version mismatch between repository %s and manifest %s" % (current, version)
sys.exit(4)
else:
print "Version mismatch between version %s and counter %s" % (version, counter)
sys.exit(3)
else:
print 'Not a valid Android project since AndroidManifest.xml is missing.'
sys.exit(2)
else:
parser.print_help()
sys.exit(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment