Gradle 编译时选择不同的 google-services.json

在做的安卓应用需要在 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分别是

1

2

processDebugGoogleServices

processReleaseGoogleServices

我们直接借用他们,添加:

afterEvaluate {
    processDebugGoogleServices.dependsOn switchToDebug
    processReleaseGoogleServices.dependsOn switchToRelease
}

这样就会在编不同build时使用对应的google-services.json了。

转自:https://www.cnblogs.com/zzub/p/5017211.html

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值