8

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?

9
  • If you are trying to add dependency to your cordova plugin and not to cordova application, then you gotta place build-extras.gradle in your plugin folder and refer it using framework src tag in plugin.xml Check this link - cordova.apache.org/docs/en/latest/guide/platforms/android which says - "Note that plugins can also include build-extras.gradle files via: <framework src="some.gradle" custom="true" type="gradleReference" />"
    – Gandhi
    Commented Sep 28, 2016 at 5:06
  • @Gandhi Yes, I'm aware they can be included using the framework element. It doesn't matter which of those methods I use to include it the build-extras.grade file still isn't working.
    – Joshua
    Commented Sep 28, 2016 at 5:19
  • where is the file placed? it should be with in your plugins folder and referred from plugin.xml i guess. Could you post what you have tried?
    – Gandhi
    Commented Sep 28, 2016 at 5:22
  • I have updated the issue. I have tried using the framework element in the plugin.xml and also directly copying the gradle file to the platforms/android directory. The build-extras.gradle file is being called, but something in that file is causing the issue.
    – Joshua
    Commented Sep 28, 2016 at 5:30
  • I guess the gradle file should be inside plugins folder if you are adding dependency for the plugin.
    – Gandhi
    Commented Sep 28, 2016 at 5:46

2 Answers 2

3

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
}
1
  • 1
    it really vorques! :B Commented Oct 2, 2019 at 14:51
1

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
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.