Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions plugins/resources/dummy-google-services.json
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +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.
* 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
Expand All @@ -35,12 +37,20 @@ import org.gradle.kotlin.dsl.register
*/
abstract class CopyGoogleServicesPlugin : Plugin<Project> {
override fun apply(project: Project) {
val copyRootGoogleServices = registerCopyRootGoogleServicesTask(project)
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 {
// fixes dependencies with gradle tasks that do not properly dependOn `preBuild`
tasks.configureEach {
if (name !== "copyRootGoogleServices") dependsOn(copyRootGoogleServices)
if (name !== "copyRootGoogleServices") {
dependsOn(copyRootGoogleServices)
}
}
}

Expand All @@ -56,22 +66,19 @@ abstract class CopyGoogleServicesPlugin : Plugin<Project> {
return gradle.startParameter.taskNames.any { testTasks.any(it::contains) }
}

private fun registerCopyRootGoogleServicesTask(project: Project) =
private fun registerCopyRootGoogleServicesTask(project: Project, path: String) =
project.tasks.register<Copy>("copyRootGoogleServices") {
val sourcePath =
System.getenv("FIREBASE_GOOGLE_SERVICES_PATH") ?: "${project.rootDir}/google-services.json"

val library = project.extensions.getByType<BaseExtension>()

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
Expand All @@ -91,4 +98,14 @@ abstract class CopyGoogleServicesPlugin : Plugin<Project> {

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(path).exists()) {
return path
}
project.logger.warn("Google services file not found, using fallback")
return "${project.rootDir}/plugins/resources/dummy-google-services.json"
}
}
Loading