我正在尝试配置一个Android项目,将多个产品口味与风味尺寸相结合。
以下是build.gradle的片段
android {
...
flavorDimensions "vendor", "type"
productFlavors {
development {
dimension "vendor"
}
production {
dimension "vendor"
}
free {
dimension "type"
}
paid {
dimension "type"
}
}
...
}
我正在使用谷歌服务,我需要拥有google-services.json
文件,我希望每个供应商都有一个不同的文件,一个用于开发,另一个用于生产
所以google-services.json
中有一个app/src/development
而app/src/production
中有一个File google-services.json is missing. The Google Services Plugin cannot function without it.
Searched Location:
<path>/app/src/developmentPaid/debug/google-services.json
<path>/app/src/debug/developmentPaid/google-services.json
<path>/app/src/developmentPaid/google-services.json
<path>/app/src/debug/google-services.json
<path>/app/google-services.json
。
构建时我收到此错误:
google-services.json
从错误中我需要app/src/developmentFree
和app/src/developmentPaid
中的app/src/productionFree
以及app/src/productionPaid
和android
中的另一个{。}}。
使用风味尺寸时,是否需要在两个地方都使用相同的文件?
修改
我最终解决了这个问题,为每个合并的productFlavor添加了几个额外的任务。我在android.productFlavors.all { flavor ->
if (flavor.dimension == "vendor") {
task("copy${flavor.name.capitalize()}GoogleServicesFile", type: Copy) {
description = 'Switches to google-services.json depending on flavor'
from "src/${flavor.name}"
include "google-services.json"
into "."
}
task("delete${flavor.name.capitalize()}GoogleServicesFile", type: Delete) {
description = 'Delete google-services.json from base folder'
delete "./google-services.json"
}
}
}
android.applicationVariants.all { variant ->
def buildType = variant.buildType.name.capitalize()
def typeFlavorName = variant.productFlavors.get(0).name.capitalize()
def vendorFlavorName = variant.productFlavors.get(1).name.capitalize()
def copyFileTaskName = "copy${vendorFlavorName}GoogleServicesFile"
def deleteFileTaskName = "delete${vendorFlavorName}GoogleServicesFile"
def processGoogleServicesTaskName = "process${typeFlavorName}${vendorFlavorName}${buildType}GoogleServices"
tasks."${processGoogleServicesTaskName}".dependsOn "${copyFileTaskName}"
tasks."${processGoogleServicesTaskName}".finalizedBy "${deleteFileTaskName}"
}
之后添加了此代码:
afterEvaluate {
typeFlavorName
}
这样,对于每个vendorFlavorName
,buildType
和google-services.json
变体,app/
文件会在process{typeFlavorName}{vendorFlavorName}{buildType}GoogleServices
之前复制到union A
{
int a:1;
int b:2;
int c1:29;
int a:1;
int b:2;
int c2:28;
int d:1;
int val;
};
并删除之后。
答案 0 :(得分:3)
最后,我为每个合并后的productFlavor额外解决了两个问题。我在android
之后添加了此代码:
afterEvaluate {
android.productFlavors.all { flavor ->
if (flavor.dimension == "vendor") {
task("copy${flavor.name.capitalize()}GoogleServicesFile", type: Copy) {
description = 'Switches to google-services.json depending on flavor' from "src/${flavor.name}"
include "google-services.json" into "."
}
task("delete${flavor.name.capitalize()}GoogleServicesFile", type: Delete) {
description = 'Delete google-services.json from base folder'
delete "./google-services.json"
}
}
}
android.applicationVariants.all { variant ->
def buildType = variant.buildType.name.capitalize()
def typeFlavorName = variant.productFlavors.get(0).name.capitalize()
def vendorFlavorName = variant.productFlavors.get(1).name.capitalize()
def copyFileTaskName = "copy${vendorFlavorName}GoogleServicesFile"
def deleteFileTaskName = "delete${vendorFlavorName}GoogleServicesFile"
def processGoogleServicesTaskName = "process${typeFlavorName}${vendorFlavorName}${buildType}GoogleServices"
tasks."${processGoogleServicesTaskName}".dependsOn "${copyFileTaskName}"
tasks."${processGoogleServicesTaskName}".finalizedBy "${deleteFileTaskName}"
}
}
通过这种方式,对于每个typeFlavorName
,vendorFlavorName
和buildType
,google-services.json
文件在app/
之前被复制到process{typeFlavorName}{vendorFlavorName}{buildType}GoogleServices
并被删除之后。