Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cordova - remove unnecessary permissions

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

like image 696
vbezhenar Avatar asked Aug 12 '14 13:08

vbezhenar


2 Answers

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.

like image 67
Saeed HosseinPoor Avatar answered Oct 04 '22 07:10

Saeed HosseinPoor


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.

like image 35
Alexander Wallace Matchneer Avatar answered Oct 04 '22 09:10

Alexander Wallace Matchneer