Skip to content

Commit c230fbb

Browse files
committed
MOBILE-4134 android: Add script to remove INSTALL_PACKAGES permission
1 parent 983e4ab commit c230fbb

File tree

4 files changed

+100
-6
lines changed

4 files changed

+100
-6
lines changed

config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<resource-file src="resources/android/icon/drawable-hdpi-smallicon.png" target="app/src/main/res/mipmap-hdpi/smallicon.png" />
6464
<resource-file src="resources/android/icon/drawable-xhdpi-smallicon.png" target="app/src/main/res/mipmap-xhdpi/smallicon.png" />
6565
<resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />
66+
<hook src="scripts/android/remove_permissions.js" type="after_prepare" />
6667
<edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application/activity[@android:name='MainActivity']">
6768
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|screenLayout|smallestScreenSize" android:exported="true" />
6869
</edit-config>

package-lock.json

Lines changed: 46 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@
172172
"terser-webpack-plugin": "4.2.3",
173173
"ts-jest": "26.4.1",
174174
"ts-node": "8.3.0",
175-
"typescript": "3.9.9"
175+
"typescript": "3.9.9",
176+
"xml2js": "0.4.23"
176177
},
177178
"engines": {
178179
"node": ">=14.15.0 <15"
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)