Pushwoosh / pushwoosh-phonegap-plugin

Pushwoosh PhoneGap Build Plugin
Other
109 stars 139 forks source link

Defer to cordova-android Google Services Plugin #360

Closed davidofwatkins closed 2 years ago

davidofwatkins commented 2 years ago

Cordova introduced GradlePluginGoogleServicesEnabled and GradlePluginGoogleServicesVersion in cordova-android 9:

To use Google APIs or Firebase services, the Gradle plugin Google Services is required to be enabled when building your Android application.

For plugin developers, this can be enabled by setting to the app's config.xml the GradlePluginGoogleServicesEnabled preference flag. From the plugin.xml file, you can do this by adding the following lines:

<config-file target="config.xml" parent="/*">
<preference name="GradlePluginGoogleServicesEnabled" value="true" />
<preference name="GradlePluginGoogleServicesVersion" value="4.2.0" />
</config-file>

This avoids conflicting versions of Google Services between Cordova plugins in projects like mine that use several of them. If I build my app after upgrading cordova-android and adding GradlePluginGoogleServicesEnabled to my config.xml, I get the following error:

FAILURE: Build failed with an exception.

* Where:
Build file '/path/to/cordova-project/platforms/android/app/build.gradle' line: 368

* What went wrong:
A problem occurred evaluating project ':app'.
> Failed to apply plugin 'com.google.gms.google-services'.
   > Cannot add extension with name 'googleServices', as there is an extension already registered with that name.

I was able to resolve this by removing the following lines from Pushwoosh's build.gradle:

https://github.com/Pushwoosh/pushwoosh-phonegap-plugin/blob/59bbd7371b093edebb6e915a7db51c2ac45ce5cb/build.gradle#L18-L22

wfhm commented 2 years ago

@davidofwatkins Thanks for the suggestion! We will check if this solution works as expected and, if it does, this will be added with the next release. I will provide a status update shortly.

unekinn commented 2 years ago

Did you have time to check this, @wfhm? It affects a project I'm working on as well. Right now I have to script removal of the duplicate apply plugin line during automated builds.

wfhm commented 2 years ago

@larskinn yes, we've tested it, and it works fine in Cordova projects, however, it is a bit more complicated for us since we have to make sure that all supported Cordova-based frameworks (specifically, Outsystems MABS) work as expected with this change. We've temporarily put this task on hold, but I believe we can still implement it or at least give you a definitive answer on whether we will do it or not by the end of 2021.

unekinn commented 2 years ago

@wfhm Thanks for the update. For now, I've added an after_plugin_install hook to fix the issue in our build.

Non-working hook #### `config.xml` ```xml ... ... ... ``` #### `scripts/fixPushwooshGradleFile.js` (replace `` with the appropriate value for your filename, as it is different per project) ```js const fs = require('fs'); const path = require('path'); module.exports = function cordovaHook(context) { if (context.opts.plugin.id !== 'pushwoosh-cordova-plugin') { return; } const gradleFilePath = path.join( context.opts.projectRoot, 'platforms/android/pushwoosh-cordova-plugin/-build.gradle' ); const gradleFile = fs.readFileSync(gradleFilePath, 'utf-8'); fs.writeFileSync( gradleFilePath, gradleFile.replace( /^(cdvPluginPostBuildExtras.add\({.*\n\r?}\))$/ms, '/* Removed to fix https://github.com/Pushwoosh/pushwoosh-phonegap-plugin/issues/360 */' ), 'utf-8' ); console.log( `Removed duplicate plugin 'com.google.gms.google-services' from ${gradleFilePath}` ); }; ```

UPDATE: Actually, "fixing" it this way leads to the following compilation error

Could not determine the dependencies of task ':app:compileReleaseJavaWithJavac'.
> In project 'app' a resolved Google Play services library dependency depends on another at an exact version (e.g. "[1.0.0
  , 1.2.99]", but isn't being resolved to that version. Behavior exhibited by the library will be unknown.

  Dependency failing: com.pushwoosh:pushwoosh-amazon:6.3.3 -> androidx.annotation:annotation@[1.0.0, 1.2.99], but annotati
  on version was 1.1.0.

However, removing the apply plugin line from platforms/android/app/build.gradle in a after_platform_add hook works fine. Seems like a more hacky workaround, though.

wfhm commented 2 years ago

@larskinn @davidofwatkins the plugin uses GradlePluginGoogleServicesEnabled and GradlePluginGoogleServicesVersion preferences starting with the 8.3.0.