我找到了另一种方法,并想与大家分享。
请注意,这是我第一次使用gradle编写一些任务,因此代码根本不是最优的(目前我无法花更多时间来改进它)。
解释
我所做的很简单。
1) 就在任务流程FlavorBuildTypeGoogleServices之前,这是来自Google Services的任务,它将读取Google服务。json文件,我触发一些代码来更新google服务。json文件。
为此:
gradle.taskGraph.beforeTask { Task task ->
if (task.name.startsWith("process") && task.name.endsWith("GoogleServices")) {
}
}
2) 从任务名称中检索当前的flavor和buildType(任务名称的示例:processProdReleaseGoogleServices,格式为process'flavor''buildType'GoogleServices)
String currentFlavor = task.name.replace("process", "").replace("GoogleServices", "")
currentFlavor = currentFlavor.toLowerCase()
3) 从currentFlavor变量中删除buildType。为了做到这一点,我只需循环遍历项目中的所有buildType,并将其从currentFlavor变量中删除
android.applicationVariants.all { variant ->
currentFlavor = currentFlavor.replace(variant.buildType.name, "")
}
此时,变量currentFlavor具有currentFlavor(例如“prod”)
4) 从我的构建中定义的风格中检索包名称。格拉德尔
在我的构建中。gradle,我为每种口味指定了包装名称:
productFlavors {
prod {
applicationId 'packageName1'
}
rec {
applicationId 'packageName2'
}
}
我这样检索它:
(包名返回时带有[],因此我必须删除它们。例如,我将检索[PackageName 1])
String currentApplicationId;
android.applicationVariants.all { variant ->
if (variant.flavorName == currentFlavor) {
currentApplicationId = variant.productFlavors.applicationId.toString().replace("[", "").replace("]", "")
}
}
5) 现在我有了当前构建的包名,我只需要打开当前的google服务。json文件,并更新其中的包名称。为此,我添加了一个方法updategogleservicesjsonfile。
请小心更改第二行上的文件路径以指向您的位置。
def updateGoogleServicesJsonFile(applicationId) {
File file = new File(getProjectDir(), "/google-services.json")
if (!file.exists())
{
project.logger.log(LogLevel.ERROR, "Error updating the google-services.json because the file doesn't exists...")
return
}
List<String> lineList = file.readLines()
for (int i = 0; i < lineList.size(); i++)
{
if (lineList.get(i).trim().startsWith("\"package_name\": \""))
{
String line = lineList.get(i)
line = line.substring(0, line.indexOf(":") + 1)
line += " \"" + applicationId + "\""
lineList.set(i, line)
}
}
file.write(lineList.join("\n"))
}
这里有一些更新谷歌服务的代码。在执行读取任务之前的json文件。
密码
def updateGoogleServicesJsonFile(applicationId) {
File file = new File(getProjectDir(), "/google-services.json")
if (!file.exists())
{
project.logger.log(LogLevel.ERROR, "Error updating the google-services.json because the file doesn't exists...")
return
}
List<String> lineList = file.readLines()
for (int i = 0; i < lineList.size(); i++)
{
if (lineList.get(i).trim().startsWith("\"package_name\": \""))
{
String line = lineList.get(i)
line = line.substring(0, line.indexOf(":") + 1)
line += " \"" + applicationId + "\""
lineList.set(i, line)
}
}
file.write(lineList.join("\n"))
}
gradle.taskGraph.beforeTask { Task task ->
// Before the task processFlavorBuildTypeGoogleServices (such as processProdReleaseGoogleServices), we update the google-services.json
if (task.name.startsWith("process") && task.name.endsWith("GoogleServices")) {
// Getting current flavor name out of the task name
String currentFlavor = task.name.replace("process", "").replace("GoogleServices", "")
currentFlavor = currentFlavor.toLowerCase()
android.applicationVariants.all { variant ->
currentFlavor = currentFlavor.replace(variant.buildType.name, "")
}
// Getting current application id that are defined in the productFlavors
String currentApplicationId;
android.applicationVariants.all { variant ->
if (variant.flavorName == currentFlavor) {
currentApplicationId = variant.productFlavors.applicationId.toString().replace("[", "").replace("]", "")
}
}
updateGoogleServicesJsonFile(currentApplicationId)
}
}