Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
@ZakTaccardi
Copy link
Author

ZakTaccardi commented Dec 14, 2015

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

@ralphilius
Copy link

ralphilius commented Feb 23, 2016

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

@justintuchek
Copy link

justintuchek commented Apr 28, 2016

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

yarinkos commented Nov 29, 2017

@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