plugin.xml Wiring

plugin.xml and How the CLI Wires a Plugin In

A plugin is an npm 2,036 package whose plugin.xml tells the CLI what goes where on each platform. The Android half of cordova-plugin-device 3.0.0 is short:

The JavaScript module and the Android section of cordova-plugin-device's plugin.xmlXML
<js-module src="www/device.js" name="device">
    <clobbers target="device" />
</js-module>
<platform name="android">
    <config-file target="res/xml/config.xml" parent="/*">
        <feature name="Device" >
            <param name="android-package" value="org.apache.cordova.device.Device"/>
        </feature>
    </config-file>
    <source-file src="src/android/Device.java" target-dir="src/org/apache/cordova/device" />
</platform>

cordova plugin add cordova-plugin-device copies the package into plugins/, records it in package.json and applies each element to platforms/android. <source-file> copies the Java class into the source tree; <config-file> merges a <feature> mapping the service name Device to that class into the platform's config.xml; <js-module> copies device.js into the web assets and lists it in a generated cordova_plugins.js, which cordova.js reads at startup before <clobbers> assigns it to window.device:

What cordova plugin add changed in the Android project
diff config.before.xml platforms/android/app/src/main/res/xml/config.xml
find platforms/android/app/src/main -path '*device*' -name '*.*'
Output
13a14,16
>     <feature name="Device">
>         <param name="android-package" value="org.apache.cordova.device.Device" />
>     </feature>
platforms/android/app/src/main/java/org/apache/cordova/device/Device.java
platforms/android/app/src/main/assets/www/plugins/cordova-plugin-device/www/device.js

Other elements work the same way: <edit-config> patches AndroidManifest.xml (edit-config), <framework> adds a Gradle 19,597 dependency, and <preference> asks for a variable such as an API key at install time.