默默地默默

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

在做的安卓应用需要在 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了。

 

 

原文: https://medium.com/google-cloud/automatic-per-variant-google-services-json-configurations-with-gradle-d3d3e40abc0e#.pxurhrjle

http://www.limelife.cn/p/17/

posted on 2015-12-03 18:39  zzub  阅读(4714)  评论(0编辑  收藏  举报