Joshua
Joshua

Reputation: 1270

Extending the cordova gradle file to include google services

I'm trying to include com.google.gms:google-services:3.0.0 into my cordova plugin without having to hack it into the main build.gradle file. I have added the following file to my application:

build-extras.gradle

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

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

I get the following error when trying to build the application:

* Where:
Script 'platforms/android/build-extras.gradle' line: 11

* What went wrong:
A problem occurred evaluating script.
> Plugin with id 'com.google.gms.google-services' not found.

I have attempted including build-extras.gradle using <framework src="src/android/build-extras.gradle" custom="true" type="gradleReference" /> and also manually copying it into platforms/android.

Cordova Reference: https://cordova.apache.org/docs/en/latest/guide/platforms/android/#extending-buildgradle

Google Reference: https://developers.google.com/identity/sign-in/android/start-integrating

If I take that same code from build-extras.gradle and directly append then to the end of build.gradle it seems to work. Any thoughts on how I can get this to work in my build-extras.gradle file?

Upvotes: 8

Views: 3243

Answers (2)

Joanne
Joanne

Reputation: 1236

You have to wrap your build-extras.gradle with

ext.postBuildExtras = { //... your gradle supplement }

Here is an example of your build-extras.gradle file:

ext.postBuildExtras = {
    android {
        packagingOptions {
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/license.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/notice.txt'
            exclude 'META-INF/ASL2.0'
        }

    }
    dependencies {
        compile files('libs/httpclient-4.5.4.jar', 'libs/httpcore-4.4.7.jar')
    }
}

and place it under the same folder as build.gradle file. After Cordova Android v7.0.0, you should place it like this:

$PROJECT_PATH/platforms/android/app/build.gradle
$PROJECT_PATH/platforms/android/app/build-extras.gradle

Upvotes: 1

andreszs
andreszs

Reputation: 2956

From some Gradle reference file:

Class must be used instead of id(string) to be able to apply plugin from non-root gradle file

You must include the class like this at the bottom of your gradle file:

ext.postBuildExtras = {
    apply plugin: com.google.gms.googleservices.GoogleServicesPlugin
}

Upvotes: 3

Related Questions