How do I automatically increment the build id and version number of an Android App using Azure Pipelines
Asked Answered
N

3

6

This is my build.gradle

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

In my YML, I have

 - 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

Unless I manually change the versionCode and versionName in my build.gradle the values are never automatically updated.

How do I get the latest value, increment by 1 and update the build.gradle and then generate a build from the new version code and version name?

Below are the existing references that didn't work for me.

Ref1

Ref2

Ref3

Nolde answered 11/6, 2020 at 0:25 Comment(0)
A
1

You may check extension Mobile Versioning, which provide a task to update Android Version 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)

Detailed documentation you can check the following link:

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

Adest answered 12/6, 2020 at 7:17 Comment(3)
Thank you for this. Unfortunately, I cannot use this because, I should do it natively without any library supportNolde
It's a DevOps extension, which installed in DevOps side.Adest
Got it. Thanks. :)Nolde
W
8

to do it natively you can follow those steps:

  1. gradle.properties add those 2 properties
appVersionCode=1
appVersionName="1.0"
  1. build.gradle App

replace

versionCode 36
versionName "5.0.2"

with

versionCode appVersionCode.toInteger()
versionName appVersionName.toString()
  1. in your YAML file add the Gradle options and pass the $(MyAppBuildNumber) and $(MyAppVersionNumber) via variables
- task: Gradle@2
      inputs:
        ...
        options: '-PappVersionCode=$(MyAppBuildNumber) -PappVersionName=$(MyAppVersionNumber)'
        ...
Whelm answered 14/12, 2020 at 20:12 Comment(6)
I followed the same steps. Still, the app version is 1.0. Not sure what I'm missing.Nolde
Have you replaced the $(MyAppBuildNumber) and $(MyAppVersionNumber) with your numbers?Whelm
Yes I did change those values. For testing purposes, I have just hard-coded the value as -PappVersionCode=8 -PappVersionName=8.0.1Nolde
I have edited my answer, just make sure in step 2 to replace it with versionCode appVersionCode.toInteger() versionName appVersionName.toString()Whelm
For example You can use Build.BuildId as versionCode: options: '-PappVersionCode=$(Build.BuildId)'Balthazar
Thank you so much. I was able to achieve the result using your approach too.Nolde
A
2

For those of you who can't install an extension, here's my PowerShell solution:

$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
Addington answered 7/12, 2020 at 14:21 Comment(3)
Thank you so much. I was able to achieve the result using your approach too.Nolde
I am not sure how the +1 increment of the version code will work with subsequent builds, as the new build number is not commitedAsp
i know this is old but stuck on this. maybe there is a way to update a variable in Pipeline to keep last build version code?Inviolate
A
1

You may check extension Mobile Versioning, which provide a task to update Android Version 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)

Detailed documentation you can check the following link:

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

Adest answered 12/6, 2020 at 7:17 Comment(3)
Thank you for this. Unfortunately, I cannot use this because, I should do it natively without any library supportNolde
It's a DevOps extension, which installed in DevOps side.Adest
Got it. Thanks. :)Nolde

© 2022 - 2024 — McMap. All rights reserved.