|
| 1 | +// (C) Copyright 2015 Moodle Pty Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/** |
| 16 | + * Script to remove permissions for APK files. Implementation obtained from: |
| 17 | + * https://stackoverflow.com/a/67530993/5820052 |
| 18 | + */ |
| 19 | +const fs = require('fs/promises'); |
| 20 | +const xml2js = require('xml2js'); |
| 21 | + |
| 22 | +const REMOVE_PERMISSIONS = [ |
| 23 | + 'android.permission.REQUEST_INSTALL_PACKAGES', |
| 24 | +]; |
| 25 | + |
| 26 | +module.exports = async function(context) { |
| 27 | + const root = context.opts.projectRoot; |
| 28 | + const manifestPath = root + '/platforms/android/app/src/main/AndroidManifest.xml'; |
| 29 | + |
| 30 | + const manifestXml = await fs.readFile(manifestPath); |
| 31 | + const manifest = await xml2js.parseStringPromise(manifestXml); |
| 32 | + |
| 33 | + const usesPermissions = manifest.manifest['uses-permission']; |
| 34 | + if (Array.isArray(usesPermissions)) { |
| 35 | + manifest.manifest['uses-permission'] = usesPermissions.filter(usesPermission => { |
| 36 | + const attrs = usesPermission.$ || {}; |
| 37 | + const name = attrs['android:name']; |
| 38 | + |
| 39 | + if (REMOVE_PERMISSIONS.includes(name)) { |
| 40 | + console.log(`Removing permission "${name}" from AndroidManifest.xml`); |
| 41 | + return false; |
| 42 | + } else { |
| 43 | + return true; |
| 44 | + } |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + const newManifest = (new xml2js.Builder()).buildObject(manifest); |
| 49 | + |
| 50 | + await fs.writeFile(manifestPath, newManifest); |
| 51 | +} |
0 commit comments