In this post we will take a look at how you can edit the AndroidManifest file the correct way for your Ionic / Cordova apps while building for Android.
There are times when you need to edit the platform specific files in your project. This is perfectly normal. In fact, there are some plugins that you can only use if you edit the platform specific files, for example, the diagnostic plugin.
The problem with directly editing platform specific files is that these files are dynamically generated and often times need to be regenerated. Which means that you will have to keep track of all the edits that you did. This is a nightmare when it comes to maintaining your app in a team environment, also it is hard to maintain such apps over time.
For example, when editing the AndroidManifest.xml file, which is generated when we add android platform using the
ionic cordova platform add android command. Now, if you remove the platform or update the platform to a newer version then the file will be created again and all the changes that you did will be lost.
As most of our configuration is in the config.xml file, it is natural to have other platform-specific configuration here as well.
In this example, we will take a look at how you can add a permissions tag to the AndroidManifest.xml file. You can edit other platform specific file as well. Also you can edit iOS files too. But I will be covering just Android and the AndroidManifest file in this post.
We will create a new project. You can work with the existing one. But, for the sake of being through I will create a new project running the following command.
ionic start configEdit
For Cordova, you can use the following command.
cordova create configEdit
Now we need to open up our config.xml file and make the following changes to the
Here we have added the namespace for android so that we do not get an error.
Also, inside the config file, we will add the following code.
<config-file parent="/manifest" target="AndroidManifest.xml">
<uses-permission android:name="android.permission.CAMERA" />
As you can see that we are using the
config-file tag to add the
user-permission tag to request for camera access. The above code will add the
user-permission tag to the AndroidManifest.xml as we have
target="AndroidManifest.xml" attribute. Also, we have
parent="/manifest" attribute will place our
user-permission inside the
<manifest> tag of AndroidManifest.xml .
Now it’s time to check the code in action. The config file setting are applied during the prepare process. So you can just run
cordova prepare to see that the
user-permission tag has been added to the specified target.
Alternatively, you can run the build command as well which will execute the
prepare command internally.
As you saw in this post, we can easily add tags to the AndroidManifest.xml file. You can do that for most platform files. There are times when this method will not be adequate for your needs, for that you can use the cordova-custom-config plugin. Check out the docs here to know more about when to use the plugin.