Cordoba adds unwanted permission to AndroidManifest.xml when creating from the command line

I use the CLI to create my Cordova application and I have added the Media plugin.

'cordova build' automatically adds android.permission.RECORD_AUDIO to my AndroidManifest.xml, although I do not use this permission.

So how to remove it? Every time I create for release, permission is added in apk.

+4
android command-line-interface cordova permissions
source share
6 answers

In your project, edit the plugins file / org.apache.cordova.media / plugin.xml You will see a specific configuration for Android.

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

delete the line <uses-permission android:name="android.permission.RECORD_AUDIO" /> , as this, the permission will not be added every time it is created.

Since the permission has already been added to AndroidManifest.xml, you will have to delete it manually and then it should not be returned the next time you create it.

+6
source share

To prevent plugins from adding unnecessary permissions for editing platforms /android/android.json.

Find these lines and delete 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 probably have to repeat this.

+6
source share

I tried the sentences above (from QuickFix and leuk98743), but the manifest file continued to be regenerated. Therefore, I created a hook to modify the manifest file during assembly.

  1. Add the content below to the file in your project: hooks/after_prepare/030_remove_permissions.js
  2. If you are running Linux, make this file executable.
  3. Modify the file to set the permissions you want to remove. My example lists 3, but you have to add / remove if necessary.
 #!/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 ); } ); } ); 
+4
source share

after unsuccessfully requesting several offers;

went to platforms \ android \ cordova and launched

 >clean 

returned to the project directory and (in windows) launched

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

open .txt result file to view files with permission in them

permission to delete all listed files has been removed, except for AudioHandler.java files.

Did the assembly, and it worked. At last.

0
source share

I did below:

  1. Permission entries from 2 files were deleted:

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

  1. Restored apk in release mode.

It worked, deleted permissions, dint entries returned to the manifest file.
Successfully uploaded the APK to the game console.

0
source share

Clone Steve-eba will answer for those who are more comfortable with python. It is assumed that the file will be located in a folder of type hooks/after_prepare/123_remove_permissions.py and will 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) 
0
source share

All Articles