Auto increment version code in Android app

后端 未结 15 1012
忘掉有多难
忘掉有多难 2020-12-07 08:22

is there a way to auto-increment the version code each time you build an Android application in Eclipse?

According to http://developer.android.com/guide/publishing/v

相关标签:
15条回答
  • 2020-12-07 08:51

    If you're using gradle then you can specific versionName and versionCode very easy in build.gradle. You can use git commit count as an increasing number to identify the build.

    You can also use this library: https://github.com/rockerhieu/Versionberg.

    0 讨论(0)
  • 2020-12-07 08:52

    Building on Rocky's answer I enhanced that python script a bit to increase also versionCode, works for me on Eclipse (integrated as per ckozl great tutorial) & Mac OSX

    #!/usr/bin/python
    from xml.dom.minidom import parse
    
    dom1 = parse("AndroidManifest.xml")
    oldVersion = dom1.documentElement.getAttribute("android:versionName")
    oldVersionCode = dom1.documentElement.getAttribute("android:versionCode")
    versionNumbers = oldVersion.split('.')
    
    versionNumbers[-1] = unicode(int(versionNumbers[-1]) + 1)
    dom1.documentElement.setAttribute("android:versionName", u'.'.join(versionNumbers))
    dom1.documentElement.setAttribute("android:versionCode", str(int(oldVersionCode)+1))
    with open("AndroidManifest.xml", 'wb') as f:
        for line in dom1.toxml("utf-8"):
            f.write(line)
    

    also don't forget to chmod +x autoincrement.py and make sure you have correct path to python on the first line (depending on your environment) as sulai pointed out

    0 讨论(0)
  • 2020-12-07 08:53

    FWIW, I was able to update the build version value in six lines of python:

    #!/bin/env python
    import os
    from xml.dom.minidom import parse
    dom1 = parse("AndroidManifest.xml")
    dom1.documentElement.setAttribute("android:versionName","%build.number%")
    f = os.open("AndroidManifest.xml", os.O_RDWR)
    os.write( f, dom1.toxml() )
    
    0 讨论(0)
提交回复
热议问题