Skip to content

Commit 573d4ed

Browse files
author
David Motsonashvili
committed
Update CopyGoogleServicesPlugin
1 parent e9ce620 commit 573d4ed

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.firebase.gradle.plugins
1818

1919
import com.android.build.gradle.BaseExtension
20+
import com.android.build.gradle.internal.tasks.factory.dependsOn
2021
import java.io.File
2122
import org.gradle.api.Plugin
2223
import org.gradle.api.Project
@@ -26,6 +27,7 @@ import org.gradle.kotlin.dsl.register
2627

2728
/**
2829
* Copies the root google-services.json into the project directory during build time.
30+
* If the file 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
@@ -36,6 +38,12 @@ import org.gradle.kotlin.dsl.register
3638
abstract class CopyGoogleServicesPlugin : Plugin<Project> {
3739
override fun apply(project: Project) {
3840
val copyRootGoogleServices = registerCopyRootGoogleServicesTask(project)
41+
val sourcePath =
42+
System.getenv("FIREBASE_GOOGLE_SERVICES_PATH") ?: "${project.rootDir}/google-services.json"
43+
if (!File(project.projectDir, "google-services.json").exists() && !File(sourcePath).exists()) {
44+
val createDummyGoogleServices = registerDummyGoogleServicesTask(project, sourcePath)
45+
copyRootGoogleServices.dependsOn(createDummyGoogleServices)
46+
}
3947

4048
project.allprojects {
4149
// fixes dependencies with gradle tasks that do not properly dependOn `preBuild`
@@ -56,6 +64,38 @@ abstract class CopyGoogleServicesPlugin : Plugin<Project> {
5664
return gradle.startParameter.taskNames.any { testTasks.any(it::contains) }
5765
}
5866

67+
private fun registerDummyGoogleServicesTask(
68+
project: Project,
69+
path: String
70+
) = project.tasks.register("createRootGoogleServices") {
71+
File(path).writeText("""
72+
{
73+
"project_info": {
74+
"project_number": "1234567",
75+
"firebase_url": "https://project-12345.firebaseio.com",
76+
"project_id": "project-12345",
77+
"storage_bucket": "project-12345.firebasestorage.app"
78+
},
79+
"client": [
80+
{
81+
"client_info": {
82+
"mobilesdk_app_id": "1:1234567:android:12345ffff54321",
83+
"android_client_info": {
84+
"package_name": "com.example.myapplication"
85+
}
86+
},
87+
"api_key": [
88+
{
89+
"current_key": "RaNdoMLett3r5aNdNuMb3rS"
90+
}
91+
]
92+
}
93+
],
94+
"configuration_version": "1"
95+
}
96+
""".trimIndent())
97+
}
98+
5999
private fun registerCopyRootGoogleServicesTask(project: Project) =
60100
project.tasks.register<Copy>("copyRootGoogleServices") {
61101
val sourcePath =

0 commit comments

Comments
 (0)