【问题标题】:How do I automatically increment the build id and version number of an Android App using Azure Pipelines如何使用 Azure Pipelines 自动增加 Android 应用的构建 ID 和版本号
【发布时间】:2020-06-11 00:25:18
【问题描述】:

这是我的 build.gradle

defaultConfig {
        applicationId "com.xyz.abc.na"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 36
        versionName "5.0.2"
        multiDexEnabled true
    }

在我的 YML 中,我有

 - task: android-manifest-version@1
  displayName: 'Version Update'
  inputs:
  sourcePath: '$(manifestDirectory)'
  versionCodeOption: 'buildid'
  versionCode: '$(Build.BuildId)'
  versionCodeOffset: '1'
  printFile: true

  - task: Gradle@2
    displayName: 'Building Gradle'
    inputs:
      workingDirectory: ''
      gradleWrapperFile: 'gradlew'
      gradleOptions: '-Xmx3072m'
      publishJUnitResults: false
      testResultsFiles: '**/TEST-*.xml'
      tasks: $(gradleTask)
      codeCoverageToolOption: 'None'
      javaHomeOption: 'JDKVersion'
      jdkVersionOption: 'default'
      jdkArchitectureOption: 'x64'
      checkStyleRunAnalysis: false
      findBugsRunAnalysis: false

除非我在 build.gradle 中手动更改 versionCodeversionName,否则这些值永远不会自动更新。

如何获取最新值,加 1 并更新 build.gradle,然后根据新版本代码和版本名称生成构建?

以下是对我不起作用的现有参考。

Ref1

Ref2

Ref3

【问题讨论】:

    标签: android azure-devops continuous-integration azure-pipelines continuous-delivery


    【解决方案1】:

    要在本地进行,您可以按照以下步骤操作:

    1. gradle.properties 添加这两个属性
    appVersionCode=1
    appVersionName="1.0"
    
    1. build.gradle应用

    替换

    versionCode 36
    versionName "5.0.2"
    

    versionCode appVersionCode.toInteger()
    versionName appVersionName.toString()
    
    1. 在您的 YAML 文件中添加 Gradle options 并通过变量传递 $(MyAppBuildNumber)$(MyAppVersionNumber)
    - task: Gradle@2
          inputs:
            ...
            options: '-PappVersionCode=$(MyAppBuildNumber) -PappVersionName=$(MyAppVersionNumber)'
            ...
    

    【讨论】:

    • 我遵循了相同的步骤。不过,应用程序版本是 1.0。不知道我错过了什么。
    • 您是否已将$(MyAppBuildNumber)$(MyAppVersionNumber) 替换为您的号码?
    • 是的,我确实更改了这些值。出于测试目的,我刚刚将值硬编码为 -PappVersionCode=8 -PappVersionName=8.0.1
    • 我已经编辑了我的答案,只需确保在第 2 步中将其替换为 versionCode appVersionCode.toInteger() versionName appVersionName.toString()
    • 例如你可以使用 Build.BuildId 作为 versionCode: options: '-PappVersionCode=$(Build.BuildId)'
    【解决方案2】:

    对于那些无法安装扩展的人,这是我的 PowerShell 解决方案:

    $BasePath = "$(Build.SourcesDirectory)\"
    $BuildGradlePath = "${BasePath}[YourAppFolderStructure]\app\build.gradle"
    $BuildGradleUpdatedPath = "${BasePath}[YourAppFolderStructure]\app\build.gradle.updated"
    Get-Content $BuildGradlePath | ForEach-Object {
        if($_ -match "versionCode \d"){
            $versionCode = $_ -replace "versionCode", "" -as [int]
            $newVersionCode = $versionCode + 1
            
            Write-Host "Found versionCode ${versionCode}. Updated to ${newVersionCode}"
            
            $_ = "        versionCode " + $newVersionCode   
        }
        elseif($_ -match "versionName"){
            Write-Host "Updating versionName to $(Build.BuildNumber)"
            $_ = "        versionName '$(Build.BuildNumber)'"
        }
        $_
    } | Set-Content $BuildGradleUpdatedPath;
    Get-Content $BuildGradleUpdatedPath | Set-Content $BuildGradlePath
    

    【讨论】:

    • 非常感谢。我也能够使用您的方法实现结果。
    • 我不确定版本代码的 +1 增量将如何与后续版本一起使用,因为未提交新版本号
    【解决方案3】:

    您可以查看扩展程序Mobile Versioning,它提供了更新Android版本Gradle的任务:

    - task: UpdateAndroidVersionGradle@1
      inputs:
        buildGradlePath: 'your_project/app/build.gradle'
        versionName: '$(MAJOR).$(MINOR).$(PATCH)' # Optional. Default is: $(MAJOR).$(MINOR).$(PATCH)
        versionCode: '$(NUMBER_OF_COMMITS)' # Optional. Default is: $(NUMBER_OF_COMMITS)
    

    详细文档可以查看以下链接:

    https://damienaicheh.github.io/azure/devops/2019/12/19/manage-your-application-version-automatically-using-git-and-azure-devops-en

    【讨论】:

    • 谢谢你。不幸的是,我不能使用它,因为我应该在没有任何库支持的情况下本机使用它
    • 这是一个DevOps扩展,安装在DevOps端。
    • 知道了。谢谢。 :)
    猜你喜欢
    • 2023-03-14
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多