2014-09-19 23 views
3

我使用這裏描述的答案,撞我的Android項目的版本號:Android項目的凹凸版本號,但並不適用於所有建設

從本質上講,我有什麼是我build.gradle文件中的另一個任務是讀(並隨後寫入),包含版本名稱和版本代碼屬性文件:

// Task designed to bump version numbers. This should be the first task run  
// after a new release branch is created.          
task bumpVersion() {                
    description = 'Bumps the version number of the current Android release. Should be used as a standalone task, and should only be the first task called after creating a release branch.' 
    group = 'Build Setup'               

    Properties props = new Properties();           
    File propsFile = new File('gradle.properties');        
    props.load(propsFile.newDataInputStream());         
    def currentVersionCode = props.getProperty("VERSION_CODE") as int;    
    def currentVersionName = props.getProperty("VERSION_NAME") as String;   
    def intPortionsOfVersionName = currentVersionName.tokenize('.').toArray();  
    def leastSignificantPortion = intPortionsOfVersionName[intPortionsOfVersionName.length - 1] as int; 

    def newVersionCode = currentVersionCode + 1;         
    def newVersionName = "";              
    if (!project.hasProperty('newVersion')) {          
    leastSignificantPortion = leastSignificantPortion + 1;      
    intPortionsOfVersionName[intPortionsOfVersionName.length - 1] = leastSignificantPortion; 
    newVersionName = intPortionsOfVersionName.collect{ it }.join(".");   
    } else {                  
    newVersionName = project.getProperty('newVersion');       
    }                    

    props.setProperty("VERSION_NAME", newVersionName as String);     
    props.setProperty("VERSION_CODE", newVersionCode as String);     

    props.store(propsFile.newWriter(), null);          
} 

這工作得很好,但我遇到的問題是,我希望它運行只有當我專門執行./gradlew bumpVersion,它每次都在運行執行一個gradle任務,例如當我運行./gradlew assembleDebug

當我運行另一個(不相關的)任務時,如何限制此任務不能運行?

回答

2

所以,我發現我做錯了什麼。我需要做的任務實際使用doLast()語法(注意<<):

// Task designed to bump version numbers. This should be the first task run  
// after a new release branch is created.          
task bumpVersion() << {                
    description = 'Bumps the version number of the current Android release. Should be used as a standalone task, and should only be the first task called after creating a release branch.' 
    group = 'Build Setup'               

    Properties props = new Properties();           
    File propsFile = new File('gradle.properties');        
    props.load(propsFile.newDataInputStream());         
    def currentVersionCode = props.getProperty("VERSION_CODE") as int;    
    def currentVersionName = props.getProperty("VERSION_NAME") as String;   
    def intPortionsOfVersionName = currentVersionName.tokenize('.').toArray();  
    def leastSignificantPortion = intPortionsOfVersionName[intPortionsOfVersionName.length - 1] as int; 

    def newVersionCode = currentVersionCode + 1;         
    def newVersionName = "";              
    if (!project.hasProperty('newVersion')) {          
    leastSignificantPortion = leastSignificantPortion + 1;      
    intPortionsOfVersionName[intPortionsOfVersionName.length - 1] = leastSignificantPortion; 
    newVersionName = intPortionsOfVersionName.collect{ it }.join(".");   
    } else {                  
    newVersionName = project.getProperty('newVersion');       
    }                    

    props.setProperty("VERSION_NAME", newVersionName as String);     
    props.setProperty("VERSION_CODE", newVersionCode as String);     

    props.store(propsFile.newWriter(), null);          
} 

不幸的是,這也意味着,當我運行gradlew tasks描述和組不被識別,所以要緩解這個,我用以下作爲我的最終任務定義:

// Task designed to bump version numbers. This should be the first task run  
// after a new release branch is created.          
task bumpVersion(description: 'Bumps the version number of the current Android release. Should be used as a standalone task, and should only be the first task called after creating a release branch.', group: 'Build Setup') << {            

    Properties props = new Properties();           
    File propsFile = new File('gradle.properties');        
    props.load(propsFile.newDataInputStream());         
    def currentVersionCode = props.getProperty("VERSION_CODE") as int;    
    def currentVersionName = props.getProperty("VERSION_NAME") as String;   
    def intPortionsOfVersionName = currentVersionName.tokenize('.').toArray();  
    def leastSignificantPortion = intPortionsOfVersionName[intPortionsOfVersionName.length - 1] as int; 

    def newVersionCode = currentVersionCode + 1;         
    def newVersionName = "";              
    if (!project.hasProperty('newVersion')) {          
    leastSignificantPortion = leastSignificantPortion + 1;      
    intPortionsOfVersionName[intPortionsOfVersionName.length - 1] = leastSignificantPortion; 
    newVersionName = intPortionsOfVersionName.collect{ it }.join(".");   
    } else {                  
    newVersionName = project.getProperty('newVersion');       
    }                    

    props.setProperty("VERSION_NAME", newVersionName as String);     
    props.setProperty("VERSION_CODE", newVersionCode as String);     

    props.store(propsFile.newWriter(), null);          
} 
相關問題