Frank Andrew
Frank Andrew

Reputation: 917

How to fix Failed to apply plugin [class 'com.google.gms.googleservices.GoogleServicesPlugin']?

I'm newbie using ionic 1 and cordova, I try to install cordova-plugin-fcm use this comment:

cordova plugin add cordova-plugin-fcm

but when I build use:

ionic cordova run android

I get this error :

Script 'D:\net\netAppV2\platforms\android\cordova-plugin-fcm\netappv2-FCMPlugin.gradle' line: 13

  • What went wrong: A problem occurred evaluating script. Failed to apply plugin [class 'com.google.gms.googleservices.GoogleServicesPlugin'] For input string: "+"

and this is content of netappv2-FCMPlugin.gradle:

buildscript {
    repositories {
            jcenter()
            mavenLocal()
        }
    dependencies {
        classpath 'com.android.tools.build:gradle:+'
        classpath 'com.google.gms:google-services:3.0.0'
    }
}
// apply plugin: 'com.google.gms.google-services'
// class must be used instead of id(string) to be able to apply plugin from non-root gradle file
apply plugin: com.google.gms.googleservices.GoogleServicesPlugin

Please anyone help me to solve this problem.

Thanks.

Upvotes: 1

Views: 3613

Answers (2)

Oleksandr Yefymov
Oleksandr Yefymov

Reputation: 6509

I met the same issue, it was caused by play-services versions. You can use temporary fix by @Mani, but if you need non only temporary some additional workaround should be done:

1) create hook with before_prepare type. That is JS file, with content:

// That hook needed for fixing ANDROID Gradle issues with multiple `google.android.gms` services versions
// details:
// https://github.com/phonegap/phonegap-plugin-push/issues/1718

const fs = require('fs');
const path = require('path');

const FCM_VERSION = '11.8.0';

// You can include more of each string if there are naming conflicts,
// but for the GMS libraries each should be unique enough.
// The full list of libraries is here:
// https://developers.google.com/android/guides/setup
const LIBRARIES = [
    'play-services-analytics',
];

const createRegex = (item) => new RegExp(`${item}:.*`, 'ig');
const createReplace = (item) => `${item}:${FCM_VERSION}`;

module.exports = (ctx) => {
    const Q = ctx.requireCordovaModule('q');
    const deferred = Q.defer();

    const gradle = path.join(ctx.opts.projectRoot, 'platforms', 'android', 'project.properties');
    fs.readFile(gradle, 'utf8', (err, data) => {
        if (err) {
            return deferred.reject(err);
        }

        let result = data;
    LIBRARIES.forEach((lib) => {
        result = result.replace(createRegex(lib), createReplace(lib));
})

    return fs.writeFile(gradle, result, 'utf8', (err) => {
        if (err) {
            deferred.reject(err);
        }
    deferred.resolve();
});
});

    return deferred.promise;
};

2) declare that hook in your configuration for android platform

<hook type="before_prepare" src="hooks/google-services-versions-fix.js" />

3) in android/project.properties find all the versions that has + and related to play-services, like: cordova.system.library.7=com.google.android.gms:play-services-analytics:+

4) add such dependencies to hook to const LIBRARIES = section.

That will update needed dependencies to approprier version and resolve such issue!

Upvotes: 0

Mani Selva
Mani Selva

Reputation: 54

You can resolve that by :

1.Open your plugin.xml of cordova-plugin-fcm from plugins folder.

2.Find + and replace the version 11.0.1

3.Then remove and add platform

4.Then Build

Upvotes: 1

Related Questions