Increment versionCode and versionName from gardle.build(app) for any productVariant

641 Views Asked by At

This is the code of gradle.build(app) right now :

    defaultConfig {
      applicationId "com.moveeasy.yourbrokerage"
      multiDexEnabled true
      minSdkVersion 22
      targetSdkVersion 30
      versionCode 25
      versionName "1.19.0"
    }
    productFlavors {
        moveeasy {
            //Updated on 21-7-2021[8 and 1.6]
            applicationId "com.moveeasy.yourbrokerage"
            minSdkVersion 22
            targetSdkVersion 29
            versionCode 17
            versionName "1.15"
            signingConfig signingConfigs.release
        }
}

My fastlane code is as below :

           android_set_version_code(
               version_code: versionCode,
               gradle_file: "app/build.gradle"
           )

           android_set_version_name(
               version_name: versionName,
               gradle_file: "app/build.gradle"
           )

So, now problem is this code is increases the code and name from defaultConfig only. I want to increase it from productVariant/moveeasy. What is the solution?

2

There are 2 best solutions below

2
On

will there another way your can change version and version code via manifest file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="org.test"
    android:versionCode="115"
    android:versionName="11.33">
0
On

Below is the fastlane code to increment versionCode:

lane :incrementVersion do
      path = '../app/build.gradle'
      vc = /versionCode\s+(\d+)/ 
      vn = /versionName\s+\"(\d+.\d+.\d+)\"/ 

      # Reading build.gradle file to get versionName and versionCode
      gradleFile = File.read(path)
      versionCode = gradleFile[vc, 1].to_i
      gradleFile[vc, 1] = (versionCode + 1).to_s

      newGradleFile = File.new(path, 'w')
      newGradleFile.write(gradleFile)
      newGradleFile.close
end