I'm developing a Cordova plugin for the Android platform.
As described here, I edited the platforms/android/res/xml/config.xml
file to include the plugin's class mapping.
It all works perfectly fine except that every time I run cordova prepare android
this file get's overwritten saying:
Generating config.xml from defaults for platform "android"
So, I have to undo the change every time which is very annoying. Is there a way to tell cordova not to do that or insert the class mapping somewhere else?
When adding plugins or platforms, use the --save flag to add them to config. xml. Ex: cordova platform add android --save. Existing projects can use cordova plugin save and cordova platform save commands to save all previously installed plugins and platforms into your project's config.
The plugin. xml file is an XML document in the plugins namespace: http://apache.org/cordova/ns/plugins/1.0 .
The Cordova config. xml file is the global configuration file of the application. The Cordova configuration file is a mandatory XML file that contains application metadata, and is stored in the root directory of the app.
You will want to create a plugin package and install it to fix this issue.
A plugin package has a plugin.xml file, a JS file and your native code stored in this structure:
PLUGIN_NAME\
src\
PLATFORM_NAME\
PLAFORM_SPECIFIC_FILES
www\
PLUGIN_JS_FILE
plugin.xml
EXAMPLE:
BackgroundAPI\
plugin.xml
src\
android\
BackgroundAPI.java
ios\
BackgroundAPI.h
BackgroundAPI.m
www\
BackgroundAPI.js
In your plugin.xml you define the items that will be placed into config.xml when the plugin is installed. This make sure your code is not removed each time run the build or prepare command.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="com.dawsonloudon.backgroundapi"
version="1.0.0">
<name>backgroundapi</name>
<description>run external api calls on a background thread</description>
<license>MIT</license>
<js-module src="www/BackgroundAPI.js" name="BackgroundAPI">
<clobbers target="BackgroundAPI" />
</js-module>
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="BackgroundAPI">
<param name="ios-package" value="BackgroundAPI" />
</feature>
</config-file>
<header-file src="src/ios/BackgroundAPI.h" />
<source-file src="src/ios/BackgroundAPI.m" />
</platform>
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="BackgroundAPI" >
<param name="android-package" value="com.dawsonloudon.backgroundapi.BackgroundAPI"/>
</feature>
</config-file>
<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.INTERNET" />
</config-file>
<source-file src="src/android/BackgroundAPI.java" target-dir="src/com/dawsonloudon/backgroundapi" />
</platform>
</plugin>
Once you have this all built out in a separate directory from your project, go to your project directory and run:
cordova plugin add /PATH/TO/YOUR/PLUGIN
Now that the plugin is installed, you will have a plugins folder in your project and all of your config.xml edits will always be present.
To edit your plugin, you make changes to the JS file in /plugins/PLUGIN_NAME/www
To edit the native code, browse to /platforms/PLATFORM/ and find your native code.
Everytime you build or prepare, your JS file will be rewritten to the platform specific paths from the plugins directory, but your native code is only written to the /platforms/ paths once when the plugin is installed.
I advise that once your plugin is complete, you should collect all of your plugin specific files and copy them back to your plugin package directory so you can use them again in future projects (or post them for open sourcing for others to use).
I am using Cordova 3.1
I think I figured out the following behavior in respect to the many config.xml files - does not seem to be what the Cordova documentation says, maybe that changed at 3.1 and the documentation was not updated? All seems to be working correctly right now in my project.
I have 3 config.xml files:
1) myproject/www/config.xml
2) myproject/platforms/android/res/xml/config.xml
3) myproject/platforms/android/assets/www/config.xml
1) and 3) have same content and include: name, description, author, content, access and several preference tags. !No feature tags for the plugins!
I only edited the file 1); and file 3) was then generated from that information using "cordova build android" - as I understand it is supposed to be. I added all additional preference tags that I wanted to configure to 1) - I basically added all that are documented:
<preference name="fullscreen" value="false" />
<preference name="webviewbounce" value="false" />
<preference name="useBrowserHistory" value="true" />
<preference name="exit-on-suspend" value="false" />
<preference name="permissions" value="none" />
<preference name="target-device" value="universal" />
<preference name="prerendered-icon" value="true" />
<preference name="stay-in-webview" value="false" />
<preference name="detect-data-types" value="true" />
<preference name="show-splash-screen-spinner" value="true" />
<preference name="auto-hide-splash-screen" value="true" />
<preference name="disable-cursor" value="false" />
<preference name="android-minSdkVersion" value="10" />
<preference name="android-installLocation" value="auto" />
<preference name="orientation" value="portrait"/>
Then there is file 2); which is quite strange!
the file includes all the plugin "feature" tags and is the only config.xml file in my app that has these feature tags. They were properly added by my "cordova plugin add" commands for all my plugins.
after I edited/added my preference tags to file 1) these tags are also copied into this file 2) every time I execute my "cordova build android".
"cordova build android" still prints to the concole "Generating config.xml from defaults for platform "android" " ... but it includes all my preferences once I have them in 1) - I think once the file 1) has more content than the "default" content it successfully copies that content to file 2)
file 2) includes the preferences, the feature tags and the correct author from file 1); BUT it does not have the correct Description and not the correct Name tags from file 1). These two tags seem to come from some cordova internal default and if you change them directly in file 2) they get overwritten by the next "cordova build android" to these defaults again. I could not find a way to make them keep my own values! - I suspect a bug there!
I wanted to have a different start page for my cordova app, and faced the same problem - my config.xml file was getting overwritten by the build command. The way to solve this is to change the common config.xml file, which is currently in the project root folder. Once I changed there, the changes were picked up in the build command. I used this page as a reference.
This is a pretty old thread - hope this helps someone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With