在做的安卓应用需要在 debug 和 release build中使用不同的谷歌服务账号,要用到不同的google-serivces.json ,手动替换的话太费时费力,好在万能的gradle可以完成轻松解决这个问题。解决步骤如下:
假设两个json文件分别放在一下目录:
app/src/debug/google_services.json app/src/main/google_services.json
在build.gradle新加两个task
task switchToDebug(type: Copy) { description = 'Switches to DEBUG google-services.json' from "src/debug" include "google-services.json" into "." } task switchToRelease(type: Copy) { description = 'Switches to RELEASE google-services.json' from "src/release" include "google-services.json" into "." }
接下来需要根据不同的build分别执行这两个task,‘com.google.gms.google-services’这个plugin定义两个task分别是
processDebugGoogleServices processReleaseGoogleServices
我们直接借用他们,添加:
afterEvaluate { processDebugGoogleServices.dependsOn switchToDebug processReleaseGoogleServices.dependsOn switchToRelease }
这样就会在编不同build时使用对应的google-services.json了。