前回は、Cordovaプラグインの仕組みを理解するために、Plugmanで作成できるCordovaプラグインの雛形を使用して、Cordovaプラグインの構成について説明しました。今回は、前回使用したCordovaプラグインを使用して、Cordovaプラグインの設定について説明していきたいと思います。今回は、plugin.xmlの設定内容について説明します。
/SamplePlugin/plugin.xmlの設定について
Cordovaプラグインの設定を行う場合は、plugin.xmlを使用します。前回作成したCordovaプラグインのplugin.xmlの内容は、以下になります。
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-plugin-sample" version="1.0.0"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<name>SamplePlugin</name>
<js-module name="SamplePlugin" src="www/SamplePlugin.js">
<clobbers target="cordova.plugins.SamplePlugin" />
</js-module>
<platform name="android">
<config-file parent="/*" target="res/xml/config.xml">
<feature name="SamplePlugin">
<param name="android-package" value="cordova-plugin-sample.SamplePlugin" />
</feature>
</config-file>
<config-file parent="/*" target="AndroidManifest.xml" />
<source-file src="src/android/SamplePlugin.java" target-dir="src/cordova-plugin-sample/SamplePlugin" />
</platform>
<platform name="ios">
<config-file parent="/*" target="config.xml">
<feature name="SamplePlugin">
<param name="ios-package" value="SamplePlugin" />
</feature>
</config-file>
<source-file src="src/ios/SamplePlugin.m" />
</platform>
</plugin>
plugin.xmlには、以下のディレクティブが設定されています。
- /plugin
- /plugin/name
- /plugin/js-module
- /plugin/js-module/clobbers
- /plugin/platform
- /plugin/platform/config-file
- /plugin/platform/config-file/feature
- /plugin/platform/config-file/feature/param
- /plugin/platform/source-file
plugin.xmlで利用できるAPIについては、以下を参照してください。
/plugin
pluginディレクティブは、ルートに設定されるディレクティブになります。pluginディレクティブには、plugman createコマンドで指定した、
- --plugin_id cordova-plugin-sample
- --plugin_version 1.0.0
の値が、id属性とversion属性に設定されます。その他に、ネームスペースの設定が追加されています。
/plugin/name
nameディレクティブは、Cordovaプラグインの名前を設定するディレクティブになります。nameディレクティブには、plugman createコマンドで指定した、
- --name SamplePlugin
の値が設定されます。
/plugin/js-module
js-moduleディレクティブは、アプリから参照できるJavaScriptの設定を行うディレクティブになります。対象のJavaScriptは、plugman createコマンドで作成された、
- /SamplePlugin/www/SamplePlugin.js
になり、src属性にパスが設定されます。name属性には、plugman createコマンドで指定した、
- --name SamplePlugin
の値が設定されます。
/plugin/js-module/clobbers
clobbersディレクティブは、アプリからCordovaプラグインの機能を参照するためのディレクティブになります。target属性に設定した値でJavaScriptのオブジェクトが作成され、アプリ側のJavaScriptから参照できるようになります。アプリ側のJavaScriptからは、
- cordova.plugins.SamplePlugin.xxx()
のような形で、メソッドやプロパティーを参照する事ができます。
/plugin/platform
platformディレクティブは、対象のOSを指定するためのディレクティブになります。
- plugman platform add --platform_name
でandroidを指定した場合は、name属性にandroidが設定されます。iosを指定した場合は、name属性にiosが設定されます。
/plugin/platform/config-file
config-fileディレクティブは、指定した xmlファイルとplistファイルに対して、新しい子要素を追加するディレクティブになります。雛形の設定では、config.xmlに、以下の要素を追加します。
<feature name="SamplePlugin">
<param name="android-package" value="cordova-plugin-sample.SamplePlugin" />
</feature>
<feature name="SamplePlugin">
<param name="ios-package" value="SamplePlugin" />
</feature>
/plugin/platform/config-file/feature
featureディレクティブは、name属性で指定されたCordovaプラグインを有効にするためのディレクティブになります。
/plugin/platform/config-file/feature/param
paramディレクティブは、Cordovaプラグインのコードの取得元となるパッケージ名やWebviewの初期化中にCordovaプラグインのコードを初期化するかを指定するためのディレクティブになります。
name属性には、以下を指定する事ができます。
- android-package
- ios-package:
- onload
android-packageとios-packageは、Cordovaプラグインのコードの初期化に使用するパッケージの名前をvalue属性に設定します。onloadは、WebViewの初期化中にプラグインのコードを初期化する場合、value属性にCordovaプラグインの名前を指定します。
/plugin/platform/source-file
source-fileディレクティブは、Cordovaプラグインの実行ファイルを指定するためのディレクティブになります。src属性にplugin.xmlを基準とした対象ファイルのパスを指定します。
おわりに
今回は、雛形のCordovaプラグインに作成されたplugin.xmlの設定について説明しました。今回説明した内容は、現在公開されているサードパーティー製Cordovaプラグインにも設定されている内容になりますので、押さえてほしい内容になります。plugin.xmlには、今回説明した設定の他にもたくさんの設定がありますので、興味があれば、以下を参照してください。
次回は、雛形のCordovaプラグインで作成された、
- /SamplePlugin/www/SamplePlugin.js
の設定について説明していきたいと思います。