Stop cordova cli reverting build.gradle - android

This one sounds a little bit weird!
I'm building an app with cordova, and I'm trying to implement firebase. I'm receiving the following error whenever I try to build
Failed to apply plugin [class 'com.google.gms.googleservices.GoogleServicesPlugin']
For input string: "11+"
I've had a look on stack overflow and the fix is to remove the + from the build.gradle dependencies. I remove it and then build through android studio which works great, but whenever I try to build via cli using cordova build android, it reverts the build.gradle and adds the + back on and the build fails.
I need able to build via Cordova build android because if I don't build the application again via the cli, my changes to the code are not recognized!?
It's almost like every time I make a change, I have to run the build for the code changes to be recognized.
How can I build via cordova build android without it adding the + back into the build.grade?

In a Cordova Android project, any files (including build.gradle) inside the platforms/android/ directory are considered volatile and should not be manually edited. In fact if you run cordova platform rm android the entire platforms/android/ directory will be deleted. The build.gradle file is dynamically recreated every time the cordova prepare lifecycle event takes place using a template from cordova-android and then is dynamically modified as Cordova plugins, etc. are processed.
So whatever solution you choose needs to work with the Cordova build lifecycle.
One option to is to use 3rd-party plugins to override the library versions specified by other Cordova plugins in order to align them and prevent such build failures, for example:
cordova-play-services-version-adapter
cordova-android-support-gradle-release
cordova-android-play-services-gradle-release
cordova-android-firebase-gradle-release

Related

Cordova build issue relating to fontVariationSettings

I am having a build issue with a Cordova project that I have inherited specifically the error raised is: -
:CordovaLib:transformClassesAndResourcesWithPrepareIntermediateJarsForDebug
:app:processDebugResourcesC:\Users\andy.gradle\caches\transforms-1\files-1.1\support-compat-28.0.0.aar\96270291fb4c7e49ddab8d4d1a4f36f7\res\values\values.xml:133:5-70: AAPT: error: resource android:attr/fontVariationSettings not found.
I have another project which builds successfully and the difference between the two projects appears to be the platforms\android\project.settings file, if I manually change the setting my project builds. However as this file appears to be re-created every time I run cordova prepare android this is not an ideal situation, the setting in question is: -
cordova.system.library.1=com.android.support:support-v4:+
if I change this to: -
cordova.system.library.1=com.android.support:support-v4:24.1.1+
my build is successful!
So my question is what impacts this setting, as I feel I need to understand the reasoning behind the differing value as other than the working project having more dependencies the non-working project contains the same references but has one additional dependency this being: -
cordova-plugin-file-opener2
I do not have SDK version 28 installed, I can see the logic behind the error and I would assume installing version 28 may resolve the issue but I would like to understand why one project creates the settings with one version number and the second project is created with a differing one?
In Cordova projects, the versions of native libraries pulled into the Android build via Gradle are controlled directly by the Cordova plugins you have installed in your project.
The Gradle config is dynamically generated by Cordova, so each time you run cordova prepare android, the build.gradle file is regenerated.
In this case, the library dependency is the Android Support Library (com.android.support).
cordova-plugin-file-opener2 specifies version + which will pull in the very latest version, which is currently v28.0.0 as can be seen on the releases page.
However, if another plugin in your project specifies a different version, this will lead to problems and often build failures due to version conflicts.
In a native Android project, the build.gradle file is under your direct control, so you'd never have these problems.
One solution is to install the cordova-android-support-gradle-release plugin into your project.
This plugin overrides the versions of the Android Support Library specified by other plugins, enabling you to align the versions and so fix the build problems.
For example, in your case you can run:
cordova plugin add cordova-android-support-gradle-release --variable ANDROID_SUPPORT_VERSION=24.1.1
This will install the plugin and override the versions specified by other plugins to ensure v24.1.1 of the Support Library is included in your project.
One consideration is that v24 is quite old, so you may want to try a newer version, e.g.:
cordova plugin add cordova-android-support-gradle-release --variable ANDROID_SUPPORT_VERSION=27.+
Just to add some detail to my solution just in case it helps anybody else in the future!
DaveAlden added a very helpful description above of what is happening which is worth reading through as it is very difficult to get a clear concise explanation of what is or should be happening with these technologies.
My project did indeed already have the cordova-android-support-gradle-release plugin installed and it was set to 27.+ however I followed the following process and everything then started working: -
Updated my Android SDK software to the latest
Updated Cordova from version 8.0.0 to 8.1.2
Removed plugin cordova plugin add cordova-android-support-gradle-release
Added plugin back in cordova plugin add cordova-android-support-gradle-release --variable ANDROID_SUPPORT_VERSION=27
I then retried the build and everything then started working.
UPDATE:
I came to deploy my app again today and I have got the same error again, to resolve it I ran: -
cordova plugin remove cordova-android-support-gradle-release
cordova plugin add cordova-android-support-gradle-release --variable ANDROID_SUPPORT_VERSION=27.+
and it then builds successfully, I have no idea why this occurs but it is something to do with the \platforms\android\project.properties file not being setup correctly as when it builds successfully this file contains the following line: -
cordova.gradle.include.1=cordova-android-support-gradle-release/portal-cordova-android-support-gradle-release.gradle
I'm sure somebody with a clearer understanding of the build process could add some clarity!

Can't add downloaded plugin to Cordova/Phonegap project

How can I get a plugin to work by installing it NOT from the repository.
Each plugin I try to install using phonegap plugin add <<git repository>> always results in different errors, because windows + npm just doesn't want to work for me...
This is the plugin I need: https://github.com/VersoSolutions/CordovaClipboard
My steps:
Download Git repository
Run phonegap plugin add <<downloaded and extracted directory>>
Remove and add android platform (yes it says that it installed the plugin)
Start Phonegap Developer app
But cordova.plugins.clipboard is still undefined. Do I have to put the clipboard.js file somewhere or reference it? If so, which version?
The one in the clipboard project or the one phonegap automatically creates within the platform/android directory (it puts a "define" around it). I actually tried both, but the object was still undefined.
The clipboard plugin does not have a package.json present which is why you are getting the error. You can either add a package.json file to the plugin or you can install it with the --nofetch flag:
cordova/phonegap plugin add https://github.com/VersoSolutions/CordovaClipboard.git --nofetch
Since cordova 7.0.0 npm install is run by default when adding a new plugin which causes an error if no package.json is present. You can find the release notes for cordova 7.0.0 here.

In Cordova/Phonegap why we need to build the project for each platform?

I created a new cordova project (from the command line, cordova create firstProject com.examplefirstproject FirstProject), then I added the platoform that I need (cordova platform add android) and at this point I read from the Cordova documentation that I need to build the project (cordova build android).
Why we need to build the project?
I tried to emulate the project without building it and it works fine. So, I'm asking why I should run the command to build the project.
The "only difference" from a project where I run the build command is the presence of more folders in firstproject\platforms\android (here is the list of folders and files of the not builded project and here is the list of the builded project).
The build command will recompile the app. There are many situations when this is important to do. Whenever, you make a change to core functionality of the app (not the HTML, JS, CSS etc), you will need to re-build the app for these changes to become apparent. Also, you will find when your project is finished, in order to publish the app you will have to build a release version of the app.
You don't need to build the app when you are merely editing the content of your app, which is what is causing your confusion it would seem.
It's also worth noting that when you use the 'run' command to emulate the app, this will also create a fresh build of the app.

Adding Cordova (Phonegap) Plugins manually to existing android projects

I'm trying to add additional Phonegap plugins (namely, the File plugin) to an existing android application that uses Phonegap.
I've tried navigating to my project directory (in my workspace) and then run the command
cordova plugin add https://theurlwhichworks.org/
It says the plugin is installing, and when I go to look in my workspace on my computer, there is a folder titled plugins, and inside, a directory called
org.apache.cordova.file
which means that the plugin (apparently) downloaded correctly, but doesn't seem to have been added to my project.
Is there any way to manually add this plugin (or others) to my android project directly? I build+run my application to an attached device from eclipse, because anytime I try to use the CLI with regard to cordova/phonegap I always seem to be doing something wrong, or the result is not at all what I expected.
Hopefully it is possible to add these plugins manually. I appreciate any suggestions
It is possible to add plugins manually, and that is how we did it in the old days before the Cordova/Phonegap CLI or PlugMan
options
A. use PlugMan directly
npm install -g plugman
check out the plugman documentation. Outside of a Cordova project, plugman is your best bet for a clean automated install
B. manually
add the .java files to the android project src/ directory
remember their path has to match their namespace
dump the plugin js file somewhere in assets/www/*
add a feature tag to platform config.xml
It has been a long time since I added a plugin manually, and the cordova plugin spec has gone through major changes multiple times since then. You may need to manually include the plugin js implementation (eg via script tags in index.html)
I would highly recommend you use PlugMan, or at least try before the manual install.
Seems you're using eclipse.
A cordova 3.x project looks like this :
/www
/plugins
/platforms
/platforms/android
/platforms/android/assets/www
...
When you run cordova plugin add https://theurlwhichworks.org/ the plugin is added only in the /plugins folder but not in your platform folder.
In your eclipse project you only see /platforms/android folder.
To actually add the plugin to the android platform, you have to build the platform :
cordova build android
or at least prepare the platform (same as build but does not try to compile):
cordova prepare android
But be aware that when you either build or prepare, the content of the platform/android/assets/www folder will be replaced with the content of the root www folder.
So if you've been modifying directly the assets/www folder in eclipse, DO NOT FORGET TO SAVE IT BEFORE YOU BUILD OR PREPARE!!!
Every time plugins start acting funny for me I usually remove and re-add the platform and everything usually sorts itself out. The commands for Android for example,
cordova platform remove android
cordova platform add android
If your plugin list has what you expect, re-adding the platform re-downloads all the plugins.

Is there a way to not rebuild cordova applications when adding a new plugin?

The title is self-explanatory but I will try to explain better.
I have a Cordova app for Android and iOS and I want to add a new plugin without the need to use the command cordova build [insert your platform here] because that command builds all the application and removes the current code and sets the project like the beginning.
How I can add plugins without having to backup all the existing project, build the platform and copy again the code with the plugins properly added?

Categories

Resources