Skip to content

Commit

Permalink
Allow setting severity of missing google-services.json
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtek-kalicinski committed Feb 23, 2023
1 parent f1fc5ac commit 464f686
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 5 deletions.
31 changes: 31 additions & 0 deletions google-services-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,37 @@ Place the `google-services.json` file for your project in the `src/main/google-s
Alternatively, you can use variant specific source-sets, for example:
`src/debug/google-services/google-services.json`.

#### Compatible Android plugins

The `com.google.gms.google-services` plugin can only be applied to projects with
`com.android.application` or `com.android.dynamic-feature`, as it requires an `applicationId`
to function.

The plugin is not compatible with plugins such as `com.android.library` that do not
contain an `applicationId`.

#### Plugin configuration

Configure the plugin's behavior through the `googleServices` block in build.gradle.kts:

```
googleServices {
// Disables checking of Google Play Services dependencies compatibility
// Default: false
disableVersionCheck = true
// Choose the behavior when google-services.json is missing:
// Default: MissingGoogleServicesStrategy.ERROR
// Possible options: IGNORE, WARN, ERROR
missingGoogleServicesStrategy = MissingGoogleServicesStrategy.WARN
}
```

You can use `missingGoogleServicesStrategy` when some variants in your project
do not require Google Play Services and are missing the `google-services.json` file.

#### Android Gradle plugin compatibility

The Google Services plugin requires AGP 7.3.0 or newer to work.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class GoogleServicesPlugin : Plugin<Project> {
"process${variant.name.capitalize()}GoogleServices",
GoogleServicesTask::class.java
) {
it.missingGoogleServicesStrategy.set(
project.extensions.getByType(GoogleServicesPluginConfig::class.java)
.missingGoogleServicesStrategy
)
it.googleServicesJsonFiles.set(variant.sources.getByName(SOURCE_TYPE).all)
it.applicationId.set(variant.applicationId)
}
Expand Down Expand Up @@ -128,11 +132,22 @@ class GoogleServicesPlugin : Plugin<Project> {
const val SOURCE_TYPE = "google-services"
}


enum class MissingGoogleServicesStrategy {
IGNORE, WARN, ERROR
}

open class GoogleServicesPluginConfig {
/**
* Disables checking of Google Play Services dependencies compatibility.
*/
var disableVersionCheck = false

/**
* Choose the behavior when google-services.json is missing.
* Defaults to ERROR, other possible values are: WARN, IGNORE.
*/
var missingGoogleServicesStrategy = MissingGoogleServicesStrategy.ERROR
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.gms.googleservices

import com.google.gms.googleservices.GoogleServicesPlugin.MissingGoogleServicesStrategy
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import org.gradle.api.DefaultTask
Expand Down Expand Up @@ -51,10 +52,14 @@ abstract class GoogleServicesTask : DefaultTask() {
@get:Input
abstract val applicationId: Property<String>

@get:Input
abstract val missingGoogleServicesStrategy: Property<MissingGoogleServicesStrategy>

@Throws(GradleException::class)
@TaskAction
fun action() {
val jsonFiles = googleServicesJsonFiles.get().map { it.file(JSON_FILE_NAME).asFile }
val jsonFiles = googleServicesJsonFiles.get()
.map { it.file(JSON_FILE_NAME).asFile }
.filter { it.isFile }
if (jsonFiles.size > 1) {
throw GradleException(
Expand All @@ -66,13 +71,20 @@ abstract class GoogleServicesTask : DefaultTask() {
}

if (jsonFiles.isEmpty()) {
throw GradleException(
"""
val message = """
File $JSON_FILE_NAME is missing.
The Google Services Plugin cannot function without it.
Searched locations: ${googleServicesJsonFiles.get().joinToString { it.asFile.path }}
""".trimIndent()
)

when (missingGoogleServicesStrategy.get()) {
MissingGoogleServicesStrategy.ERROR -> throw GradleException(message)
MissingGoogleServicesStrategy.WARN -> logger.warn(message)
MissingGoogleServicesStrategy.IGNORE -> {
// ignore
}
}
return
}

val quickstartFile = jsonFiles.single()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class GoogleServicesPluginTest {
}

@Test
fun `missing Google Services file`() {
fun `missing Google Services file - error`() {
val projectName = "project1"

copyProjectToTemp(projectName)
Expand All @@ -115,6 +115,49 @@ class GoogleServicesPluginTest {
Assert.assertTrue(buildResult.output.contains("File google-services.json is missing"))
}

@Test
fun `missing Google Services file - warn`() {
val projectName = "project1"

copyProjectToTemp(projectName)
tempFolder.root.resolve("app/src/main/google-services/google-services.json").delete()
val buildFile = tempFolder.root.resolve("app/build.gradle.kts")
buildFile.writeText(buildFile.readText().replace(
"MissingGoogleServicesStrategy.ERROR",
"MissingGoogleServicesStrategy.WARN"
))
val buildResult = runBuild()

Assert.assertEquals(
TaskOutcome.SUCCESS,
buildResult.task(":app:processFreeOneDebugGoogleServices")?.outcome
)
Assert.assertTrue(buildResult.output.contains("File google-services.json is missing"))
}

@Test
fun `missing Google Services file - ignore`() {
val projectName = "project1"

copyProjectToTemp(projectName)
tempFolder.root.resolve("app/src/main/google-services/google-services.json").delete()
val buildFile = tempFolder.root.resolve("app/build.gradle.kts")
buildFile.writeText(buildFile.readText().replace(
"MissingGoogleServicesStrategy.ERROR",
"MissingGoogleServicesStrategy.IGNORE"
))

val buildResult = runBuild()

Assert.assertEquals(
TaskOutcome.SUCCESS,
buildResult.task(":app:processFreeOneDebugGoogleServices")?.outcome
)
Assert.assertFalse(buildResult.output.contains("File google-services.json is missing"))
}



@Test
fun `flavor specific google-services,json`() {
val projectName = "project3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ plugins {
id("com.google.gms.google-services")
}

import com.google.gms.googleservices.GoogleServicesPlugin.MissingGoogleServicesStrategy

android {
namespace = "com.example.myapplication"
compileSdk = 33
Expand Down Expand Up @@ -34,4 +36,8 @@ android {
dimension = "bar"
}
}
}

googleServices {
missingGoogleServicesStrategy = MissingGoogleServicesStrategy.ERROR
}

0 comments on commit 464f686

Please sign in to comment.