1717package com.google.firebase.gradle.plugins
1818
1919import com.android.build.gradle.BaseExtension
20+ import com.android.build.gradle.internal.tasks.factory.dependsOn
2021import java.io.File
2122import org.gradle.api.Plugin
2223import org.gradle.api.Project
@@ -25,7 +26,8 @@ import org.gradle.kotlin.dsl.getByType
2526import org.gradle.kotlin.dsl.register
2627
2728/* *
28- * Copies the root google-services.json into the project directory during build time.
29+ * Copies the root google-services.json into the project directory during build time. If the file
30+ * doesn't exist, a dummy file is created and copied instead
2931 *
3032 * If a path is provided via `FIREBASE_GOOGLE_SERVICES_PATH`, that will be used instead. The file
3133 * will also be renamed to `google-services.json`, so provided files do *not* need to be properly
@@ -35,12 +37,20 @@ import org.gradle.kotlin.dsl.register
3537 */
3638abstract class CopyGoogleServicesPlugin : Plugin <Project > {
3739 override fun apply (project : Project ) {
38- val copyRootGoogleServices = registerCopyRootGoogleServicesTask(project)
40+ if (File (project.projectDir, " google-services.json" ).exists()) {
41+ project.logger.warn(" Google Services file already present in project, skipping copy task" )
42+ return
43+ }
44+
45+ val sourcePath = getSourcePath(project)
46+ val copyRootGoogleServices = registerCopyRootGoogleServicesTask(project, sourcePath)
3947
4048 project.allprojects {
4149 // fixes dependencies with gradle tasks that do not properly dependOn `preBuild`
4250 tasks.configureEach {
43- if (name != = " copyRootGoogleServices" ) dependsOn(copyRootGoogleServices)
51+ if (name != = " copyRootGoogleServices" ) {
52+ dependsOn(copyRootGoogleServices)
53+ }
4454 }
4555 }
4656
@@ -56,22 +66,19 @@ abstract class CopyGoogleServicesPlugin : Plugin<Project> {
5666 return gradle.startParameter.taskNames.any { testTasks.any(it::contains) }
5767 }
5868
59- private fun registerCopyRootGoogleServicesTask (project : Project ) =
69+ private fun registerCopyRootGoogleServicesTask (project : Project , path : String ) =
6070 project.tasks.register<Copy >(" copyRootGoogleServices" ) {
61- val sourcePath =
62- System .getenv(" FIREBASE_GOOGLE_SERVICES_PATH" ) ? : " ${project.rootDir} /google-services.json"
63-
6471 val library = project.extensions.getByType<BaseExtension >()
6572
6673 val targetPackageLine = " \" package_name\" : \" ${library.namespace} \" "
6774 val packageLineRegex = Regex (" \" package_name\" :\\ s+\" .*\" " )
6875
69- from(sourcePath )
76+ from(path )
7077 into(project.projectDir)
7178
7279 rename { " google-services.json" }
7380
74- if (fileIsMissingPackageName(sourcePath , targetPackageLine)) {
81+ if (fileIsMissingPackageName(path , targetPackageLine)) {
7582 /* *
7683 * Modifies `google-services.json` such that all declared `package_name` entries are
7784 * replaced with the project's namespace. This tricks the google services plugin into
@@ -91,4 +98,14 @@ abstract class CopyGoogleServicesPlugin : Plugin<Project> {
9198
9299 return ! file.readText().contains(targetPackageLine)
93100 }
101+
102+ private fun getSourcePath (project : Project ): String {
103+ val path =
104+ System .getenv(" FIREBASE_GOOGLE_SERVICES_PATH" ) ? : " ${project.rootDir} /google-services.json"
105+ if (File (path).exists()) {
106+ return path
107+ }
108+ project.logger.warn(" Google services file not found, using fallback" )
109+ return " ${project.rootDir} /plugins/resources/dummy-google-services.json"
110+ }
94111}
0 commit comments