Bump版本号的android项目,但不是每个构建

时间:2014-09-19 21:12:02

标签: android gradle version task

我正在使用此处描述的答案来破坏我的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

如何在运行其他(不相关的)任务时将此任务限制为不运行?

1 个答案:

答案 0 :(得分:3)

所以,我发现了我做错了什么。我需要让任务实际使用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);                                     
}