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

前端 未结 6 938
Happy的楠姐
Happy的楠姐 2021-01-18 07:13

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.

6条回答
  •  温柔的废话
    2021-01-18 08:06

    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/<plugin-name>/plugin.xml files for <uses-permission> 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

提交回复
热议问题