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分别是

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/


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM