Skip to content

Commit 89b3348

Browse files
fix: Enforce '--user 0' argument if cmd package list packages throws an access error (#761)
1 parent 5555823 commit 89b3348

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

lib/tools/apk-utils.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ const RESOLVER_ACTIVITY_NAME = 'android/com.android.internal.app.ResolverActivit
4444
*/
4545
apkUtilsMethods.isAppInstalled = async function isAppInstalled (pkg, opts = {}) {
4646
const {
47-
user
47+
user,
4848
} = opts;
4949

5050
log.debug(`Getting install status for ${pkg}`);
51+
/** @type {boolean} */
5152
let isInstalled;
5253
if (await this.getApiLevel() < 26) {
5354
try {
@@ -66,7 +67,18 @@ apkUtilsMethods.isAppInstalled = async function isAppInstalled (pkg, opts = {})
6667
if (util.hasValue(user)) {
6768
cmd.push('--user', user);
6869
}
69-
const stdout = await this.shell(cmd);
70+
/** @type {string} */
71+
let stdout;
72+
try {
73+
stdout = await this.shell(cmd);
74+
} catch (e) {
75+
// https://github.com/appium/appium-uiautomator2-driver/issues/810
76+
if (_.includes(e.stderr || e.stdout || e.message, 'access user') && _.isEmpty(user)) {
77+
stdout = await this.shell([...cmd, '--user', '0']);
78+
} else {
79+
throw e;
80+
}
81+
}
7082
isInstalled = new RegExp(`^package:${_.escapeRegExp(pkg)}$`, 'm').test(stdout);
7183
}
7284
log.debug(`'${pkg}' is${!isInstalled ? ' not' : ''} installed`);

0 commit comments

Comments
 (0)