有没有办法为各种产品风格设置不同的versionNameSuffix
,以便可以为构建类型执行此操作?
以下代码适用于我:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
verstionNameSuffix "-prod"
}
}
但是当我想为这样的产品风格设置versionNameSuffix
时:
productFlavors {
production {
versionNameSuffix "-prod"
}
development {
versionNameSuffix "-dev"
}
}
我收到此错误:
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.
是否有其他方法可以为产品风味设置versionNameSuffix
?
答案 0 :(得分:0)
没有直接的方法。目前,com.android.application
gradle-plugin不支持productFlavors
拥有versionNameSuffix
的任何DSL。
如果你想这样做,你必须创建两种方法。
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 "";
}
}
并更新buildTypes
DSL
buildTypes {
debug {
versionNameSuffix getCurrentVersionSuffix()
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
versionNameSuffix getCurrentVersionSuffix()
}
}
这是目前实现的唯一途径。可能在将来,他们可以提供一些DSL或其他东西。
我已经写了一篇博客(http://www.pcsalt.com/android/product-flavors-android/),代码推送到GitHub(https://github.com/krrishnaaaa/product-flavours/blob/master/app/build.gradle)。
答案 1 :(得分:0)
productFlavors现在支持versionNameSuffix
来自android开发人员文档
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"
...
}
}
}
...
您还可以像这样对productFlavor进行操作:
android.productFlavors.all { flavor ->
flavor.versionNameSuffix = "-${flavor.name}"
}