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

Nagaraj Alagusundaram
  • 2,304
  • 2
  • 24
  • 31

3 Answers3

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)'
        ...
Yahia
  • 691
  • 1
  • 7
  • 24
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
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

Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39