How to add android:allowBackup=“false” via cordova plugin

旧街凉风 提交于 2020-05-25 10:44:11

问题


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.


回答1:


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




回答2:


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>



回答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.

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



回答4:


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




回答5:


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');
};



回答6:


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



回答7:


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



回答8:


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!




回答9:


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



回答10:


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



来源:https://stackoverflow.com/questions/30527107/how-to-add-androidallowbackup-false-via-cordova-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!