How to add android:allowBackup="false" via cordova plugin
Asked Answered
K

11

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.

Kunstlied answered 29/5, 2015 at 10:20 Comment(1)
same to: #27550560Kunstlied
D
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>
Disclamation answered 5/3, 2019 at 7:42 Comment(1)
Faced an issue due to the higher cordova version, solved with the first way :)Zebu
L
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

Loom answered 19/4, 2018 at 19:55 Comment(1)
Worked well in config.xml too! (cordova-android 6.4; cordova 7.1)Coacher
W
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>
Wolpert answered 15/3, 2019 at 1:21 Comment(0)
S
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');
};
Sorehead answered 10/2, 2016 at 20:25 Comment(0)
M
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

Martica answered 16/6, 2017 at 3:4 Comment(0)
G
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>`
Glorious answered 31/1, 2017 at 12:47 Comment(1)
Tried but didnt work for me. What did work was: <platform name="android"> <custom-preference name="android-manifest/application/@android:allowBackup" value="false" />Tatting
P
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" />
Pollster answered 19/3, 2019 at 15:38 Comment(0)
S
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

Sedgemoor answered 9/5, 2019 at 13:58 Comment(0)
S
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!

Sausage answered 5/12, 2018 at 7:33 Comment(0)
T
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" />
Tatting answered 4/4, 2019 at 13:25 Comment(0)
H
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>
Histiocyte answered 4/4, 2021 at 7:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.