Auto Version numbering your Android App using Git and Eclipse

后端 未结 4 799
梦如初夏
梦如初夏 2020-12-07 23:58

I believe that computers are the best things for doing repetitive tasks. I certainly am not, I either forget, or (mostly) don\'t do things in a consistent way - which isn\'

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 00:24

    Idea (using ant and git executable)

    ok, here's a novel way of doing it:

    • for calculating version.code:

      git rev-list master --first-parent --count

      this follows the versioning guideline. As it effectively finds the number of commits from the initial commit (inclusive) which is always incrementing from the previous version.

    • for calculating the version.name:

      git describe --tags --dirty --abbrev=7

    Implementation:

    build.xml usually imports a custom ant script called custom_rules.xml

    so making the content of the script as:

    
    
    
    
        
        
        
        
        
        
        
            
                
                
            
        
    
    
    
        
            
                
                
                
            
        
        
            
                
                
                
            
        
        ${versioning.code}, ${versioning.name}
        
        
    
    
    
        
        
    
    

    would just do.

    In a nut shell, it just replaces the android.versionCode and android.versionName with the current version code and name, stored in git.

    Caveats

    • initial version code and name is set to 0 upon the completion of build. If you require it, replace the zero in the -post-build target, or (though I highly doubt you would require it) you could make it configurable and place it in some property (file or embedded; your choice)
    • if the build is failed, or aborted, the version will remain as it is. (though i highly doubt it is of any concern, just revert the file!)

    Enjoy.

    Refs

    • Best way to integrate Git with Ant?
    • macrodef for git improvised from Ant Tasks for Git | tlrobinson.net blog

    Important Edit

    1. prevent using HEAD to calculate the build number; causes version downgrade issue when doing a build during development, and later when installing the stable version (or when doing a beta to main release transition). using the master (or the branch which is used for production builds) ref instead.

    PS: relevant for AS users: Automatically versioning Android project from git describe with Android Studio/Gradle

提交回复
热议问题