cordova - remove unnecessary permissions

痞子三分冷 提交于 2019-12-10 02:38:50

问题


I need to play sounds in my game, so I added org.apache.cordova.media plugin to my application. Now platforms/android/AndroidManifest.xml contains 2 entries I don't need:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

If I remove those lines, this file regenerated and permissions are added again. What is correct way to remove these permissions? I use apache cordova 3.5.0


回答1:


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 ...



回答2:


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.




回答3:


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:

  1. Add the following namespace declaration to the top-level <manifest> tag xmlns:tools="http://schemas.android.com/tools"
  2. (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.




回答4:


I tried the answer above but the manifest file kept getting re-generated. I solved the problem by creating an "after_prepare" hook that modifies the manifest file during the build.

The code is posted on this thread.



来源:https://stackoverflow.com/questions/25265908/cordova-remove-unnecessary-permissions

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