【问题标题】:how to add xmlns (XML namespaces) to android manifest header如何将 xmlns(XML 命名空间)添加到 android 清单标头
【发布时间】:2020-08-14 05:06:35
【问题描述】:

我用的是离子框架,我用过

        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
            <application android:allowBackup="false" tools:replace="android:allowBackup" />
            <application android:fullBackupOnly="false" />
        </edit-config>

避免安卓应用中的备份服务。

我在config.xml 中添加了它,它会自动添加到清单中,但需要在清单中手动添加xmlns: tools="http://schemas.android.com/tools",否则构建失败并引发错误:

The prefix "tools" for attribute "tools: replace" associated with an element type "application" is not bound.

有什么方法可以将 xmlns 从配置文件自动添加到 android 清单中?

【问题讨论】:

    标签: android cordova ionic-framework android-manifest xml-namespaces


    【解决方案1】:

    您可以使用挂钩脚本来添加命名空间。例如,在您的 Cordova 项目中创建 hooks/add_tools_namespace.js:

    #!/usr/bin/env node
    
    const fs = require('fs'),
        path = require('path');
    
    module.exports = function (context) {
        const toolsAttribute = "xmlns:tools=\"http://schemas.android.com/tools\"";
        const manifestOpen = "<manifest";
    
        const manifestPath = path.join(context.opts.projectRoot, 'platforms/android/app/src/main/AndroidManifest.xml');
        let manifest = fs.readFileSync(manifestPath).toString();
    
        if(manifest.indexOf(toolsAttribute) == -1) {
            manifest = manifest.replace(manifestOpen, manifestOpen + " " + toolsAttribute + " ");
            fs.writeFileSync(manifestPath, manifest, 'utf8');
        }
    };
    

    然后从您的config.xml 引用它:

    <platform name="android">
        ...
        <hook type="after_prepare" src="hooks/add_tools_namespace.js" />
    </platform> 
    

    【讨论】:

      猜你喜欢
      • 2011-11-27
      • 1970-01-01
      • 2011-09-01
      • 2018-08-25
      • 2011-08-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2017-09-21
      相关资源
      最近更新 更多