Skip to content

Instantly share code, notes, and snippets.

@douo
Created September 2, 2013 08:04
Show Gist options
  • Save douo/6410339 to your computer and use it in GitHub Desktop.
Save douo/6410339 to your computer and use it in GitHub Desktop.
auto-increment-version-code-in-android-app
#!/bin/python
#auto-increment-version-code-in-android-app
import re
def readManifest():
f = file('AndroidManifest.xml')
entry = f.read();
f.close();
return entry;
def writeMainifest(entry):
f = file('AndroidManifest.xml','w');
f.write(entry);
f.close;
def increase():
entry = readManifest();
code_pattern = '(?<=android:versionCode=")(?P<code>\d*)(?=")'
m = re.search(code_pattern,entry)
if m and m.groups() > 0:
code = int( m.group())
code += 1
entry = entry[0:m.start(1)] + str(code) + entry[m.end(1):len(entry)]
## if the versionName have pattern like 'A.B.C.D' then increase D
name_pattern = '(?<=android:versionName=")(?P<code>[\d.]*)(?=")'
m = re.search(name_pattern,entry)
if m and m.groups() > 0:
name = m.group()
lidx = name.rfind('.')
if lidx != -1 :
name = name[:lidx+1]+str(int(name[lidx+1:])+1)
entry = entry[0:m.start(1)] + name + entry[m.end(1):len(entry)]
writeMainifest(entry);
if __name__ == "__main__":
increase()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment