11

I want to keep different configurations for my debug/release build variants but apparently, the google-services.json file only allows for one. Is there any alternative? Is there a way to keep several files?

Roberto Betancourt
  • 2,375
  • 3
  • 27
  • 35

2 Answers2

18

I'm using this workaround to solve a similar issue with build flavours.

The flavour specific google-service.json files are stored under /app/src/{flavour-name}/google-service.json. To copy this to the /app dir the following code may be added to the /app/build.gradle file:

    gradle.taskGraph.beforeTask { Task task ->
        if(task.name ==~ /process.*GoogleServices/) {
            android.applicationVariants.all { variant ->
                if(task.name ==~ /(?i)process${variant.name}GoogleServices/){ 
                    copy {
                        from "/src/${variant.flavorName}"
                        into '.'
                        include 'google-services.json'
                    }
                }
            }
        }
    }

In the absence of flavours (as I understand your question) the following /app/build.gradle code snippet did the job in an android studio test project:

    gradle.taskGraph.beforeTask { Task task ->
        if(task.name ==~ /process.*GoogleServices/) {
            android.applicationVariants.all { variant ->
                if(task.name ==~ /(?i)process${variant.name}GoogleServices/){
                    copy {
                        from '.'
                        into '.'
                        rename { String fileName ->
                            fileName.replace("google-services-${variant.name}.json", 'google-services.json')
                        }
                        include "google-services-${variant.name}.json"
                    }
                }
            }
        }
    }

The snippet expects a google-services-debug.json and a google-services-release.json in your /app dir and copies and renames it to google-services.json.

Hope this helps.

Martin S.
  • 181
  • 3
  • 1
    this will run for all the variants each time. Right ? didn't try it but tried stuff like that. – ahmed_khan_89 Aug 08 '15 at 15:43
  • 1
    I needed to remove the first / before src `from "src/${variant.flavorName}"` but otherwise @MartinS your solution is just plain awesome. – Richard Poole Oct 06 '15 at 20:06
  • 1
    it should be from "src/${variant.flavorName}", not from "/src/${variant.flavorName}". Please verify – DàChún Oct 13 '15 at 21:37
  • I can confirm that the first / must be removed. For me, it worked properly with Gradle 1.3.1 and Play Services plugin 1.3.1, but when updating both to 1.5.0, the following error was shown: `File google-services.json is missing from module root folder. The Google Services Plugin cannot function without it` – Ereza Nov 24 '15 at 18:00
  • @AndroidGecko Yes, it works if you remove the first slash: `from "/src/${variant.flavorName}"` becomes `from "src/${variant.flavorName}"` – Ereza Dec 01 '15 at 13:41
  • Do we need to add a different project in firebase for each variant to get google-services.json file. – Rohan Lodhi May 31 '21 at 14:21
0

No there is no way to define build specific configs at this point. Good idea though, seems like something useful.

Mark Pazon
  • 6,167
  • 2
  • 34
  • 50
Arthur Thompson
  • 9,087
  • 4
  • 29
  • 33