Skip to content Skip to sidebar Skip to footer

Cordova Adds Unwanted Permission To Androidmanifest.xml When Building From Cli

I use CLI to build my Cordova app, and I have added the Media plugin. 'cordova build' automatically adds the android.permission.RECORD_AUDIO to my AndroidManifest.xml even though I

Solution 1:

In your project, edit the file plugins/org.apache.cordova.media/plugin.xml You'll see the android specific configuration

<platformname="android"><config-filetarget="res/xml/config.xml"parent="/*"><featurename="Media" ><paramname="android-package"value="org.apache.cordova.media.AudioHandler"/></feature></config-file><config-filetarget="AndroidManifest.xml"parent="/*"><uses-permissionandroid:name="android.permission.RECORD_AUDIO" /><uses-permissionandroid:name="android.permission.MODIFY_AUDIO_SETTINGS" /><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permissionandroid:name="android.permission.READ_PHONE_STATE" /></config-file>
...

remove the line <uses-permission android:name="android.permission.RECORD_AUDIO" />like this the permission will not be added each time you build.

As the permission has already been added to AndroidManifest.xml, you'll have to remove it manually and then it should not come back next time you build.

Solution 2:

To keep plugins from re-adding unnecessary permissions edit platforms/android/android.json.

Locate these lines and remove them:

{
    "xml": "<uses-permission android:name=\"android.permission.RECORD_AUDIO\" />",
    "count": 1
}

Please note that this is a "dirty" solution. After adding/updating plugins, you'll probably have to repeat this.

Solution 3:

I have tried the suggestions above (from QuickFix and leuk98743) but the manifest file kept getting re-generated. So I created a hook to modify the manifest file during the build.

  1. Add the content below into a file in your project: hooks/after_prepare/030_remove_permissions.js
  2. If you're on Linux, make that file executable.
  3. Modify the file to set the permissions you want to remove. There are 3 listed in my example but you should add/remove as appropriate.
#!/usr/bin/env node//// This hook removes specific permissions from the AndroidManifest.xml// The AndroidManifest is re-generated during the prepare stage,// so this must be run on the "after_prepare" hook.//// Configure the permissions to be forcefully removed.// NOTE: These permissions will be removed regardless of how many plugins//       require the permission. You can check the permission is only required//       by the plugin you *think* needs it, by looking at the "count" shown in//       your /plugins/android.json file.//       If the count is more than 1, you should search through//       the /plugins/&lt;plugin-name&gt;/plugin.xml files for &lt;uses-permission&gt; tags.var permissionsToRemove = [ "RECORD_AUDIO", "MODIFY_AUDIO_SETTINGS", "READ_PHONE_STATE" ];


var fs = require('fs');
var path = require('path');
var rootdir = process.argv[2];
var manifestFile = path.join(rootdir, "platforms/android/AndroidManifest.xml");

fs.readFile( manifestFile, "utf8", function( err, data )
{
    if (err)
        returnconsole.log( err );

    var result = data;
    for (var i=0; i<permissionsToRemove.length; i++)
        result = result.replace( "&lt;uses-permission android:name=\"android.permission." + permissionsToRemove[i] + "\" /&gt;", "" );

    fs.writeFile( manifestFile, result, "utf8", function( err )
    {
        if (err)
            returnconsole.log( err );
    } );
} );

Solution 4:

I have done below:

  1. Removed permission entries from below 2 files:

myapp\platforms\android\app\src\main\AndroidManifest.xml myapp\platforms\android\android.json

  1. Rebuilt the apk in release mode.

It worked, removed permission entries dint come back in manifest file. Successfully uploaded apk to Play console.

Solution 5:

A clone of Steve-e-b's answer for those who are more comfortable with python. It assumes the file will be located under something like hooks/after_prepare/123_remove_permissions.py and be executable.

#!/usr/bin/env pythonimport os

script_dir = os.path.dirname(os.path.abspath(__file__))
project_dir = os.path.abspath(os.path.join(script_dir, '../..'))

bad_permissions = [
    'WRITE_EXTERNAL_STORAGE',
    'RECORD_AUDIO',
    'MODIFY_AUDIO_SETTINGS',
]

android_manifest = os.path.join(project_dir, 'platforms/android/app/src/main/AndroidManifest.xml')

withopen(android_manifest, 'r') as fr:
    lines = fr.readlines()

new_lines = [line for line in lines ifnot [perm for perm in bad_permissions if perm in line]]

withopen(android_manifest, 'w') as fw:
    fw.writelines(new_lines)

Post a Comment for "Cordova Adds Unwanted Permission To Androidmanifest.xml When Building From Cli"