如果每個味道都有自己的applicationID和你想有一個不同的跟蹤Google Analytics的ID,那麼您需要爲每種口味生成google-services.json。
請小心,因爲谷歌-services.json必須在應用程序文件夾下放置,你需要時,你變了味道手動複製的味道谷歌,services.json到app文件夾。
這裏是我的方式做到這一點:
比方說,我們有兩種口味,開發和生產。然後在src/development/google-services文件夾中放置用於開發的google-services.json。並在src/production/google-services文件夾中進行生產。
這時我們就需要配置這些谷歌 - service.json文件的複製任務。把這個腳本放在app文件夾下的build.gradle中。對我而言,我把這個腳本放在android {...}
之下。
android {
...
}
task switchToDevelopment(type: Copy) {
description = 'Switches to DEVELOPMENT google-services.json'
from "src/development/google-services"
include "google-services.json"
into "."
}
task switchToProduction(type: Copy) {
description = 'Switches to PRODUCTION google-services.json'
from "src/production/google-services"
include "google-services.json"
into "."
}
afterEvaluate {
processDevelopmentDebugGoogleServices.dependsOn switchToDevelopment
processDevelopmentReleaseGoogleServices.dependsOn switchToDevelopment
processProductionDebugGoogleServices.dependsOn switchToProduction
processProductionReleaseGoogleServices.dependsOn switchToProduction
}
無論何時更改風格,都會執行該腳本。在執行process[FlavorBuildtypes]GoogleServices
之前,它會將正確的google-services.json文件複製到應用程序文件夾中。希望能幫助到你! :)