Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 267
Joshua Avatar asked Sep 28 '16 04:09

Joshua


People also ask

Which gradle version for Cordova?

Gradle. As of Cordova-Android 6.4. 0, Gradle is required to be installed. When installing on Windows, you need to add the path to the Gradle's binary directory to your path environment variable.

In which configuration file do we add the required dependencies?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.

What are Cordova plugins?

What is a Cordova plugin? A plugin is a bit of add-on code that provides JavaScript interface to native components. They allow your app to use native device capabilities beyond what is available to pure web apps.

What is Cordova Android version?

11.0.0 • Public • Published 4 months ago. 11 Dependencies.


2 Answers

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
}
like image 153
andreszs Avatar answered Sep 25 '22 03:09

andreszs


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
like image 28
Joanne Avatar answered Sep 22 '22 03:09

Joanne