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
.
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
.
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
<platform name="android">
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
<application android:allowBackup="false"/>
</edit-config>
</platform>
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>
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
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>
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');
};
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>
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>`
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" />
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
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!
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" />
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>
© 2022 - 2024 — McMap. All rights reserved.