Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

edit-config removes tags in my AndroidManifest.xml

I added an edit-config directive to my project's config.xml, and now every time I run 'cordova run android', another line is removed from the file being edited.

Here's my edit-config:

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

How can I get this to work without removing a tag every time I call 'cordova run android'?

I suspect this is a bug, but I haven't found other instances of it - I'd be happy to report it if I had a better understanding of how to create it.

like image 742
Crag Avatar asked Jul 14 '18 13:07

Crag


People also ask

How do I change AndroidManifest XML?

Open your Android project and go to <AppProjectFolder>/app/src/main/. Open the app manifest file AndroidManifest. xml and add the following lines to the file as child elements of the <manifest> element. Note: You must include the QUERY_ALL_PACKAGES permission if your app targets Android 11 (API Level 30) or higher.

What is Android name in AndroidManifest XML?

android:name is "An optional name of a class implementing the overall android. app. Application for this package. [string]".

Where is the manifest file in Android Studio?

The file is located at WorkspaceName>/temp/<AppName>/build/luaandroid/dist. The manifest file provides essential information about your app to the Android operating system, and Google Play store. The Android manifest file helps to declare the permissions that an app must have to access data from other apps.


1 Answers

I've encountered similar problems when trying to use <edit-config> to change attributes on the <application> element. It seems Cordova overwrites the changes made by <edit-config> when it applies it's own default change to the <application> element, i.e. to set android:label, etc.

My workaround has been to use an after_prepare hook script to apply the changes after Cordova has finished making its changes. In your case, you'd want something like this:

config.xml

<platform name="android">
  ...
  <hook type="after_prepare" src="scripts/set_allowBackup.js" />
  ...
</platform>

scripts/set_allowBackup.js:

#!/usr/bin/env node

var TARGET_ATTRIBUTE = 'android:allowBackup';
var TARGET_VALUE = TARGET_ATTRIBUTE+'="false"';
var TARGET_REGEX = new RegExp(TARGET_ATTRIBUTE+'="([^"]+)"');

module.exports = function(context) {

  var fs = context.requireCordovaModule('fs'),
      path = context.requireCordovaModule('path');

  var platformRoot = path.join(context.opts.projectRoot, 'platforms/android');
  var manifestFile = path.join(platformRoot, 'AndroidManifest.xml');

  if (fs.existsSync(manifestFile)) {
    fs.readFile(manifestFile, 'utf8', function (err, data) {
      if (err) {
        throw new Error('Unable to find AndroidManifest.xml: ' + err);
      }

      var result;
      if(!data.match(TARGET_ATTRIBUTE)) {
        result = data.replace(/<application/g, '<application ' + TARGET_VALUE);
      }else if (data.match(TARGET_REGEX) && !data.match(TARGET_VALUE)){
        result = data.replace(TARGET_REGEX, TARGET_VALUE);
      }

      if(result){
        fs.writeFile(manifestFile, result, 'utf8', function (err) {
          if (err) throw new Error('Unable to write AndroidManifest.xml: ' + err);
        })
      }
    });
  }
};

Another alternative is to use cordova-custom-config which applies changes after_prepare by default:

Add the plugin:

cordova plugin add cordova-custom-config

Set the custom preference in config.xml:

<platform name="android">
  ...
  <custom-preference name="android-manifest/application/@android:allowBackup" value="false" />
  ...
</platform>
like image 73
DaveAlden Avatar answered Sep 30 '22 13:09

DaveAlden