Skip to content Skip to sidebar Skip to footer

How To Add Android:allowbackup="false" Via Cordova Plugin

I am now developing a Cordova Plugin, I wanna add android:allowBackup='true' into AndroidManifest.xml, but I do not know how to specify it in plugin.xml.

Solution 1:

Answer shared by @Muhammad Omar works for cordova-android < 7. But things changed for cordova-android >= 7

https://cordova.apache.org/announcements/2017/12/04/cordova-android-7.0.0.html

So you need to change it a bit for

cordova-android >= 7

<platformname="android"><edit-configfile="app/src/main/AndroidManifest.xml"mode="merge"target="/manifest/application"><applicationandroid:allowBackup="false"/></edit-config></platform>

cordova-android < 7

The configuration edit that has worked for me was:

<platformname="android"><edit-configfile="AndroidManifest.xml"target="/manifest/application"mode="merge"><applicationandroid:allowBackup="false"/></edit-config></platform>

Solution 2:

The configuration edit that has worked for me was:

<platformname="android"><edit-configfile="AndroidManifest.xml"target="/manifest/application"mode="merge"><applicationandroid:allowBackup="false"/></edit-config></platform>

EDIT Feb-2020: Please refer to answer from @Shashank Agrawal below for cordova-android >= 7

Solution 3:

To avoid android app from restoring backup on install, following config added to config.xml.

Ensure xml namespace is defined. Cordova build failed without this for me.

Solved by adding below.

<platformname="android"><edit-configfile="AndroidManifest.xml"mode="merge"target="/manifest/application"xmlns:android="http://schemas.android.com/apk/res/android"><applicationandroid:allowBackup="false" /></edit-config></platform>

Solution 4:

You have to do it via hooks (below is an example for Ionic app):

In your config.xml add a hook as:

<platform name="android"> <hook type="before_plugin_install" src="hooks/androidBeforeInstall.js" />

In hooks folder create the js file androidBeforeInstall.js and add this below code:

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') {
    thrownewError(manifestPath + ' has incorrect root node name (expected "manifest")');
 }

 doc.getroot().find('./application').attrib['android:allowBackup'] = "true";

 //write the manifest file
 fs.writeFileSync(manifestPath, doc.write({
    indent: 4
 }), 'utf-8');
};

Solution 5:

If you're writing a plugin that needs to add/edit something in the app's AndroidManifest.xml, there is functionality built into plugin.xml to do this. It should be something like this for the question example:

<edit-configfile="AndroidManifest.xml"target="/manifest/application"mode="merge"><applicationandroid:allowBackup="true" /></edit-config>

Check out the docs for config-file and edit-config

Post a Comment for "How To Add Android:allowbackup="false" Via Cordova Plugin"