Customizing Android Manifest in Cordova

November 17th 2017 Cordova Android

Cordova has well documented support for modifying the contents of AndroidManifest.xml file from a plugin by adding config-file and edit-config elements to its plugin.xml file. It's less obvious that these same configuration elements can also be used directly inside the application's config.xml file.

Most of the time, modifying the AndroidManifest.xml from a plugin will be all that you need. It contains only native specific settings, which typically originate from Cordova plugins. However, sometimes a plugin might not expose a setting for the native functionality, which you want to modify.

In my case it was Firebase configuration setting, which I wanted to modify, but couldn't do it through the plugin. I wanted to disable Firebase crash reporting, as I was already using Google Analytics for the same purpose. This can be easily achieved by adding a meta-data entry to AndroidManifest.xml, but since the plugin does not provide this functionality, I had the following options at my disposal:

  • Add the functionality to the plugin and submit it as a pull request, hoping that it will be accepted.
  • Create a custom plugin with the only purpose of adding the required entry to the manifest file.
  • Modify the manifest file with a hook.

During research, I stumbled across a remark that Cordova was now supposed to support config-file and edit-config in config.xml as well. Although I couldn't find it mentioned anywhere in the docs, I decided to try it anyway. I added the following inside <platform name="android"> element of my config.xml file:

<config-file parent="/manifest/application" target="AndroidManifest.xml">
  <meta-data android:name="firebase_crash_collection_enabled" android:value="false" />
</config-file>

After I built the project, the following line was added inside the application element of my AndroidManifest.xml file:

<meta-data android:name="firebase_crash_collection_enabled" android:value="false" />

Success! Of course, it was much easier to do than any of the alternatives I was considering. I'm not sure why it isn't documented. Hopefully it's not going away in one of the future versions. It works great in Cordova 7.1.0.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Copyright
Creative Commons License