我发现了另一种做法,并且想要分享一下。
请注意,这是我第一次使用gradle编写任务,因此代码并不完美(我现在无法花更多时间来改进它)。
说明
我所做的事情非常简单。
1)在处理FlavorBuildTypeGoogleServices进程之前,也就是来自Google服务的读取google-services.json文件的任务,我触发了一些代码来更新google-services.json文件。
为了完成这个目标:
gradle.taskGraph.beforeTask { Task task ->
if (task.name.startsWith("process") && task.name.endsWith("GoogleServices")) {
}
}
2) 从任务名称中获取当前风味和构建类型(任务名称示例:processProdReleaseGoogleServices,格式为 process'Flavor''BuildType'GoogleServices)
String currentFlavor = task.name.replace("process", "").replace("GoogleServices", "")
currentFlavor = currentFlavor.toLowerCase()
3) 从currentFlavor变量中删除buildType。为了做到这一点,我只需遍历项目中的所有buildTypes,并将它们从currentFlavor变量中删除。
android.applicationVariants.all { variant ->
currentFlavor = currentFlavor.replace(variant.buildType.name, "")
}
此时,变量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 ->
if (task.name.startsWith("process") && task.name.endsWith("GoogleServices")) {
String currentFlavor = task.name.replace("process", "").replace("GoogleServices", "")
currentFlavor = currentFlavor.toLowerCase()
android.applicationVariants.all { variant ->
currentFlavor = currentFlavor.replace(variant.buildType.name, "")
}
String currentApplicationId;
android.applicationVariants.all { variant ->
if (variant.flavorName == currentFlavor) {
currentApplicationId = variant.productFlavors.applicationId.toString().replace("[", "").replace("]", "")
}
}
updateGoogleServicesJsonFile(currentApplicationId)
}
}