nativescript-cli icon indicating copy to clipboard operation
nativescript-cli copied to clipboard

fix(android): list installed packages on some Samsung devices

Open rigor789 opened this issue 3 years ago • 2 comments

PR Checklist

  • [x] The PR title follows our guidelines: https://github.com/NativeScript/NativeScript/blob/master/tools/notes/CONTRIBUTING.md#commit-messages.
  • [ ] There is an issue for the bug/feature this PR is for. To avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it.
  • [x] You have signed the CLA.
  • [x] All existing tests are passing: https://github.com/NativeScript/nativescript-cli/blob/master/CONTRIBUTING.md#contribute-to-the-code-base
  • [ ] Tests for the changes are included.

What is the current behavior?

On some devices listing installed packages via adb shell breaks due to a permission error - and makes other parts of the CLI fail because we can't verify whether or not the app has been installed.

What is the new behavior?

In case listing the packages fails - we try an alternative (slower) method:

  • list all users
  • list all packages per user and concatenate them
  • continue with the rest of the logic...

rigor789 avatar Jan 20 '23 17:01 rigor789

Update: draft state because errors are part of stderr but not caught in the catch block - need to parse command result for output like this:

./adb shell "pm list packages"

Exception occurred while executing 'list':
java.lang.SecurityException: Shell does not have permission to access user 150
 com.android.server.am.ActivityManagerService.handleIncomingUser:14872 android.app.ActivityManager.handleIncomingUser:4802 com.android.server.pm.PackageManagerShellCommand.translateUserId:3499
        at com.android.server.am.UserController.handleIncomingUser(UserController.java:2672)
        at com.android.server.am.ActivityManagerService.handleIncomingUser(ActivityManagerService.java:14872)
        at android.app.ActivityManager.handleIncomingUser(ActivityManager.java:4802)
        at com.android.server.pm.PackageManagerShellCommand.translateUserId(PackageManagerShellCommand.java:3499)
        at com.android.server.pm.PackageManagerShellCommand.runListPackages(PackageManagerShellCommand.java:965)
        at com.android.server.pm.PackageManagerShellCommand.runListPackages(PackageManagerShellCommand.java:861)
        at com.android.server.pm.PackageManagerShellCommand.runList(PackageManagerShellCommand.java:720)
        at com.android.server.pm.PackageManagerShellCommand.onCommand(PackageManagerShellCommand.java:222)
        at com.android.modules.utils.BasicShellCommandHandler.exec(BasicShellCommandHandler.java:97)
        at android.os.ShellCommand.exec(ShellCommand.java:38)
        at com.android.server.pm.PackageManagerService$IPackageManagerImpl.onShellCommand(PackageManagerService.java:6922)
        at android.os.Binder.shellCommand(Binder.java:1085)
        at android.os.Binder.onTransact(Binder.java:903)
        at android.content.pm.IPackageManager$Stub.onTransact(IPackageManager.java:4909)
        at com.android.server.pm.PackageManagerService$IPackageManagerImpl.onTransact(PackageManagerService.java:6906)
        at android.os.Binder.execTransactInternal(Binder.java:1321)
        at android.os.Binder.execTransact(Binder.java:1280)
Exit code is: 255

Temporary fix was to modify the android-debug-bridge -> executeCommand() function to concat stderr to output + parse/check for the specific string:

result.stdout.concat(result.stderr)

// ...

if(result.includes('Exception occurred while executing')) {
  throw result
}

rigor789 avatar Feb 13 '23 18:02 rigor789