Cordova adds unwanted permission to AndroidManifest.xml when building from CLI

浪子不回头ぞ 提交于 2019-12-01 17:58:32

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

   <platform name="android">
        <config-file target="res/xml/config.xml" parent="/*">
            <feature name="Media" >
                <param name="android-package" value="org.apache.cordova.media.AudioHandler"/>
            </feature>
        </config-file>

        <config-file target="AndroidManifest.xml" parent="/*">
            <uses-permission android:name="android.permission.RECORD_AUDIO" />
            <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
            <uses-permission android: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.

leuk98743

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.

Steve-e-b

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)
        return console.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)
            return console.log( err );
    } );
} );

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.

after unsuccessfully trying several suggestions ;

went to platforms\android\cordova and ran

>clean

went back to project directory and (on windows) ran

>findstr /s /M RECORD_AUDIO *.* > results.txt

opened results.txt to see files with the permission in them

removed mention of permission from all files listed except the AudioHandler.java files.

Did a build and it worked. Finally.

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 python
import 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')

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

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

with open(android_manifest, 'w') as fw:
    fw.writelines(new_lines)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!