17
17
package com.google.firebase.gradle.plugins
18
18
19
19
import com.android.build.gradle.BaseExtension
20
+ import com.android.build.gradle.internal.tasks.factory.dependsOn
20
21
import java.io.File
21
22
import org.gradle.api.Plugin
22
23
import org.gradle.api.Project
@@ -25,7 +26,8 @@ import org.gradle.kotlin.dsl.getByType
25
26
import org.gradle.kotlin.dsl.register
26
27
27
28
/* *
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
29
31
*
30
32
* If a path is provided via `FIREBASE_GOOGLE_SERVICES_PATH`, that will be used instead. The file
31
33
* 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
35
37
*/
36
38
abstract class CopyGoogleServicesPlugin : Plugin <Project > {
37
39
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)
39
47
40
48
project.allprojects {
41
49
// fixes dependencies with gradle tasks that do not properly dependOn `preBuild`
42
50
tasks.configureEach {
43
- if (name != = " copyRootGoogleServices" ) dependsOn(copyRootGoogleServices)
51
+ if (name != = " copyRootGoogleServices" ) {
52
+ dependsOn(copyRootGoogleServices)
53
+ }
44
54
}
45
55
}
46
56
@@ -56,22 +66,19 @@ abstract class CopyGoogleServicesPlugin : Plugin<Project> {
56
66
return gradle.startParameter.taskNames.any { testTasks.any(it::contains) }
57
67
}
58
68
59
- private fun registerCopyRootGoogleServicesTask (project : Project ) =
69
+ private fun registerCopyRootGoogleServicesTask (project : Project , path : String ) =
60
70
project.tasks.register<Copy >(" copyRootGoogleServices" ) {
61
- val sourcePath =
62
- System .getenv(" FIREBASE_GOOGLE_SERVICES_PATH" ) ? : " ${project.rootDir} /google-services.json"
63
-
64
71
val library = project.extensions.getByType<BaseExtension >()
65
72
66
73
val targetPackageLine = " \" package_name\" : \" ${library.namespace} \" "
67
74
val packageLineRegex = Regex (" \" package_name\" :\\ s+\" .*\" " )
68
75
69
- from(sourcePath )
76
+ from(path )
70
77
into(project.projectDir)
71
78
72
79
rename { " google-services.json" }
73
80
74
- if (fileIsMissingPackageName(sourcePath , targetPackageLine)) {
81
+ if (fileIsMissingPackageName(path , targetPackageLine)) {
75
82
/* *
76
83
* Modifies `google-services.json` such that all declared `package_name` entries are
77
84
* replaced with the project's namespace. This tricks the google services plugin into
@@ -91,4 +98,14 @@ abstract class CopyGoogleServicesPlugin : Plugin<Project> {
91
98
92
99
return ! file.readText().contains(targetPackageLine)
93
100
}
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
+ }
94
111
}
0 commit comments