Cordova Android Plugin: config.xml overwritten by cordova prepare?

android, cordova

Solution

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).

Problem

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?

Original source