从cordova插件中的AndroidManifest中的"application"标签添加属性

Shi*_*rra 15 plugins android android-manifest cordova cordova-plugins

我为Cordova创建了一个插件,我想编辑AndroidManifest以在"application"标签中添加一个属性.我知道配置文件添加新标签,但我没有找到是否可以更新现有标签.

例如,我有这个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>
Run Code Online (Sandbox Code Playgroud)

我想添加android:isGame=true<application>标签.

如果我不能从中做到这一点plugin.xml,我将创建一个钩子来AndroidManifest自己编辑,但我希望它没有必要.

Qui*_*Fix 6

我结束了做一个我不知道存在的插件钩子.插件挂钩是插件中定义的挂钩,可以在添加或删除插件之前或之后调用(当您运行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>
Run Code Online (Sandbox Code Playgroud)

然后我写了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');

};
Run Code Online (Sandbox Code Playgroud)

它比在plugin.xml中添加配置文件行要复杂一些,但是一旦你有了良好的语法,它就会变得非常强大.

编辑:

由于某些原因,只有在before_plugin_install中的钩子,AndroidManifest.xml在平台添加期间被正确更新,但在platdorm add结束时恢复了它的默认状态.

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

<hook type="after_platform_add" src="scripts/androidBeforeInstall.js" />
Run Code Online (Sandbox Code Playgroud)