how to add xmlns (XML namespaces) to android manifest header

Viewed 589

I am using ionic framework, I used

        <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>

to avoid backup service in android app.

I added this in config.xml and it gets added automatically to the manifest but xmlns: tools="http://schemas.android.com/tools" is need to be added manually in the manifest otherwise builds fails and throws an error :

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

Is there any way that I can add xmlns automatically from the config file to android manifest?

1 Answers

You could use a hook script to add the namespace. For example, in your Cordova project create 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');
    }
};

Then reference it from your config.xml:

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