19

I decided to open new question because none of those that are already posted, has a good answer.

I need to update AndroidManifest.xml "from plugin.xml", so that the <application> tag has the following property, alongside those it already has:

android:name="mypackage"

How can do that?

Thank you

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
user2548436
  • 915
  • 2
  • 16
  • 35
  • i'm not really understand what you want – Nurdin Dec 19 '14 at 02:31
  • From the plugin.xml file of cordova plugin, you can set activity, intent etc etc that were written in the AndroidManifest.xml when you build the app. I'd need to write also an additional attribute in tag of AndroidManifest so that the tag became something like I can do this manually and it works but it's possibile an automated way through plugin.xml file? – user2548436 Dec 19 '14 at 17:05
  • @user2548436 So have you found a solution later? – Tong Shen May 22 '15 at 18:13
  • 1
    I am also looking for this solution. If there is not, we may have to use element to tell users add it manually. – poordeveloper May 29 '15 at 16:04
  • 2
    looking for this solution too – SooCheng Koh Jun 07 '15 at 15:43

4 Answers4

14

I had the same issue, and I used a Cordova hook to do the work.

First, edit your config.xml file to add the hook:

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

Create a file called scripts/android_app_name.js (set it executable), and inside, just use a search/replace function. It should look like:

#!/usr/bin/env node

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 appClass = 'YOU_APP_CLASS';

      if (data.indexOf(appClass) == -1) {

        var result = data.replace(/<application/g, '<application android:name="' + appClass + '"');

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


};
Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
ndeverge
  • 21,378
  • 4
  • 56
  • 85
  • 1
    Worked, except that i had to change ```after_prepare``` to ```after_plugin_add```. Thank you! – Patrick Boos Nov 04 '15 at 12:27
  • Nice! I am also trying to remove 'android:smallScreens="true" ' from the tag, but I can't get right the data.replace – Juangui Jordán Mar 07 '16 at 02:55
  • I commend the use of the after_prepare hook to make changes in the AndroidManifest if you need to reference elements/attributes with a namespace (eg, android:xxx) - see comment against marcRDZ answer. It is preferable to use some form of json editing as suggested by curtis here: [http://www.curtismlarson.com/blog/2018/10/03/edit-xml-node-js/] rather than pattern replacement. Since Cordova will run a prepare prior to each build, the pattern replacement approach has to harden against that, which is much easier if you can simply check the xml structure using json first. – simon coleman Oct 16 '19 at 14:32
5

The simplest and the latest(cordova version 8.1.2) way to do it to use edit-config tag like below:

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

In similar way you can edit other configurations as well.

Hope it will be helpful!

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
  • I have seen issues with using `edit-config` where the manifest is altered in unexpected ways. Others have as well: https://issues.apache.org/jira/browse/CB-13514 and https://issues.apache.org/jira/browse/CB-13474 – BRass Nov 05 '20 at 17:37
4

Indeed, as jlreymendez mentioned, the right way is this:

    <edit-config file="AndroidManifest.xml" target="/manifest/application" mode="merge">
      <application android:name="com.mypackage.MyApplication"/>
    </edit-config>

Also note that modifications will revert if you remove the plugin, what will not happen with the hook trick.

marcRDZ
  • 281
  • 2
  • 8
  • This solution is the best one unless you have conflicts. I got to the point where I could only add my plugin with `--force`, removing some stuff from other plugins in the process. – Slav Jun 06 '17 at 15:16
  • This does not work if you need to modify any of the android:xxxx tags. Although edit-config *is* able to modify the AndroidManifest.xml file, the change is also propogated to one of the generated intermediate config.xml files. This config file is reloaded later in the build process and the build will abort due to the lack of the android namespace against the config.xml root node. I have not found a method of injecting the namespace into the intermediate config.xml file. I would recommend using an after_prepare build hook instead. – simon coleman Oct 16 '19 at 14:26
  • I have seen issues with using edit-config where the manifest is altered in unexpected ways. Others have as well: issues.apache.org/jira/browse/CB-13514 and issues.apache.org/jira/browse/CB-13474 – BRass Nov 05 '20 at 17:38
1

I think I had the same problem as you. I found this in the cordova documentation.

https://cordova.apache.org/docs/en/4.0.0/plugin_ref_spec.md.html

If you search the title "config-file Element" you will find an example:

<config-file target="AndroidManifest.xml" parent="/manifest/application">
    <activity android:name="com.foo.Foo" android:label="@string/app_name">
        <intent-filter>
        </intent-filter>
    </activity>
</config-file>
jlreymendez
  • 107
  • 4
  • The `config-file` tag is used only to add things into a file. Alltough it makes sense, he wants to edit what already exists, which is the `application` tag on `AndroidManifest.xml`. Maybe the tag `edit-config` could be useful, but in my case I couldn't make it work, because I don't want to change the children of the `application` tag. – Renan Bandeira Dec 28 '16 at 12:42