cordova - remove unnecessary permissions
Create a file in root directory of your project and rename it to remove_permissions.js then put the following code into it:
var permissionsToRemove = [ "RECORD_AUDIO", "MODIFY_AUDIO_SETTINGS"];
var fs = require('fs');
var path = require('path');
var rootdir = "";
var manifestFile = path.join(rootdir, "platforms/android/app/src/main/AndroidManifest.xml");
fs.readFile( manifestFile, "utf8", function( err, data )
{
if (err)
return console.log( err );
var result = data;
for (var i=0; i<permissionsToRemove.length; i++)
result = result.replace( "<uses-permission android:name=\"android.permission." + permissionsToRemove[i] + "\" />", "" );
fs.writeFile( manifestFile, result, "utf8", function( err )
{
if (err)
return console.log( err );
} );
} );
Open config.xml and add the bellow line in Android part:
<platform name="android">
...
<hook type="after_prepare" src="remove_permissions.js"/>
...
</platform>
Now rebuild APK file.
You have to open plugins/android.json
, locate in that file part which looks like
"AndroidManifest.xml": {
"parents": {
"/*": [
......
{
"xml": "<uses-permission android:name=\"android.permission.MODIFY_AUDIO_SETTINGS\" />",
"count": 1
},
......
{
"xml": "<uses-permission android:name=\"android.permission.RECORD_AUDIO\" />",
"count": 1
},
......
],
and remove these files. After removal of that lines, make sure that android.json
still be valid JSON file.
Also please notice the count
property which indicates how many plugins use the permissions. If you have value more then 1 you should find which other plugins could use that permission. Also I don't sure that Cordova plugins was tested to be workable without proper permissions, so you on your own when removing permissions from that file.
If you put your system in the broken state, you always could remove all content of platforms and plugins folders and recreate your project by running
cordova create ...
cordova platform add ...
cordova plugin add ...
The approach in @codevision's response used to work for me, but in more recent (I don't know exactly what cutoff) versions of the cordova-android platform, I found that permissions were still being merged into the final AndroidManifest.xml in the APK.
The cause of this was the Merge Multiple Manifest Files build step that caused permissions declared from various plugin dependencies (including .aar files) to sneak back into the final AndroidManifest.xml.
What worked for me was creating an after-prepare script that modified platforms/android/AndroidManifest.xml
as follows:
- Add the following namespace declaration to the top-level
<manifest>
tagxmlns:tools="http://schemas.android.com/tools"
- (Read carefully) Ensure that all the permissions that you don't want are present, but mark them with the attribute
tools:node="remove"
, e.g.:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" tools:node="remove">
This prevents any dependencies from sneaking their MODIFY_AUDIO_SETTINGS
permission into the final APK.