Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 894
Nagaraj Alagusundaram Avatar asked Jun 11 '20 00:06

Nagaraj Alagusundaram


2 Answers

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)'
        ...
like image 181
Yahia Avatar answered Sep 24 '22 03:09

Yahia


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
like image 20
Arthur Garcia Avatar answered Sep 25 '22 03:09

Arthur Garcia