play-services-plugins icon indicating copy to clipboard operation
play-services-plugins copied to clipboard

Allow specifying `google-services.json` values as output of a custom Gradle task

Open ZakTaccardi opened this issue 4 years ago • 2 comments

I would like to provide a custom location for the google-services.json file.

We are passing API keys as either gradle.properties or local.properties, and we dynamically generate the google-services.json file based on that, so we need to be able to specify the file generated as an output of a new gradle task that we can send as input to the Google Services Gradle Plugin

ZakTaccardi avatar Mar 19 '20 17:03 ZakTaccardi

Since google-services.json is expected to be in a specific place, could your task write the generated file to that expected place?

sperzion avatar Apr 03 '20 16:04 sperzion

Actually - I was under the mistaken impression that different flavors would have different expected locations for the google-services.json

For security reasons that prevent us from storing API keys directly in our GitHub, we have a custom gradle task createGoogleServicesJson that does what you expect. This means that before Gradle is started, the google-services.json file does not exist.

To ensure we generate the google-services.json file before any of the Google Services Gradle plugin tasks run, we do the following:

tasks.whenTaskAdded { task ->
    if (task.name.contains("GoogleServices")) {
        task.dependsOn createGoogleServicesJson
    }
}

However, this is non-idiomatic Gradle as this code would break or if you have a task that doesn't contain the String "GoogleServices" in the future.

To address this, an API similar to the following would be ideal:

googleServices {
  jsonLocation = createGoogleServicesJson.outputs.files // note: `createGoogleServicesJson` is a `Copy` task
}

ZakTaccardi avatar Apr 07 '20 21:04 ZakTaccardi