我为Cordova创建了一个插件,我想编辑AndroidManifest以在“ application”标签中添加属性。我知道config-file添加新标签,但是我找不到是否可以更新现有标签。
例如,我有这个AndroidManifest:
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.test.testCordova" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="CordovaApp" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="@string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
</manifest>
并且我想在android:isGame=true
标签中添加<application>
。
如果无法通过plugin.xml
执行此操作,我将创建一个钩子来单独编辑AndroidManifest
,但我希望它没有必要。
我结束了一个我不知道存在的plugin hook。插件挂钩是在插件中定义的挂钩,可以在添加或删除插件之前或之后调用(在您运行cordova插件cli命令时或当cordova使用cordova platform add命令将插件添加到平台时)。
我不想使用钩子,因为我认为钩子必须放在config.xml中并且不能与插件链接。
在这里,我在plugin.xml文件的android平台部分添加了这一行(我的要求与OP的要求有所不同,但该示例仍然可以提供帮助:]
<platform name="android">
<hook type="before_plugin_install" src="scripts/androidBeforeInstall.js" />
...
</platform>
然后我写了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中添加了以下行,因此该脚本也在平台末尾启动。add(plugin.xml中定义的幸运钩子不仅可以在添加时运行,或删除插件)。
<hook type="after_platform_add" src="scripts/androidBeforeInstall.js" />
对于cordova version 8.1.2
文件的最新AndroidManifest.xml
路径已更改
如果使用cordova-custom-config
,则可能需要在cordova-custom-config
中添加新的custom-preference
: