我正在尝试处理google-services.json和不同的口味.文档说我们需要根文件夹中的文件.
我有一个任务,可以轻松地将文件从flavor文件夹复制到根文件夹:
task CopyToRoot(type: Copy) {
def appModuleRootFolder = '.'
def srcDir = 'src'
def googleServicesJson = 'google-services.json'
outputs.upToDateWhen { false }
def flavorName = android.productFlavors.flavor1.name
description = "Switches to $flavorName $googleServicesJson"
delete "$appModuleRootFolder/$googleServicesJson"
from "${srcDir}/$flavorName/"
include "$googleServicesJson"
into "$appModuleRootFolder"
}
Run Code Online (Sandbox Code Playgroud)
然后,在afterEvaluate I force
afterEvaluate {
processFlavor1DebugGoogleServices.dependsOn CopyToRoot
processFlavor1ReleaseGoogleServices.dependsOn CopyToRoot
}
Run Code Online (Sandbox Code Playgroud)
这仅适用于1种风味(静态定义).如何为每种口味做到这一点?我有4种口味.如何获得正在编译的当前风味?
[ 更新1 ] - 也尝试过:
task CopyToRoot(type: Copy) {
def appModuleRootFolder = '.'
def srcDir = 'src'
def googleServicesJson = 'google-services.json'
outputs.upToDateWhen { false }
def flavorName = …Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用了flavor维度,并且我一直在使用for循环将applicationId设置为我生成的flavor:
flavorDimensions "appname", "brand"
productFlavors {
user {
dimension "appname"
}
installer {
dimension "appname"
}
branda {
dimension "brand"
}
brandb {
dimension "brand"
}
brandc {
dimension "brand"
}
brandd {
dimension "brand"
}
}
Run Code Online (Sandbox Code Playgroud)
我过滤那些我现在不支持的:
variantFilter { variant ->
def names = variant.flavors*.name
if (names.contains("installer") && (names.contains("brandc") || names.contains("brancd")) ) {
variant.ignore = true
}
}
Run Code Online (Sandbox Code Playgroud)
然后我根据flavor名称更新applicationId:
applicationVariants.all { variant ->
def flavorString = variant.getVariantData().getVariantConfiguration().getFlavorName()
def mergedFlavour = variant.getVariantData().getVariantConfiguration().getMergedFlavor();
switch (flavorString) {
/**
* user
*/
case …Run Code Online (Sandbox Code Playgroud)