Reputation: 3471
Is there any way to set different versionNameSuffix
for various product flavors in a way it is possible to do this for build types?
The following code works for me:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
verstionNameSuffix "-prod"
}
}
But when I want to set versionNameSuffix
for product flavors like this:
productFlavors {
production {
versionNameSuffix "-prod"
}
development {
versionNameSuffix "-dev"
}
}
I get this error:
Error:(48, 0) Could not find method versionNameSuffix() for arguments [-dev] on ProductFlavor_Decorated{name=development, dimension=null, minSdkVersion=null, targetSdkVersion=null, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptNdkModeEnabled=null, versionCode=null, versionName=null, applicationId=null, testApplicationId=null, testInstrumentationRunner=null, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfig=null, mBuildConfigFields={BASE_URL=com.android.builder.internal.ClassFieldImpl@1630a8db}, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[], mManifestPlaceholders={}} of type com.android.build.gradle.internal.dsl.ProductFlavor.
Is there any other way to set the versionNameSuffix
for the product flavor?
Upvotes: 1
Views: 2760
Reputation: 7628
productFlavors now support versionNameSuffix
From the android developer docs
https://developer.android.com/studio/build/build-variants#flavor-dimensions
android {
...
buildTypes {
debug {...}
release {...}
}
// Specifies the flavor dimensions you want to use. The order in which you
// list each dimension determines its priority, from highest to lowest,
// when Gradle merges variant sources and configurations. You must assign
// each product flavor you configure to one of the flavor dimensions.
flavorDimensions "api", "mode"
productFlavors {
demo {
// Assigns this product flavor to the "mode" flavor dimension.
dimension "mode"
...
}
full {
dimension "mode"
...
}
// Configurations in the "api" product flavors override those in "mode"
// flavors and the defaultConfig block. Gradle determines the priority
// between flavor dimensions based on the order in which they appear next
// to the flavorDimensions property above--the first dimension has a higher
// priority than the second, and so on.
minApi24 {
dimension "api"
minSdkVersion 24
// To ensure the target device receives the version of the app with
// the highest compatible API level, assign version codes in increasing
// value with API level. To learn more about assigning version codes to
// support app updates and uploading to Google Play, read Multiple APK Support
versionCode 30000 + android.defaultConfig.versionCode
versionNameSuffix "-minApi24"
...
}
minApi23 {
dimension "api"
minSdkVersion 23
versionCode 20000 + android.defaultConfig.versionCode
versionNameSuffix "-minApi23"
...
}
minApi21 {
dimension "api"
minSdkVersion 21
versionCode 10000 + android.defaultConfig.versionCode
versionNameSuffix "-minApi21"
...
}
}
}
...
You can also operate on productFlavors like this:
android.productFlavors.all { flavor ->
flavor.versionNameSuffix = "-${flavor.name}"
}
Upvotes: 2
Reputation: 689
There is no direct way of doing so. Currently, com.android.application
gradle-plugin does not support any DSL for productFlavors
to have versionNameSuffix
.
If you want to do this, you have to create two methods.
import java.util.regex.Matcher
import java.util.regex.Pattern
def getCurrentVersionSuffix() {
def currentFlavor = getCurrentFlavor()
if (currentFlavor.equals("prod")) {
return "-prod"
} else if (currentFlavor.equals("uat")) {
return "-uat"
} else if (currentFlavor.equals("dev")) {
return "-dev"
}
}
def getCurrentFlavor() {
String taskRequestName = getGradle().getStartParameter().getTaskRequests().toString()
Pattern pattern;
if (taskRequestName.contains("assemble"))
pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
else
pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
Matcher matcher = pattern.matcher(taskRequestName)
if (matcher.find()) {
return matcher.group(1).toLowerCase()
} else {
return "";
}
}
and update the buildTypes
DSL
buildTypes {
debug {
versionNameSuffix getCurrentVersionSuffix()
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
versionNameSuffix getCurrentVersionSuffix()
}
}
This is the only way to achieve right now. May be in future, they could provide some DSL or something.
I have written a blog on this (http://www.pcsalt.com/android/product-flavors-android/) and the code is pushed on GitHub (https://github.com/krrishnaaaa/product-flavours/blob/master/app/build.gradle).
Upvotes: 1