Skip to content

Instantly share code, notes, and snippets.

@ZakTaccardi
Created August 26, 2015 01:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ZakTaccardi/708d5e2ad7d7d5bbd084 to your computer and use it in GitHub Desktop.
Save ZakTaccardi/708d5e2ad7d7d5bbd084 to your computer and use it in GitHub Desktop.
Automatic per-variant google_services.json configurations with Gradle
//append code below to existing build.gradle
def appModuleRootFolder = '.'
def srcDir = 'src'
def googleServicesJson = 'google-services.json'
task switchToDebug(type: Copy) {
def buildType = 'debug'
description = 'Switches to DEBUG google-services.json'
from "${srcDir}/${buildType}"
include "$googleServicesJson"
into "$appModuleRootFolder"
}
task switchToRelease(type: Copy) {
def buildType = 'release'
description = 'Switches to RELEASE google-services.json'
from "${srcDir}/${buildType}/"
include "$googleServicesJson"
into "$appModuleRootFolder"
}
afterEvaluate {
processDebugGoogleServices.dependsOn switchToDebug
processReleaseGoogleServices.dependsOn switchToRelease
}
@Babay88
Copy link

Babay88 commented Sep 28, 2015

@dharmik8, you should add "classpath 'com.google.gms:google-services:1.4.0-beta3'" to your dependencies. like that:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'com.google.gms:google-services:1.4.0-beta3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

and you should add "apply plugin: 'com.google.gms.google-services'" to your project gradle file. something like that:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

@Kisty
Copy link

Kisty commented Oct 30, 2015

Couldn't you make a task programatically for supporting multiple flavours using something like:

android.productFlavors.all { productFlavor ->
    task("prepare${productFlavor.name.capitalize}{buildType}GoogleServices").dependsOn("process${buildType}GoogleServices") {
        ...
    }
}

@Kisty
Copy link

Kisty commented Nov 3, 2015

Argh, this doesn't work for concurrent builds.

In the end, I just used one API console project and registered it with multiple package names using https://developers.google.com/mobile/add. That way there's only one JSON file and no need for these custom tasks. :)

@ZakTaccardi
Copy link
Author

@dharmik8 the processDebugGoogleServices gradle task wont be visible to your build script until the afterEvaluate phase begins.

@ralphilius
Copy link

It should be def buildType = 'main' for task switchToRelease

@justintuchek
Copy link

for anybody stopping by on this page, buildType based google-services.json files are now supported.

update your top-level build file to utilize v2.1.0 of google play services (example)

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'com.google.gms:google-services:2.1.0'
    }
}

@yarinkos
Copy link

@Chrispassold
Copy link

Chrispassold commented Nov 30, 2017

I fixed the problem with processDebugGoogleServices using the code below:

afterEvaluate {
    project.tasks.findByName('processDebugGoogleServices')?.dependsOn switchToDebug
    project.tasks.findByName('processQualityGoogleServices')?.dependsOn switchToQuality
    project.tasks.findByName('processReleaseGoogleServices')?.dependsOn switchToRelease
}

It checks if the task exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment