Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add product flavor to version name in Gradle

I have a Android Gradle project that has multiple flavors like so.

android {
    ...
    productFlavors {
        apple {
            applicationId "com.myapp.apple"
        }

        orange {
            applicationId "com.myapp.orange"
        }

        banana {
            applicationId "com.myapp.banana"
        }
    }
    ...
}

In my build.gradle I also have the following piece of code that sets the versionName to either 0.8-dev if it's an local build or 0.8-123 if it's a Jenkin's build.

android {
    ...
    defaultConfig {
        ...
        // Get the build number from Jenkins, otherwise use 'dev'
        ext.buildNumber = System.getenv("BUILD_NUMBER") ?: "dev"
        versionName "0.8-$buildNumber"
        ...
    }
}

Since I now have different flavors of the same project, I'd like to prefix the versionName with the flavor so it looks something like this apple-0.8-dev or orange-0.8-dev, depending on which flavor is built.

How do I go about doing this?

I've tried using BuildConfig.FLAVOR but I can't access that in build.gradle.

like image 654
FilmiHero Avatar asked Dec 15 '22 15:12

FilmiHero


1 Answers

Re-define the versionName in your flavors:

def version="0.8"

android {
    defaultConfig {
      versionName version
    }
    ...
    productFlavors {
        apple {
            applicationId "com.myapp.apple"
            versionName "apple-$version"
        }

        orange {
            applicationId "com.myapp.orange"
            versionName "orange-$version"
        }

        banana {
            applicationId "com.myapp.banana"
            versionName "banana-$version"
        }
    }
    ...
}
like image 176
CommonsWare Avatar answered Dec 17 '22 05:12

CommonsWare