Skip to content

Commit a68b884

Browse files
davidmotsonDavid Motsonashvili
andauthored
Update CopyGoogleServicesPlugin (#7454)
This change alters the plugin to include a default google-services.json file that can be copied into the test app if the google-services.json is not present for some reason. This is highly conditional to maintain the existing workflow of using the test apps to actually test the sdks --------- Co-authored-by: David Motsonashvili <[email protected]>
1 parent e894db1 commit a68b884

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"project_info": {
3+
"project_number": "000000000",
4+
"firebase_url": "https://fakeProject.firebaseio.com",
5+
"project_id": "fakeProject",
6+
"storage_bucket": "fakeProject.firebasestorage.app"
7+
},
8+
"client": [
9+
{
10+
"client_info": {
11+
"mobilesdk_app_id": "1:0000000:android:fakeProjectCopyGoogleServices",
12+
"android_client_info": {
13+
"package_name": "com.example.myapplication"
14+
}
15+
},
16+
"api_key": [
17+
{
18+
"current_key": "aFakeKeyBecauseThisWholeJsonFileIsFake"
19+
}
20+
]
21+
}
22+
],
23+
"configuration_version": "1"
24+
}

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

Lines changed: 26 additions & 9 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
@@ -25,7 +26,8 @@ import org.gradle.kotlin.dsl.getByType
2526
import 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
*/
3638
abstract 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

Comments
 (0)