Adding Cordova (Phonegap) Plugins manually to existing android projects - android

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.

Related

How to launch a Cordova project with full rebuilding of plugins?

I'm working on a Cordova plugin. As a plugin can't be executed, I'm using a Cordova project to test it. I found that running a Cordova project will not reimport and rebuild plugins, so currently I'm doing this:
rmdir \"./plugins/my-plugin\" /q /s
cordova platform remove android
cordova platform add android
cordova run android --device
Did I miss something or Cordova really doesn't have a command to fully rebuild everything before running the project?
Unfortunately, cordova will check your plugins folder only when adding or updating a plugin or platform. Then it will build and copy the needed plugin(s) into the designated OS in the platforms folder.
By your script I assume the source of the plugin is outside of the project folder. So the script you're using should do the work. But if I may give you a better solution would be as follows:
Create a Symlink/shortcut of the source folder for your plugin into the plugins folder, so you'll not need to remove the folder and stress the disk into copying files everytime you want to run the update.
Your "update" script should delete only the installed plugin folder at platforms/android/platform_www/plugins/my-plugin and it'll make the update far more quickier than removing and adding the platform again.

`.gitignore` for an Ionic project with Android target

I'm creating an Ionic project using Capacitor (edit: the newer recommended framework compared to Cordova) and added Android as a target.
I've been able to build the app and add the Android target, which creates a bucketload of files under android/, using:
ionic build
ionic cap add android
Then, I was able to launch Android Studio and emulate my app, using npx cap open android, with "Run App" in Android Studio. This added a few more bucketloads of files.
I was able to get most of them to be ignored by adding the "ionic" and "AndroidStudio" gitignore entries from gitignore.io, but if someone clones the repo, they can rebuild the entire Android directory from scratch, right? Do I just consider everything in the android folder to be a build artifact that can be recreated from the core ionic project using capacitor (i.e. "cap").
Is there anything that needs to be saved to the repo in the ionic android directory? If so, which files - or should I fall back to ionic/Android Studio gitignore entries for the android folder?
Is there anything that needs to be saved to the repo in the ionic android directory?
I would suggest that you include your native folders in your repo as well.
Based on my experience with capacitor over the past year while developing two applications, consider these things.
Most of the times you will be using at least a few cordova plugins, since capacitor still doesn't have a viable plugin for certain features.
These plugins, will require you to manually add some variables ( like sdk keys ) or initialization function on the native side of the codes.
In fact, even for capacitor community plugins ( which are plugins created by other samaritans ), you will have to add some minimum lines of code to the native side. Consider this plugin for facebook sign in. Just go through the installation steps, you will see a long list of configurations on both ios and android sides.
Capacitor is not formerly Cordova. Both of these are different frameworks. Capacitor completely depends on native ide's for compilation, whereas cordova cli could build the native codes for you (ios, mac required).
but if someone clones the repo, they can rebuild the entire Android directory from scratch, right?
When your application grows, depending on its intended functionality, this would not be an easy task. They will have to go through all these minor tweaks to get it working.
All in all, put your native code in the repo as well. For me, the repository size with the native codes is not terribly huge.
In Capacitor you are supposed to commit the ios and android folders. They contain their own .gitignore files with the recommended files for iOS and Android.
They are not build artifacts, if you change icons or configurations (in Info.plist, AndroidManifest.xml, etc) and you delete the folders, those changes will be lost.
If you clone a Capacitor project you should run this commands:
npm install (or other install package depending on your package manager)
npm run build (or ionic build if using ionic)
npx cap sync (this copies files and generates non committed files)
As #jcesarmobile
In Capacitor you are supposed to commit the ios and android folders. They contain their own .gitignore files with the recommended files for iOS and Android.
But if you're looking for a .gitignore to use at the root of your project, this one should do the job.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
.vscode
.idea
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Optional eslint cache
.eslintcache

How can I cleanly automate Cordova plugin management in non-Cordova based android application?

Preface
I have an android app that was already prebuilt but I later decided I wanted to add Cordova functionality by embedding it in the webviews. I was able to accomplish this by following the instructions
here on the Cordova official documentation (made the jar, applied it to my view, ect)
This works fine and dandy if I simply download the plugins and manage them manually I.E. manually move the java and js files over to my project following the proper project structure AND manually editing the config.xml and cordova_plugins.js files.
The Problem
I want to take all the manual work out of the equation when adding a new plugin. I am looking for a way to have the simplicity of "cordova plugin install" but I cannot utilize this tool because my project "is not a Cordova project." I did not set it up using Cordova but instead just added the lib jar to my project.
What I have tried
Cordova package management tools - Not a option. "Not a Cordova project"
NPM - Pulls everything but also doesn't handle the configuration changes.
Plugman - Works! BUT it is installing the whole plugin library into my project including all the other platforms (blackberry, ios, ect) code that I do not want. I understand that these libs also contain config information for the plugins, but this does simply causes my app size to be huge and adds a bunch of unneeded files.
Example of plugman command:
plugman install --platform android --project project/path/ofManifest --plugin cordova-plugin-camera
As you can see I do provide the platform and it does plug into the app properly but the original plugin library all sticks around as well.
For all of the above I actually setup a package json and have npm fire off post install js scripts. When finished it will then go use npm/cordova/plugman to install the plugins I need.
Note - I have not tried ionic because I cannot get past the firewall with the download
The question
Is there a way to just install the android-only version of a plugin including the auto configuration modifications that the other tools already do without adding a bunch of other noise the the plugin provides? I have looked around google and stack and plugman is the general answer but as state in my write up, it is including other platform lib code that I do not want in my apk.

In phonegap On adding a new plugin if I rebuild my app it is removing the total project and creating a new one

Im trying to add a new plugin in phonegap, in order to work a new plugin we should rebuild the app. But if i rebuild the app my entire project is removed and it is creating a new one.
how can i use the plugin without rebuild or how can i rebuild app without loosing my code.
Im trying to add the splashscreen plugin.
First off all: I would recommend you to get a deeper look into Cordova. Therefore you need to read the Cordova Documentation.
The reason for just having a clear index and things like that is, that the command cordova build will copy all files from the root www folder into your platforms. That means, that you probably have worked in the www folders from your platforms.
Your folder structure is sth like this
So if you run the cordova build command it will copy all files from the www folder (2.) in your folders for your platforms (1.). If you now have not worked in the root www folder, it'll just copy the files which were created while you build the project the first time.
Normaly the Plugins you installed are available directly after you installed them - without a rebuild. So i would first recommend you to update your cordova (npm update -g cordova). After that you should create a new project and copy your files from your last project into the root www folder. A workflow for that should look like follows:
cordova create testProject com.example.com testProject
cd testProject
cordova platform add [platform you need]
cordova plugin add org.apache.cordova.splashscreen [plugins you need] - please note: Since Cordova is on V5, all Plugins are available via npm so the new command for installing plugins looks like: cordova plugin add cordova-plugin-splashscreen.
After you added all now you're copying the index and css file from your project into your new www root folder
Now you run cordova build
If you want an extra plugin now, which you have not installed when you created your project, you close all files your IDE (Xcode or Android Studio) and then move into your project with your terminal/console. There you can add a plugin now and it should be available after it is installed with cordova plugin add cordova-plugin-* without the build command!
Hope that helped you, let me know if you need further information!

phonegap - navigator.connection.type not defined

I am trying to check for an established network connection through the phonegap/cordova API, but alas, it is not working.
I added the plugin as explained on the cordova documentation, and phonegap local plugin list gives [phonegap] org.apache.cordova.network-information.
However, simply calling
document.addEventListener("deviceready", function () {
alert(Object.keys(navigator.connection));
}, false);
results on my phone (Android 4.0.4) always returning 0 and running on an emulator with Android 4.4.2 logs into LogCat that navigator.connection is undefined. In fact, on both machines alert(Object.keys(navigator)); never shows the connection key.
Any solution?
Phonegap with Cordova 3.3.0
When you install the plugin it copies files to www/plugins and updates android.json
then when you build the project, the build copies the java to platforms/android/src/... and the js files to platforms\android\assets\www\plugins (and does any customisation defined in plugin.xml)
At multiple occasions I saw android.json not correctly updated and javascript files not copied. Removing and re-installing plugins solved the problem for me.
If you did not perform manual customisations to the platforms/android folder, I think the easiest way to clean your project would be to empty the /plugins folder and the platforms folder and then re-install all the plugins you need.
I had the same issue and found I had to run "cordova build" and then the status was returned correctly.
BEWARE
When I run cordova build, it appears to take everything in my ~/app/www directory and overried everything in app/platforms/android/assets/www/
My "install process" is as follows:
cordova create app com.app "App"
cd app
cordova platform add android
cordova plugin add org.apache.cordova.network-information
cordova plugin add org.apache.cordova.camera
cordova plugin add org.apache.cordova.geolocation
cordova build
I can then do code changes in app/www and when happy, 'deploy' it using 'cordova build' (which seems to always copy the files to app/platforms/android/assets/www/.
If I add another plugin using: (for example)
cordova plugin add org.apache.cordova.file
then I need to run
cordova build
to have it work.
I hope this helps
(I am using cordova 3.3.1-0.1.2 )

Categories

Resources