我结束了一个
plugin hook,我不知道存在.插件钩子是在插件中定义的钩子,可以在添加或删除插件之前或之后调用插件(当您运行cordova插件cli命令时,或当cordova使用cordova platform add命令将插件添加到平台时).
我不想使用钩子,因为我认为钩子必须放在config.xml中,不能与插件链接.
在这里我把这行添加到plugin.xml文件的平台android部分(我的要求与OP有一点不同,但是示例可能有帮助):
...
然后我写了androidBeforeInstall.js钩子脚本:
module.exports = function(ctx) {
var fs = ctx.requireCordovaModule('fs'),
path = ctx.requireCordovaModule('path'),
xml = ctx.requireCordovaModule('cordova-common').xmlHelpers;
var manifestPath = path.join(ctx.opts.projectRoot, 'platforms/android/AndroidManifest.xml');
var doc = xml.parseElementtreeSync(manifestPath);
if (doc.getroot().tag !== 'manifest') {
throw new Error(manifestPath + ' has incorrect root node name (expected "manifest")');
}
//adds the tools namespace to the root node
doc.getroot().attrib['xmlns:tools'] = 'http://schemas.android.com/tools';
//add tools:replace in the application node
doc.getroot().find('./application').attrib['tools:replace'] = 'android:label';
//write the manifest file
fs.writeFileSync(manifestPath, doc.write({indent: 4}), 'utf-8');
};
这比在plugin.xml中添加配置文件行要复杂一点,但一旦你拥有了很好的语法,它就可以非常强大.
编辑:
由于某些原因,只能在before_plugin_install中挂钩,所以AndroidManifest.xml在平台添加过程中被正确更新,但是在平台添加的末尾被恢复为默认状态.
由于我无法找出原因,我在plugin.xml中添加了以下一行,因此脚本也在平台末尾启动(在plugin.xml中定义的luckilly hooks不仅可以在添加或删除时运行一个插件).