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