Skip to content

Instantly share code, notes, and snippets.

@alexsunday
Created March 29, 2015 12:45
Show Gist options
  • Save alexsunday/cdaad11245da51c8b965 to your computer and use it in GitHub Desktop.
Save alexsunday/cdaad11245da51c8b965 to your computer and use it in GitHub Desktop.
#!/bin/sh
CUR_DIR=`pwd`
if [ ! -f "AndroidManifest.xml" ]; then
echo "AndroidManifest.xml NOT FOUND" >&2
exit 1
fi
EXISTS_PYTHON=`which python`
if [ ! -x "$EXISTS_PYTHON" ]; then
echo "PYTHON NOT FOUND"
exit 1
fi
python <<EOF
import xml.etree.ElementTree as ET
import sys
code_key = '{http://schemas.android.com/apk/res/android}versionCode'
name_key = '{http://schemas.android.com/apk/res/android}versionName'
manifestfile = 'AndroidManifest.xml'
ET.register_namespace("android", "http://schemas.android.com/apk/res/android")
tree = ET.ElementTree()
tree.parse(manifestfile)
root = tree.getroot()
ver_code = root.attrib[code_key]
ver_name = root.attrib[name_key]
if not ver_code:
print 'versionCode NOT FOUND in %s' % manifestfile
sys.exit(1)
try:
ver_code = int(ver_code)
except ValueError as _:
print 'versionCode MUST INTEGER'
sys.exit(1)
root.attrib[code_key] = str(int(ver_code) + 1)
# root.attrib[name_key] = ver_name
out = ET.tostring(tree.getroot(), encoding='utf-8', method='xml')
with open(manifestfile, 'wt') as f1:
f1.write(out)
EOF
git add AndroidManifest.xml
exit 1
@alexsunday
Copy link
Author

自动为 Android项目增加版本号。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment