jwir3
jwir3

Reputation: 6209

Bump version number of android project but not for every build

I'm using the answer described here to bump the version number of my android project:

Essentially, what I have is another task in my build.gradle file that reads (and then subsequently writes to) a properties file containing the version name and version code:

// 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);                                     
}   

This works pretty well, but the problem I'm having is that I want it to run only when I specifically execute ./gradlew bumpVersion, and it's currently running every time I execute a gradle task, e.g. when I run ./gradlew assembleDebug

How can I restrict this task to not be run when I run another (unrelated) task?

Upvotes: 4

Views: 1300

Answers (1)

jwir3
jwir3

Reputation: 6209

So, I found out what I was doing wrong. I needed to make the task actually use the doLast() syntax (notice the <<):

// 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);                                     
}  

Unfortunately, this also means that the description and group aren't recognized when I run gradlew tasks, so to alleviate this, I use the following as my final task definition:

// 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);                                     
}  

Upvotes: 3

Related Questions