Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niczak/40c2059027b3740f34e3170043eb635e to your computer and use it in GitHub Desktop.
Save niczak/40c2059027b3740f34e3170043eb635e to your computer and use it in GitHub Desktop.
Add permissions to AndroidManifest.xml with cordova hook for Ionic 2
This script uses as a cordova hook and provides ability to add android permissions to AndroidManifest.xml on the fly. It also
checks and computes which of provided permissions are already added and didn't rewrite AndroidManifest.xml if no permissions provided.
Executes only for android platform.
- node 4.2+
- npm modules: lodash, xml2js
Distributed under the MIT license.
// create a file 020_add_android_permissions.js in your hooks/after_prepare folder with the following code:
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const _ = require('lodash');
const xml2js = require('xml2js');
// here add/remove permissions you need for Android app
let permissions = [
module.exports = function (context) {
const parseString = xml2js.parseString;
const builder = new xml2js.Builder();
const manifestPath = context.opts.projectRoot + '/platforms/android/AndroidManifest.xml';
const androidManifest = fs.readFileSync(manifestPath).toString();
let manifestRoot,
if (androidManifest && permissions.length > 0) {
parseString(androidManifest, (err, manifest) => {
if (err) return console.error(err);
manifestRoot = manifest['manifest'];
if (!manifestRoot['uses-permission']) {
manifestRoot['uses-permission'] = [];
missedPermissions = _.difference(permissions,['uses-permission'], `$['android:name']`));
if (missedPermissions.length > 0) {
missedPermissions.forEach(perm => manifestRoot['uses-permission'].push({'$': {'android:name': perm}}));
fs.writeFileSync(manifestPath, builder.buildObject(manifest));
console.log(`Added ${missedPermissions.length} permissions:`);
missedPermissions.forEach(perm => console.log(` - ${perm}`));
// after that adjust yous config.xml to look like this:
<widget ...>
<platform name="android">
<hook type="after_prepare" src="hooks/after_prepare/020_add_android_permissions.js"/>
// run `ionc build android`
// Enjoy!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment