android添加标签属性,在cordman插件的AndroidManifest中的“application”标签中添加一个属性...

我结束了一个

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中挂钩,所以A​​ndroidManifest.xml在平台添加过程中被正确更新,但是在平台添加的末尾被恢复为默认状态.

由于我无法找出原因,我在plugin.xml中添加了以下一行,因此脚本也在平台末尾启动(在plugin.xml中定义的luckilly hooks不仅可以在添加或删除时运行一个插件).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值