我找到了另一种方法,并想分享它。
请注意,这是我第一次使用 gradle 编写一些任务,所以代码根本不是最优的(我现在不能花更多的时间来改进它)。
说明
我所做的很简单。
1) 就在任务 processFlavorBuildTypeGoogleServices 之前,即来自 Google 服务的将读取 google-services.json 文件的任务,我触发了一些将更新 google-services.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) 从我的 build.gradle 中定义的风味中检索包名称
在我的 build.gradle 中,我为每种风味指定了 packageName:
productFlavors {
prod {
applicationId 'packageName1'
}
rec {
applicationId 'packageName2'
}
}
我这样检索它:
(包名是用[]返回的,所以我必须删除它们。例如我会检索[packageName1])
String currentApplicationId;
android.applicationVariants.all { variant ->
if (variant.flavorName == currentFlavor) {
currentApplicationId = variant.productFlavors.applicationId.toString().replace("[", "").replace("]", "")
}
}
5) 现在我有了当前构建的包名,我只需要打开当前的 google-services.json 文件,并更新里面的包名。为此,我添加了一个方法 updateGoogleServicesJsonFile。
请注意将第二行的 filePath 更改为指向您的位置。
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"))
}
你有它,一些代码在读取它的任务执行之前更新 google-services.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)
}
}