2014-09-19 2 views
3

Я использую ответ, описанный здесь, чтобы ударяться номер версии моего андроида проекта:Bump номер версии андроида проекта, но не для каждого построить

По сути, у меня есть еще одна задача в моей 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, и он в настоящее время выполняется каждый раз, когда я выполнить задачу градиента, например. когда я запускаю ./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);          
} 
Смежные вопросы