Android Studio - productFlavor combinations to set version

1k Views Asked by At

In my build.gradle, I'm defining 2 different flavor dimensions:

//list flavorDimensions in override priority order
flavorDimensions 'flavor', 'platform'

//assign each productFlavor a corresponding flavorDimension
productFlavors {
    flavor1 {
        dimension 'flavor'
        applicationId "com.flavor1"
    }
    flavor2 {
        dimension 'flavor'
        applicationId "com.flavor2"
    }
    mobile {
        dimension 'platform'
        applicationIdSuffix '.mobile'
        minSdkVersion 22
    }
    tv {
        dimension 'platform'
        applicationIdSuffix '.tv'
        minSdkVersion 21
    }
}

I would like to be able to set versionCode and versionName based on the combinations, so each of the following could have it's own version info:

  • flavor1Mobile
    • versionName = 1.0.0
    • versionCode = 3
  • flavor1Tv
    • versionName = 1.0.2
    • versionCode = 5
  • flavor2Mobile
    • versionName = 3.0.0
    • versionCode = 10
  • flavor2Tv
    • versionName = 2.0.0
    • versionCode = 4
2

There are 2 best solutions below

0
On BEST ANSWER

Was able to build a solution by combining the solutions from :

  • https://stackoverflow.com/a/30880966
  • https://developer.android.com/studio/build/gradle-tips#configure-dynamic-version-codes

    project.ext{
    
        flavor1Mobile_versionName = "1.0.0"
        flavor1Mobile_versionCode = 3
    
        flavor1Tv_versionName = "1.0.2"
        flavor1Tv_versionCode = 5
    
    
        flavor2Mobile_versionName = "3.0.0"
        flavor2Mobile_versionCode = 10
    
        flavor2Tv_versionName = "2.0.0"
        flavor2Tv_versionCode = 4
    
    }
    
    
    project.android.applicationVariants.all {variant ->
        def versionName = project.ext."${variant.flavorName}_versionName"
        def versionCode = project.ext."${variant.flavorName}_versionCode"
        variant.outputs.each { output ->
            output.versionNameOverride = versionName
            output.versionCodeOverride = versionCode
        }
    }
    
0
On

I've got the same problem and was able to solve it a little bit different, but depending on the circumstances of future visitors of this page, it may help.

In your global gradle.properties file you can define your different versions:

flavor1_versionName=1.0.0
flavor2_versionName=1.2.3
mobile_versionName=1.0.1
tv_versionName=1.0.2

Then you can just override the versionName parameter in your build.gradle(:app) file where you add your productFlavors:

productFlavors {
    flavor1 {
        dimension 'flavor'
        applicationId "com.flavor1"
        versionName = flavor1_versionName;
    }
    flavor2 {
        dimension 'flavor'
        applicationId "com.flavor2"
        versionName = flavor2_versionName;
    }
    mobile {
        dimension 'platform'
        applicationIdSuffix '.mobile'
        minSdkVersion 22
        versionName = mobile_versionName;
    }
    tv {
        dimension 'platform'
        applicationIdSuffix '.tv'
        minSdkVersion 21
        versionName = tv_versionName;
    }
}

In your defaultConfig block in the same build.gradle file, you can say:

versionName version
project.version version