android - 颠簸android项目的版本号但不是每个构建

标签 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

当我运行另一个(不相关的)任务时,如何限制此任务不运行?

最佳答案

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

关于android - 颠簸android项目的版本号但不是每个构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25942425/

相关文章:

iOS : how can I check programmatically if the app is up to date?

安卓标签小部件

java - 如何在没有 Gradle 的情况下构建 Android 应用程序

android - AndroidX 迁移后应用程序崩溃(数据绑定(bind)错误)

java - intellij idea gradle 插件无法使用 gradle.properties 中指定的相对路径

android - 无法从开发者帐户中删除应用信息。版本代码问题

c# - 我使用的是哪个版本的 C#

java - 从非主线程读取/proc/self/exe 时,Android 权限被拒绝

android - Android 应用程序的内部存储空间中存储的最大数据量应该是多少?

java - 反序列化期间出现奇怪的异常