Skip to content

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

Open
rigor789 wants to merge 3 commits into
mainfrom
fix/android-list-installed-packages-samsung
Open

fix(android): list installed packages on some Samsung devices#5713
rigor789 wants to merge 3 commits into
mainfrom
fix/android-list-installed-packages-samsung

Conversation

@rigor789

@rigor789 rigor789 commented Jan 20, 2023

Copy link
Copy Markdown
Member

PR Checklist

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...

Summary by CodeRabbit

  • Bug Fixes
    • Improved detection of installed Android applications when global package listings are unavailable or incomplete.
    • Added support for discovering packages installed across multiple Android users, with duplicate entries removed.
    • Improved reliability by falling back to per-user package listings when needed.
    • Improved performance by reusing results from per-user application scans.

@cla-bot cla-bot Bot added the cla: yes label Jan 20, 2023
@rigor789

rigor789 commented Feb 13, 2023

Copy link
Copy Markdown
Member Author

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 rigor789 added this to the 8.5 milestone Mar 15, 2023
@NathanWalker NathanWalker modified the milestones: 8.5, 9.0 Oct 23, 2023
@rigor789 rigor789 removed this from the 9.0 milestone Apr 3, 2024
@neryortez

Copy link
Copy Markdown

Is there anything I can help with this one ?
I'm having the same issue, I was about to start developing this but I found your PR already

@rigor789
rigor789 force-pushed the fix/android-list-installed-packages-samsung branch from c39f616 to c4560b9 Compare September 9, 2026 06:12
@coderabbitai

coderabbitai Bot commented Sep 9, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: a9a925cc-602b-437b-97a5-22ee3948f458

📥 Commits

Reviewing files that changed from the base of the PR and between b2ad407 and 9792f52.

📒 Files selected for processing (2)
  • lib/common/mobile/android/android-application-manager.ts
  • lib/common/test/unit-tests/android-application-manager.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • lib/common/test/unit-tests/android-application-manager.ts
  • lib/common/mobile/android/android-application-manager.ts

Included review availability: Your plan provides up to 4 included reviews per hour; 2 remain after this review.


📝 Walkthrough

Walkthrough

getInstalledApplications now supports global package discovery and cached per-user fallback. The change parses Android user IDs, deduplicates package names, and adds tests for fallback, caching, and command execution.

Changes

Android package discovery

Layer / File(s) Summary
Package discovery flow
lib/common/mobile/android/android-application-manager.ts
The manager tries global package listing first. When required, it lists Android users, queries packages per user, parses output, and removes duplicates.
Package discovery validation
lib/common/test/unit-tests/android-application-manager.ts
Tests verify global parsing, per-user fallback, deduplication, cached fallback, and shell command sequences. Existing formatting-only changes are included.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 9792f

Package discovery now falls back to per-user listing when global Android package listing fails, with deduplication and cached results. No concrete merge-blocking risk remains.

Sequence Diagram(s)

sequenceDiagram
  participant AndroidApplicationManager
  participant AndroidDebugBridge
  participant AndroidPackageManager
  AndroidApplicationManager->>AndroidDebugBridge: Run global package listing
  AndroidDebugBridge->>AndroidPackageManager: Execute pm list packages
  AndroidPackageManager-->>AndroidDebugBridge: Return package output
  alt Output is empty or per-user mode is enabled
    AndroidApplicationManager->>AndroidDebugBridge: Run pm list users
    AndroidDebugBridge->>AndroidPackageManager: Execute user listing
    AndroidPackageManager-->>AndroidDebugBridge: Return user IDs
    AndroidApplicationManager->>AndroidDebugBridge: Run pm list packages --user ID
    AndroidDebugBridge->>AndroidPackageManager: Execute per-user listing
    AndroidPackageManager-->>AndroidDebugBridge: Return package output
  end
  AndroidApplicationManager-->>AndroidApplicationManager: Parse and deduplicate packages
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: fixing installed-package listing for affected Samsung Android devices through fallback package enumeration.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks each package trail
Across the users, without fail
Empty lists send plans awry
Duplicate names hop by
Cached commands rest warm and neat
Tests confirm the flow is complete

Comment @coderabbitai help to get the list of available commands.

`executeShellCommand` never rejects on a non-zero exit code, so catching
an error around `pm list packages` could not detect the SecurityException
some Samsung devices raise for the Secure Folder user (150). Detect the
failure from the empty package output instead, list packages per user,
deduplicate the result, and keep using the per-user path for the rest of
the session since the installed-apps check is polled frequently.
@rigor789
rigor789 marked this pull request as ready for review September 9, 2026 06:27
@rigor789
rigor789 requested a review from edusperoni September 9, 2026 06:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants