diff --git a/plugins/resources/dummy-google-services.json b/plugins/resources/dummy-google-services.json new file mode 100644 index 00000000000..bb2bb819ac0 --- /dev/null +++ b/plugins/resources/dummy-google-services.json @@ -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" +} \ No newline at end of file diff --git a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt index 28af40f7e7a..603143c84ad 100644 --- a/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt +++ b/plugins/src/main/java/com/google/firebase/gradle/plugins/CopyGoogleServicesPlugin.kt @@ -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 @@ -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 @@ -35,12 +37,20 @@ import org.gradle.kotlin.dsl.register */ abstract class CopyGoogleServicesPlugin : Plugin { 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) + } } } @@ -56,22 +66,19 @@ abstract class CopyGoogleServicesPlugin : Plugin { return gradle.startParameter.taskNames.any { testTasks.any(it::contains) } } - private fun registerCopyRootGoogleServicesTask(project: Project) = + private fun registerCopyRootGoogleServicesTask(project: Project, path: String) = project.tasks.register("copyRootGoogleServices") { - val sourcePath = - System.getenv("FIREBASE_GOOGLE_SERVICES_PATH") ?: "${project.rootDir}/google-services.json" - val library = project.extensions.getByType() 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 @@ -91,4 +98,14 @@ abstract class CopyGoogleServicesPlugin : Plugin { 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" + } }