简体   繁体   中英

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 .

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"
        }
    }
    ...
}

In the newer version of Android Studio, it can be done through dialog from the top menu:

File > Project Structure >

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM