20

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.

Venkat Kotra
  • 10,413
  • 3
  • 49
  • 53
poordeveloper
  • 2,272
  • 1
  • 23
  • 36
  • same to: http://stackoverflow.com/questions/27550060/add-androidname-something-to-androidmanifest-xml-application-tag-from-cordo – poordeveloper May 29 '15 at 15:55

11 Answers11

22

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

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

cordova-android < 7

The configuration edit that has worked for me was:

<platform name="android">
    <edit-config file="AndroidManifest.xml" target="/manifest/application" mode="merge">
        <application android:allowBackup="false"/>
    </edit-config>
</platform>
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
18

The configuration edit that has worked for me was:

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

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

11

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.

<platform name="android">
    <edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
        <application android:allowBackup="false" />
    </edit-config>
</platform>
Venkat Kotra
  • 10,413
  • 3
  • 49
  • 53
6

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') {
    throw new Error(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');
};
Arun Gopalpuri
  • 2,340
  • 26
  • 27
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-config file="AndroidManifest.xml" target="/manifest/application" mode="merge">
    <application android:allowBackup="true" />
</edit-config>

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

markdon
  • 784
  • 8
  • 19
4

Here is a simpler example, but it requires the cordova custom config plugin.

<platform name="android">
  <preference name="android-manifest/application/@android:allowBackup" value="true" />
</platform>`
Bala Clark
  • 1,524
  • 12
  • 19
3

First make sure that your looks something like this: (to import android)

<widget id="com.myapp" version="0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android">

Then in your android section

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

Note: This edit config section can be used to make many changes to your AndroidManifest.xml

And in case anyone was wondering, something similar for iOS can be achieved by adding this under the section:

<preference name="BackupWebStorage" value="none" />
Eli
  • 81
  • 4
2

In my case I used the plugin cordova-custom-config

cordova plugin add cordova-custom-config

and the problem was solved.

In the config.xml to aggregate

<platform name="android">
...
    <custom-preference name="android-manifest/application/@android:allowBackup" value="false" />
...
</platform>

view more detail of plugin in a example in cordova-custom-config-example

and documentation of andoid in guide

Leonardo Pineda
  • 990
  • 8
  • 10
1

None of the answers worked for me for latest cordova version 8.1.2. Below is how I did it and working perfectly fine. In similar way you can also update any other configuration of AndroidManifest.xml.

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

You are done!

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
1

Using edit-config breaks plugins so use it with care and test all plugins afterwards.

More info: https://issues.apache.org/jira/browse/CB-13514

What works without breaking plugins is by using the following (REQUIRES cordova-custom-config)

<platform name="android">
    <custom-preference name="android-manifest/application/@android:allowBackup" value="false" />
spa900
  • 927
  • 9
  • 19
0

Android

edit "platforms/android/CordovaLib/AndroidManifest.xml" like

 <manifest  xmlns:android="http://schemas.android.com/apk/res/android"  ...>
   // ... exiting conf directives ....

     <application android:allowBackup="false"/>
 </manifest>

or edit "platforms/android/app/src/main/AndroidManifest.xml"

<application   // exiting attributes 
     android:allowBackup="false"
 > </application>
bortunac
  • 4,642
  • 1
  • 32
  • 21