我正在尝试处理google-services.json和不同风格的问题。文档表示我们需要在根文件夹中拥有该文件。
我有一个任务可以轻松将该文件从风格文件夹复制到根文件夹:
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"
}
然后,在afterEvaluate中,我强制执行
afterEvaluate {
processFlavor1DebugGoogleServices.dependsOn CopyToRoot
processFlavor1ReleaseGoogleServices.dependsOn CopyToRoot
}
这只适用于一个已静态定义的口味。如何针对每种口味做到这一点?我有4种口味。如何获取正在编译的当前口味?
[更新1] - 也尝试过:
task CopyToRoot(type: Copy) {
def appModuleRootFolder = '.'
def srcDir = 'src'
def googleServicesJson = 'google-services.json'
outputs.upToDateWhen { false }
def flavorName = android.productFlavors.flavor1.name
android.applicationVariants.all { variant ->
def flavorString = variant.getVariantData().getVariantConfiguration().getFlavorName()
println('flavorString: ' + flavorString)
description = "Switches to $flavorString $googleServicesJson"
delete "$appModuleRootFolder/$googleServicesJson"
from "${srcDir}/$flavorString/"
include "$googleServicesJson"
into "$appModuleRootFolder"
}
}
但是这并没有复制正确的文件。有什么帮助吗?