From 573d4eddd9784b932a8eabf2dc0dd66d6269276e Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Wed, 8 Oct 2025 15:35:31 -0700 Subject: [PATCH 01/11] Update CopyGoogleServicesPlugin --- .../plugins/CopyGoogleServicesPlugin.kt | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt index 28af40f7e7a..ffc4bfc2a4b 100644 --- a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt +++ b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt @@ -17,6 +17,7 @@ package com.google.firebase.gradle.plugins import com.android.build.gradle.BaseExtension +import com.android.build.gradle.internal.tasks.factory.dependsOn import java.io.File import org.gradle.api.Plugin import org.gradle.api.Project @@ -26,6 +27,7 @@ import org.gradle.kotlin.dsl.register /** * Copies the root google-services.json into the project directory during build time. + * If the file doesn't exist, a dummy file is created and copied instead * * If a path is provided via `FIREBASE_GOOGLE_SERVICES_PATH`, that will be used instead. The file * will also be renamed to `google-services.json`, so provided files do *not* need to be properly @@ -36,6 +38,12 @@ import org.gradle.kotlin.dsl.register abstract class CopyGoogleServicesPlugin : Plugin { override fun apply(project: Project) { val copyRootGoogleServices = registerCopyRootGoogleServicesTask(project) + val sourcePath = + System.getenv("FIREBASE_GOOGLE_SERVICES_PATH") ?: "${project.rootDir}/google-services.json" + if (!File(project.projectDir, "google-services.json").exists() && !File(sourcePath).exists()) { + val createDummyGoogleServices = registerDummyGoogleServicesTask(project, sourcePath) + copyRootGoogleServices.dependsOn(createDummyGoogleServices) + } project.allprojects { // fixes dependencies with gradle tasks that do not properly dependOn `preBuild` @@ -56,6 +64,38 @@ abstract class CopyGoogleServicesPlugin : Plugin { return gradle.startParameter.taskNames.any { testTasks.any(it::contains) } } + private fun registerDummyGoogleServicesTask( + project: Project, + path: String + ) = project.tasks.register("createRootGoogleServices") { + File(path).writeText(""" + { + "project_info": { + "project_number": "1234567", + "firebase_url": "https://project-12345.firebaseio.com", + "project_id": "project-12345", + "storage_bucket": "project-12345.firebasestorage.app" + }, + "client": [ + { + "client_info": { + "mobilesdk_app_id": "1:1234567:android:12345ffff54321", + "android_client_info": { + "package_name": "com.example.myapplication" + } + }, + "api_key": [ + { + "current_key": "RaNdoMLett3r5aNdNuMb3rS" + } + ] + } + ], + "configuration_version": "1" + } + """.trimIndent()) + } + private fun registerCopyRootGoogleServicesTask(project: Project) = project.tasks.register("copyRootGoogleServices") { val sourcePath = From afa4c4188242d77354c0398f118a787bf6b1fe1e Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Thu, 9 Oct 2025 09:46:43 -0700 Subject: [PATCH 02/11] remove circular dependency --- .../firebase/gradle/plugins/CopyGoogleServicesPlugin.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt index ffc4bfc2a4b..813ff85215b 100644 --- a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt +++ b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt @@ -48,7 +48,9 @@ abstract class CopyGoogleServicesPlugin : Plugin { project.allprojects { // fixes dependencies with gradle tasks that do not properly dependOn `preBuild` tasks.configureEach { - if (name !== "copyRootGoogleServices") dependsOn(copyRootGoogleServices) + if (name !== "copyRootGoogleServices" || name!== "createRootGoogleServices") { + dependsOn(copyRootGoogleServices) + } } } From 1c794555ef5e39257ab56a546035de77fe70bb27 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Thu, 9 Oct 2025 09:48:51 -0700 Subject: [PATCH 03/11] add log --- .../google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt index 813ff85215b..478b3f0d3f4 100644 --- a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt +++ b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt @@ -70,6 +70,7 @@ abstract class CopyGoogleServicesPlugin : Plugin { project: Project, path: String ) = project.tasks.register("createRootGoogleServices") { + println("Google services file not found, using fallback") File(path).writeText(""" { "project_info": { From 3ccf58088572a61243bbb91ea9ad15a591c73a13 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Thu, 9 Oct 2025 09:57:50 -0700 Subject: [PATCH 04/11] binary logic fail --- .../google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt index 478b3f0d3f4..51d59dae23e 100644 --- a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt +++ b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt @@ -48,7 +48,7 @@ abstract class CopyGoogleServicesPlugin : Plugin { project.allprojects { // fixes dependencies with gradle tasks that do not properly dependOn `preBuild` tasks.configureEach { - if (name !== "copyRootGoogleServices" || name!== "createRootGoogleServices") { + if (name !== "copyRootGoogleServices" && name!== "createRootGoogleServices") { dependsOn(copyRootGoogleServices) } } From 8bfff2c59be0a3a7e0b5f4296f12238776621813 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Thu, 9 Oct 2025 10:51:12 -0700 Subject: [PATCH 05/11] format --- .../plugins/CopyGoogleServicesPlugin.kt | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt index 51d59dae23e..195243a79ca 100644 --- a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt +++ b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt @@ -26,8 +26,8 @@ import org.gradle.kotlin.dsl.getByType import org.gradle.kotlin.dsl.register /** - * Copies the root google-services.json into the project directory during build time. - * If the file doesn't exist, a dummy file is created and copied instead + * Copies the root google-services.json into the project directory during build time. If the file + * doesn't exist, a dummy file is created and copied instead * * If a path is provided via `FIREBASE_GOOGLE_SERVICES_PATH`, that will be used instead. The file * will also be renamed to `google-services.json`, so provided files do *not* need to be properly @@ -48,7 +48,7 @@ abstract class CopyGoogleServicesPlugin : Plugin { project.allprojects { // fixes dependencies with gradle tasks that do not properly dependOn `preBuild` tasks.configureEach { - if (name !== "copyRootGoogleServices" && name!== "createRootGoogleServices") { + if (name !== "copyRootGoogleServices" && name !== "createRootGoogleServices") { dependsOn(copyRootGoogleServices) } } @@ -66,12 +66,12 @@ abstract class CopyGoogleServicesPlugin : Plugin { return gradle.startParameter.taskNames.any { testTasks.any(it::contains) } } - private fun registerDummyGoogleServicesTask( - project: Project, - path: String - ) = project.tasks.register("createRootGoogleServices") { - println("Google services file not found, using fallback") - File(path).writeText(""" + private fun registerDummyGoogleServicesTask(project: Project, path: String) = + project.tasks.register("createRootGoogleServices") { + println("Google services file not found, using fallback") + File(path) + .writeText( + """ { "project_info": { "project_number": "1234567", @@ -96,8 +96,10 @@ abstract class CopyGoogleServicesPlugin : Plugin { ], "configuration_version": "1" } - """.trimIndent()) - } + """ + .trimIndent() + ) + } private fun registerCopyRootGoogleServicesTask(project: Project) = project.tasks.register("copyRootGoogleServices") { From d4216c797ca2ab49f6ef613f2724a8a905c74f3f Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Thu, 9 Oct 2025 13:22:12 -0700 Subject: [PATCH 06/11] minor refactor and fixes for comments --- plugins/resources/dummy-google-services.json | 24 +++++++++ .../plugins/CopyGoogleServicesPlugin.kt | 50 ++++--------------- 2 files changed, 35 insertions(+), 39 deletions(-) create mode 100644 plugins/resources/dummy-google-services.json diff --git a/plugins/resources/dummy-google-services.json b/plugins/resources/dummy-google-services.json new file mode 100644 index 00000000000..bb2bb819ac0 --- /dev/null +++ b/plugins/resources/dummy-google-services.json @@ -0,0 +1,24 @@ +{ + "project_info": { + "project_number": "000000000", + "firebase_url": "https://fakeProject.firebaseio.com", + "project_id": "fakeProject", + "storage_bucket": "fakeProject.firebasestorage.app" + }, + "client": [ + { + "client_info": { + "mobilesdk_app_id": "1:0000000:android:fakeProjectCopyGoogleServices", + "android_client_info": { + "package_name": "com.example.myapplication" + } + }, + "api_key": [ + { + "current_key": "aFakeKeyBecauseThisWholeJsonFileIsFake" + } + ] + } + ], + "configuration_version": "1" +} \ No newline at end of file diff --git a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt index 195243a79ca..47c0b5c633b 100644 --- a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt +++ b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt @@ -37,9 +37,9 @@ import org.gradle.kotlin.dsl.register */ abstract class CopyGoogleServicesPlugin : Plugin { override fun apply(project: Project) { - val copyRootGoogleServices = registerCopyRootGoogleServicesTask(project) val sourcePath = System.getenv("FIREBASE_GOOGLE_SERVICES_PATH") ?: "${project.rootDir}/google-services.json" + val copyRootGoogleServices = registerCopyRootGoogleServicesTask(project, sourcePath) if (!File(project.projectDir, "google-services.json").exists() && !File(sourcePath).exists()) { val createDummyGoogleServices = registerDummyGoogleServicesTask(project, sourcePath) copyRootGoogleServices.dependsOn(createDummyGoogleServices) @@ -48,7 +48,7 @@ abstract class CopyGoogleServicesPlugin : Plugin { project.allprojects { // fixes dependencies with gradle tasks that do not properly dependOn `preBuild` tasks.configureEach { - if (name !== "copyRootGoogleServices" && name !== "createRootGoogleServices") { + if (name !== "copyRootGoogleServices" && name !== "copyDummyGoogleServices") { dependsOn(copyRootGoogleServices) } } @@ -67,56 +67,28 @@ abstract class CopyGoogleServicesPlugin : Plugin { } private fun registerDummyGoogleServicesTask(project: Project, path: String) = - project.tasks.register("createRootGoogleServices") { - println("Google services file not found, using fallback") - File(path) - .writeText( - """ - { - "project_info": { - "project_number": "1234567", - "firebase_url": "https://project-12345.firebaseio.com", - "project_id": "project-12345", - "storage_bucket": "project-12345.firebasestorage.app" - }, - "client": [ - { - "client_info": { - "mobilesdk_app_id": "1:1234567:android:12345ffff54321", - "android_client_info": { - "package_name": "com.example.myapplication" - } - }, - "api_key": [ - { - "current_key": "RaNdoMLett3r5aNdNuMb3rS" - } - ] - } - ], - "configuration_version": "1" - } - """ - .trimIndent() - ) + project.tasks.register("copyDummyGoogleServices") { + logger.warn("Google services file not found, using fallback") + + from("${project.rootDir}/plugins/resources/dummy-google-services.json") + into(path) + rename { "google-services.json" } } - private fun registerCopyRootGoogleServicesTask(project: Project) = + private fun registerCopyRootGoogleServicesTask(project: Project, path: String) = project.tasks.register("copyRootGoogleServices") { - val sourcePath = - System.getenv("FIREBASE_GOOGLE_SERVICES_PATH") ?: "${project.rootDir}/google-services.json" val library = project.extensions.getByType() val targetPackageLine = "\"package_name\": \"${library.namespace}\"" val packageLineRegex = Regex("\"package_name\":\\s+\".*\"") - from(sourcePath) + from(path) into(project.projectDir) rename { "google-services.json" } - if (fileIsMissingPackageName(sourcePath, targetPackageLine)) { + if (fileIsMissingPackageName(path, targetPackageLine)) { /** * Modifies `google-services.json` such that all declared `package_name` entries are * replaced with the project's namespace. This tricks the google services plugin into From 78b0e62849478b9e9899bfa72eb8ba18a3de0895 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Thu, 9 Oct 2025 13:28:21 -0700 Subject: [PATCH 07/11] format --- .../google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt index 47c0b5c633b..3cb2b3069b6 100644 --- a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt +++ b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt @@ -77,7 +77,6 @@ abstract class CopyGoogleServicesPlugin : Plugin { private fun registerCopyRootGoogleServicesTask(project: Project, path: String) = project.tasks.register("copyRootGoogleServices") { - val library = project.extensions.getByType() val targetPackageLine = "\"package_name\": \"${library.namespace}\"" From c817207b1046953ccd53654f10ea35b5c3605a2b Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Thu, 9 Oct 2025 13:37:36 -0700 Subject: [PATCH 08/11] format --- .../plugins/CopyGoogleServicesPlugin.kt | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt index 3cb2b3069b6..c584e4b0042 100644 --- a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt +++ b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt @@ -37,13 +37,8 @@ import org.gradle.kotlin.dsl.register */ abstract class CopyGoogleServicesPlugin : Plugin { override fun apply(project: Project) { - val sourcePath = - System.getenv("FIREBASE_GOOGLE_SERVICES_PATH") ?: "${project.rootDir}/google-services.json" + val sourcePath = getSourcePath(project) val copyRootGoogleServices = registerCopyRootGoogleServicesTask(project, sourcePath) - if (!File(project.projectDir, "google-services.json").exists() && !File(sourcePath).exists()) { - val createDummyGoogleServices = registerDummyGoogleServicesTask(project, sourcePath) - copyRootGoogleServices.dependsOn(createDummyGoogleServices) - } project.allprojects { // fixes dependencies with gradle tasks that do not properly dependOn `preBuild` @@ -66,15 +61,6 @@ abstract class CopyGoogleServicesPlugin : Plugin { return gradle.startParameter.taskNames.any { testTasks.any(it::contains) } } - private fun registerDummyGoogleServicesTask(project: Project, path: String) = - project.tasks.register("copyDummyGoogleServices") { - logger.warn("Google services file not found, using fallback") - - from("${project.rootDir}/plugins/resources/dummy-google-services.json") - into(path) - rename { "google-services.json" } - } - private fun registerCopyRootGoogleServicesTask(project: Project, path: String) = project.tasks.register("copyRootGoogleServices") { val library = project.extensions.getByType() @@ -107,4 +93,13 @@ abstract class CopyGoogleServicesPlugin : Plugin { return !file.readText().contains(targetPackageLine) } + + private fun getSourcePath(project: Project): String { + val path = + System.getenv("FIREBASE_GOOGLE_SERVICES_PATH") ?: "${project.rootDir}/google-services.json" + if (File(project.projectDir, "google-services.json").exists() || File(path).exists()) { + return path + } + return "${project.rootDir}/plugins/resources/dummy-google-services.json" + } } From 5295a5adf6e4f6eef9f78e8d1ebfd93bffc28062 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Thu, 9 Oct 2025 13:39:36 -0700 Subject: [PATCH 09/11] refactor out extra task --- .../google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt index c584e4b0042..c5c00ea32de 100644 --- a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt +++ b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt @@ -43,7 +43,7 @@ abstract class CopyGoogleServicesPlugin : Plugin { project.allprojects { // fixes dependencies with gradle tasks that do not properly dependOn `preBuild` tasks.configureEach { - if (name !== "copyRootGoogleServices" && name !== "copyDummyGoogleServices") { + if (name !== "copyRootGoogleServices") { dependsOn(copyRootGoogleServices) } } @@ -100,6 +100,7 @@ abstract class CopyGoogleServicesPlugin : Plugin { if (File(project.projectDir, "google-services.json").exists() || File(path).exists()) { return path } + project.logger.warn("Google services file not found, using fallback") return "${project.rootDir}/plugins/resources/dummy-google-services.json" } } From 8f07a3bcce1439272cc255b05ffccb6513f08668 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Thu, 9 Oct 2025 14:58:33 -0700 Subject: [PATCH 10/11] refactored to explicitly do nothing in cases where a google-service file is already present in the directory --- .../gradle/plugins/CopyGoogleServicesPlugin.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt index c5c00ea32de..f19ae6c676e 100644 --- a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt +++ b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt @@ -38,6 +38,11 @@ import org.gradle.kotlin.dsl.register abstract class CopyGoogleServicesPlugin : Plugin { override fun apply(project: Project) { val sourcePath = getSourcePath(project) + if (sourcePath == null) { + project.logger.warn("Google Services file already present in project, skipping copy task") + return + } + val copyRootGoogleServices = registerCopyRootGoogleServicesTask(project, sourcePath) project.allprojects { @@ -94,10 +99,13 @@ abstract class CopyGoogleServicesPlugin : Plugin { return !file.readText().contains(targetPackageLine) } - private fun getSourcePath(project: Project): String { + private fun getSourcePath(project: Project): String? { val path = System.getenv("FIREBASE_GOOGLE_SERVICES_PATH") ?: "${project.rootDir}/google-services.json" - if (File(project.projectDir, "google-services.json").exists() || File(path).exists()) { + if (File(project.projectDir, "google-services.json").exists()) { + return null + } + if (File(path).exists()) { return path } project.logger.warn("Google services file not found, using fallback") From 9983dec96f2c3d576d68c9f599059029efef2019 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Thu, 9 Oct 2025 15:00:04 -0700 Subject: [PATCH 11/11] refactor out the if --- .../firebase/gradle/plugins/CopyGoogleServicesPlugin.kt | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt index f19ae6c676e..603143c84ad 100644 --- a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt +++ b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt @@ -37,12 +37,12 @@ import org.gradle.kotlin.dsl.register */ abstract class CopyGoogleServicesPlugin : Plugin { override fun apply(project: Project) { - val sourcePath = getSourcePath(project) - if (sourcePath == null) { + if (File(project.projectDir, "google-services.json").exists()) { project.logger.warn("Google Services file already present in project, skipping copy task") return } + val sourcePath = getSourcePath(project) val copyRootGoogleServices = registerCopyRootGoogleServicesTask(project, sourcePath) project.allprojects { @@ -99,12 +99,9 @@ abstract class CopyGoogleServicesPlugin : Plugin { return !file.readText().contains(targetPackageLine) } - private fun getSourcePath(project: Project): String? { + private fun getSourcePath(project: Project): String { val path = System.getenv("FIREBASE_GOOGLE_SERVICES_PATH") ?: "${project.rootDir}/google-services.json" - if (File(project.projectDir, "google-services.json").exists()) { - return null - } if (File(path).exists()) { return path }