Skip to content

fix(android): add null checks for plugin annotation when retrieving permissions - #8400

Merged
OS-pedrogustavobilro merged 4 commits into
ionic-team:mainfrom
robingenz:fix/bridge-npe-get-permission-states
Sep 10, 2026
Merged

OS-pedrogustavobilro merged 4 commits into
ionic-team:mainfrom
robingenz:fix/bridge-npe-get-permission-states

Conversation

@robingenz

@robingenz robingenz commented Mar 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes a NullPointerException crash in Bridge.getPermissionStates() that occurs when getPluginAnnotation() returns null at runtime (see #8399). The fix wraps the permissions iteration in a null check, matching the defensive pattern in Plugin.isPermissionDeclared() (see here).

Closes #8399

Copilot AI review requested due to automatic review settings March 24, 2026 16:07
@jcesarmobile

Copy link
Copy Markdown
Member

#8399 is closed due to the lack of reproduction, can you provide a sample app that reproduces the issue?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Prevents an Android crash in Bridge.getPermissionStates() when a plugin’s @CapacitorPlugin annotation metadata is missing at runtime (reported in #8399), by guarding the permissions iteration with a null check.

Changes:

  • Add null guard around annotation.permissions() iteration in Bridge.getPermissionStates().
  • Return an empty permission-state map when plugin annotation metadata is unavailable.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@robingenz

Copy link
Copy Markdown
Contributor Author

@zovi-code Can you please provide a Minimal, Reproducible Example? Feel free to use this template.

@zovi-code

Copy link
Copy Markdown

@robingenz

I will create it now but it is important to add that it does not happen always but sometimes.

I have 5 Android devices and I can not reproduce it but in Crashlytics there are reports where exactly this causes crash.
There I saw it happened on these devices today: Galaxy A15 (Android 16), Galaxy S25+ (Android 16), Galaxy A22 5G (Android 13)

@zovi-code

Copy link
Copy Markdown

@robingenz here it is https://github.com/ferhadm/capcrash

Note: My original app is in NextJS so I wanted to use same exact code that I use there and here is minimum version.

@zovi-code

Copy link
Copy Markdown

@robingenz I want to inform you that in the meantime, I found a device that can reproduce this issue. I installed a patch and pushed it to production. This same device reinstalled the app and crash was not happening anymore.

That proves that you managed to fix this issue. Thank you very much!

Screenshot 2026-03-26 at 21 04 44

@jadonhansen

This comment was marked as abuse.

@jadonhansen

Copy link
Copy Markdown

Updated stats. This bug is causing a lot of crashes in production. This is a real issue and the solution is already out there
Screenshot 2026-04-23 at 12 44 29

@arianfaridNNI

Copy link
Copy Markdown

Hello team,

Thanks for working on a fix for this.

Adding this comment here, since the original issue is now limited to collaborators.

My team hit this same bug in production on @capacitor/[email protected], confirmed on Android 15 and 16 devices. We applied this fix via patch-package and confirmed it resolves the getPermissionStates crash.

There's a second unguarded path with the same (or related) root cause in Plugin.getPermissionStringsForAliases():

private String[] getPermissionStringsForAliases(@NonNull String[] aliases) {
    CapacitorPlugin annotation = handle.getPluginAnnotation();
    HashSet<String> perms = new HashSet<>();
    for (Permission perm : annotation.permissions()) { // NPE when annotation is null

We applied the same fix, wrapping the for loop in if (annotation != null).

I will note that with both null guards in place the crash is resolved, but local notifications are silently non-functional for affected users.

@cxcxcxcx

Copy link
Copy Markdown

We hit the same release-only permission metadata problem on @capacitor/[email protected] (Samsung, Android 16, minifyEnabled true). Related to #8399, but we saw a second failure mode that explains why null guards alone were not enough for us.

Symptom 1 (original)

NPE in Bridge.getPermissionStates() when opening camera.

Symptom 2 (after null-guard patch only)

No NPE, but camera still fails:

E Capacitor: isPermissionDeclared: No alias defined for camera or missing @CapacitorPlugin annotation.
E AndroidRuntime: SecurityException: ... with revoked permission android.permission.CAMERA

checkCameraPermissions() sets needCameraPerms = false when isPermissionDeclared() fails, skips the prompt, and launches the camera intent without CAMERA granted.

Root cause (our finding)

Shipped consumer rules in android/capacitor/proguard-rules.pro keep:

@com.getcapacitor.annotation.Permission <methods>

But @Permission is nested in @CapacitorPlugin(permissions = …), not on methods. In release, R8 can strip that nested metadata. Then plugin permission aliases are unavailable at runtime.

What fixed it for us

App-level proguard-rules.pro:

-keepattributes RuntimeVisibleAnnotations, AnnotationDefault
-keep @interface com.getcapacitor.annotation.CapacitorPlugin { *; }
-keep @interface com.getcapacitor.annotation.Permission { *; }

After this, isPermissionDeclared() works and camera capture works in release without a camera-plugin workaround.

Not sure if your issue may have a similar root cause.

SableWolphen added a commit to SableWolphen/plushlist that referenced this pull request Aug 3, 2026
…ar has nothing to show, log widget update failures (#79)

The push-notification-enable crash was never actually fixed by the earlier
proguard/double-permission-request changes — got the real Crashlytics stack
trace this time, and it's an open, unfixed upstream Capacitor bug
(ionic-team/capacitor#8400): Bridge.getPermissionStates() throws a real
native NullPointerException when PushNotifications.requestPermissions() is
called, crashing the whole app before any JS try/catch ever gets a chance
to run. Confirmed present on both 1.0.8 and 1.0.9, unrelated to anything
shipped so far this session.

Added NotificationPermissionPlugin, a small native plugin that requests
POST_NOTIFICATIONS directly with Android's own permission API instead of
going through Capacitor's broken helper. enableNativeNotifications() now
calls it first, falling back to the old Capacitor call only if that plugin
is somehow unavailable.

Also: html/body never had an explicit background-color, so the safe-area
strip under Android's edge-to-edge status bar could fall back to the
WebView's own default canvas color instead of the app's actual background
— set explicitly now. And the widget's update call no longer swallows
errors silently, so a real failure is visible in the WebView console/logcat
instead of just disappearing.

Co-authored-by: SableWolphen <[email protected]>
cursor Bot pushed a commit to Cap-go/capacitor-plus that referenced this pull request Aug 13, 2026
@riderx

riderx commented Aug 26, 2026

Copy link
Copy Markdown

This fix is available as a quick patch through @capgo/capacitor-patch.

Patch ID: upstream-pr-8400-android

plugins: {
  CapacitorPatch: {
    patches: ['upstream-pr-8400-android'],
    strict: true,
  },
}

Run npx cap sync after installing @capgo/capacitor-patch.

@OS-pedrogustavobilro OS-pedrogustavobilro changed the title fix(android): add null check for plugin annotation in getPermissionStates fix(android): add null checks for plugin annotation when retrieving permissions Sep 10, 2026

@OS-pedrogustavobilro OS-pedrogustavobilro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @robingenz, thanks for opening the PR, sorry for the time it took to look into it.

I took the liberty of making an additional null-check on Plugin.java that was mentined in a comment.

I was able to reproduce this on https://github.com/ferhadm/capcrash + turning on minification, which may not be the exact same reproduction conditions, but with this PR the crash no longer occured.

While there could be future work we'll do to address other issues with proguard and app obfuscation (especially with upcoming Play Store changes for early 2027), this PR still makes sense, so we'll be merging it, and it should go on the next Capacitor release.

Thanks for the contribution!

renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Oct 12, 2026
##### [v8.5.2](https://github.com/ionic-team/capacitor/blob/HEAD/CHANGELOG.md#852-2026-09-11)

##### Bug Fixes

- **android:** add null checks for plugin annotation when retrieving permissions ([#8400](ionic-team/capacitor#8400)) ([035b16a](ionic-team/capacitor@035b16a))
- **ios:** do not forward scene lifecycle events to the page before it has loaded ([#8595](ionic-team/capacitor#8595)) ([c567328](ionic-team/capacitor@c567328))
- resolve issues with safe area / systembars plugin ([#8535](ionic-team/capacitor#8535)) ([e37d9c6](ionic-team/capacitor@e37d9c6))
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Oct 12, 2026
##### [v8.5.2](https://github.com/ionic-team/capacitor/blob/HEAD/CHANGELOG.md#852-2026-09-11)

##### Bug Fixes

- **android:** add null checks for plugin annotation when retrieving permissions ([#8400](ionic-team/capacitor#8400)) ([035b16a](ionic-team/capacitor@035b16a))
- **ios:** do not forward scene lifecycle events to the page before it has loaded ([#8595](ionic-team/capacitor#8595)) ([c567328](ionic-team/capacitor@c567328))
- resolve issues with safe area / systembars plugin ([#8535](ionic-team/capacitor#8535)) ([e37d9c6](ionic-team/capacitor@e37d9c6))
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Oct 12, 2026
##### [v8.5.2](https://github.com/ionic-team/capacitor/blob/HEAD/CHANGELOG.md#852-2026-09-11)

##### Bug Fixes

- **android:** add null checks for plugin annotation when retrieving permissions ([#8400](ionic-team/capacitor#8400)) ([035b16a](ionic-team/capacitor@035b16a))
- **ios:** do not forward scene lifecycle events to the page before it has loaded ([#8595](ionic-team/capacitor#8595)) ([c567328](ionic-team/capacitor@c567328))
- resolve issues with safe area / systembars plugin ([#8535](ionic-team/capacitor#8535)) ([e37d9c6](ionic-team/capacitor@e37d9c6))
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Oct 12, 2026
##### [v8.5.2](https://github.com/ionic-team/capacitor/blob/HEAD/CHANGELOG.md#852-2026-09-11)

##### Bug Fixes

- **android:** add null checks for plugin annotation when retrieving permissions ([#8400](ionic-team/capacitor#8400)) ([035b16a](ionic-team/capacitor@035b16a))
- **ios:** do not forward scene lifecycle events to the page before it has loaded ([#8595](ionic-team/capacitor#8595)) ([c567328](ionic-team/capacitor@c567328))
- resolve issues with safe area / systembars plugin ([#8535](ionic-team/capacitor#8535)) ([e37d9c6](ionic-team/capacitor@e37d9c6))
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Oct 12, 2026
##### [v8.5.2](https://github.com/ionic-team/capacitor/blob/HEAD/CHANGELOG.md#852-2026-09-11)

##### Bug Fixes

- **android:** add null checks for plugin annotation when retrieving permissions ([#8400](ionic-team/capacitor#8400)) ([035b16a](ionic-team/capacitor@035b16a))
- **ios:** do not forward scene lifecycle events to the page before it has loaded ([#8595](ionic-team/capacitor#8595)) ([c567328](ionic-team/capacitor@c567328))
- resolve issues with safe area / systembars plugin ([#8535](ionic-team/capacitor#8535)) ([e37d9c6](ionic-team/capacitor@e37d9c6))
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Oct 12, 2026
##### [v8.5.2](https://github.com/ionic-team/capacitor/blob/HEAD/CHANGELOG.md#852-2026-09-11)

##### Bug Fixes

- **android:** add null checks for plugin annotation when retrieving permissions ([#8400](ionic-team/capacitor#8400)) ([035b16a](ionic-team/capacitor@035b16a))
- **ios:** do not forward scene lifecycle events to the page before it has loaded ([#8595](ionic-team/capacitor#8595)) ([c567328](ionic-team/capacitor@c567328))
- resolve issues with safe area / systembars plugin ([#8535](ionic-team/capacitor#8535)) ([e37d9c6](ionic-team/capacitor@e37d9c6))
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Oct 12, 2026
##### [v8.5.2](https://github.com/ionic-team/capacitor/blob/HEAD/CHANGELOG.md#852-2026-09-11)

##### Bug Fixes

- **android:** add null checks for plugin annotation when retrieving permissions ([#8400](ionic-team/capacitor#8400)) ([035b16a](ionic-team/capacitor@035b16a))
- **ios:** do not forward scene lifecycle events to the page before it has loaded ([#8595](ionic-team/capacitor#8595)) ([c567328](ionic-team/capacitor@c567328))
- resolve issues with safe area / systembars plugin ([#8535](ionic-team/capacitor#8535)) ([e37d9c6](ionic-team/capacitor@e37d9c6))
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Oct 12, 2026
##### [v8.5.2](https://github.com/ionic-team/capacitor/blob/HEAD/CHANGELOG.md#852-2026-09-11)

##### Bug Fixes

- **android:** add null checks for plugin annotation when retrieving permissions ([#8400](ionic-team/capacitor#8400)) ([035b16a](ionic-team/capacitor@035b16a))
- **ios:** do not forward scene lifecycle events to the page before it has loaded ([#8595](ionic-team/capacitor#8595)) ([c567328](ionic-team/capacitor@c567328))
- resolve issues with safe area / systembars plugin ([#8535](ionic-team/capacitor#8535)) ([e37d9c6](ionic-team/capacitor@e37d9c6))
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Nov 8, 2026
##### [v8.6.0](https://github.com/ionic-team/capacitor/blob/HEAD/CHANGELOG.md#860-2026-10-28)

##### Bug Fixes

- **android:** keep callbacks on non-public base classes from R8 ([02e43f8](ionic-team/capacitor@02e43f8))
- **android:** keep plugin annotation types from R8 full mode ([#8646](ionic-team/capacitor#8646)) ([9dc5aeb](ionic-team/capacitor@9dc5aeb))
- **cli:** bump xcode to ^4.0.0 to drop vulnerable uuid\@7 ([#8674](ionic-team/capacitor#8674)) ([463225f](ionic-team/capacitor@463225f))
- **cli:** redact sensitive values from telemetry payloads ([#8665](ionic-team/capacitor#8665)) ([d19f1ea](ionic-team/capacitor@d19f1ea))
- **cli:** report npm 12 blocked install scripts in doctor and migrate ([#8655](ionic-team/capacitor#8655)) ([841017a](ionic-team/capacitor@841017a))

##### Features

- **android:** recognize [@capacitor-community/ssl-pinning](https://github.com/capacitor-community/ssl-pinning) for cert pinning ([#8669](ionic-team/capacitor#8669)) ([19d2a40](ionic-team/capacitor@19d2a40))
- **cli:** recognize [@capacitor-community/ssl-pinning](https://github.com/capacitor-community/ssl-pinning) for auto cert copy ([#8666](ionic-team/capacitor#8666)) ([50a3c51](ionic-team/capacitor@50a3c51))

##### Reverts

- Revert "chore(ios): add `App/CapApp-SPM/Package.resolved` to `.gitignore` ([#8649](ionic-team/capacitor#8649))" ([175c933](ionic-team/capacitor@175c933)), closes [#8649](ionic-team/capacitor#8649)

#### [8.5.3](ionic-team/capacitor@8.5.2...8.5.3) (2026-10-07)

##### Bug Fixes

- **cli:** change Cordova AppDelegate.h imports for SPM ([#8618](ionic-team/capacitor#8618)) ([145560e](ionic-team/capacitor@145560e))

#### [8.5.2](ionic-team/capacitor@8.5.1...8.5.2) (2026-09-11)

##### Bug Fixes

- **android:** add null checks for plugin annotation when retrieving permissions ([#8400](ionic-team/capacitor#8400)) ([035b16a](ionic-team/capacitor@035b16a))
- **ios:** do not forward scene lifecycle events to the page before it has loaded ([#8595](ionic-team/capacitor#8595)) ([c567328](ionic-team/capacitor@c567328))
- resolve issues with safe area / systembars plugin ([#8535](ionic-team/capacitor#8535)) ([e37d9c6](ionic-team/capacitor@e37d9c6))

#### [8.5.1](ionic-team/capacitor@8.5.0...8.5.1) (2026-08-31)

##### Bug Fixes

- block navigation to the internal HTTP proxy path ([ee586ae](ionic-team/capacitor@ee586ae))
- **cli:** use POSIX paths in CapApp-SPM Package.swift ([#8549](ionic-team/capacitor#8549)) ([5e5bb3b](ionic-team/capacitor@5e5bb3b))
- **core:** prevent removeListener from removing wrong listener ([#8271](ionic-team/capacitor#8271)) ([5ac4dd6](ionic-team/capacitor@5ac4dd6))
##### [v8.5.3](https://github.com/ionic-team/capacitor/blob/HEAD/CHANGELOG.md#853-2026-10-07)

##### Bug Fixes

- **cli:** change Cordova AppDelegate.h imports for SPM ([#8618](ionic-team/capacitor#8618)) ([145560e](ionic-team/capacitor@145560e))
riderx added a commit to Cap-go/capacitor-plus that referenced this pull request Nov 11, 2026
* Release 8.3.2

* fix(cli): copy plugin files in CocoaPods projects (ionic-team#8467)

* Release 8.3.3

* chore(cli): update telemetry endpoint (ionic-team#8464)

* Release 8.3.4

* chore: format java code (ionic-team#8475)

* chore: format java code (ionic-team#8477)

* feat: add method getDouble to plugin config (ionic-team#7638)

Co-authored-by: jcesarmobile <[email protected]>

* fix(SystemBars): make `safe-area-inset-x` available on API <= 34 (ionic-team#8424)

* feat(cli): add experimental packageOptions (ionic-team#8471)

* feat(cli): capture ios_package_manager in telemetry (ionic-team#8482)

* fix(cli): revert live reload config on failure (ionic-team#8485)

* chore: fix typo in declarations.ts (ionic-team#8488)

* fix(SystemBars): respect `insetsHandling` disable (ionic-team#8481)

* fix(android): show only the requested system bar (ionic-team#8480)

Co-authored-by: Joey Pender <[email protected]>
Co-authored-by: Pedro Bilro <[email protected]>
Co-authored-by: Mark Anderson <[email protected]>

* chore: Decrease timeout for CI jobs from 60 to 30 minutes (ionic-team#8476)

Co-authored-by: jcesarmobile <[email protected]>

* Release 8.4.0

* fix(cli): patch Capacitor SPM dependency version in plugins (ionic-team#8492)

Co-authored-by: Mark Anderson <[email protected]>

* fix(cli): make SPM dependency patch work on prereleases (ionic-team#8508)

* Release 8.4.1

* chore: run `npm run fmt` to fix lint errors (ionic-team#8516)

* fix(android): explicitly grant URI permissions for image capture intent (ionic-team#8526)

* Release 8.4.2

* chore(android): fix lint issues (ionic-team#8542)

* fix(cli): support TypeScript 7 when loading capacitor.config.ts (ionic-team#8534)

* feat(ios): UIScene Support (ionic-team#8536)

* feat(cli): add migrator functionality for adopting UIScene (ionic-team#8544)

* Release 8.5.0

* fix(cli): use POSIX paths in CapApp-SPM Package.swift (ionic-team#8549)

Co-authored-by: Cursor <[email protected]>

* fix(core): prevent removeListener from removing wrong listener (ionic-team#8271)

Co-authored-by: Eric Horodyski <[email protected]>

* fix: block navigation to the internal HTTP proxy path

* Release 8.5.1

* fix(ios): do not forward scene lifecycle events to the page before it has loaded (ionic-team#8595)

* fix(android): add null checks for plugin annotation when retrieving permissions (ionic-team#8400)

Co-authored-by: Claude Opus 4.6 <[email protected]>
Co-authored-by: Pedro Bilro <[email protected]>

* fix: resolve issues with safe area / systembars plugin (ionic-team#8535)

* Release 8.5.2

* fix(cli): change Cordova AppDelegate.h imports for SPM (ionic-team#8618)

* chore: add .build/ folder to gitignore (ionic-team#8640)

* Release 8.5.3

* chore(ios): add `App/CapApp-SPM/Package.resolved` to `.gitignore` (ionic-team#8649)

* Revert "chore(ios): add `App/CapApp-SPM/Package.resolved` to `.gitignore` (ionic-team#8649)"

This reverts commit eb06606.

* fix(android): keep plugin annotation types from R8 full mode (ionic-team#8646)

* docs: minor correction (ionic-team#8660)

* fix(cli): report npm 12 blocked install scripts in doctor and migrate (ionic-team#8655)

* feat(cli): recognize @capacitor-community/ssl-pinning for auto cert copy (ionic-team#8666)

* feat(android): recognize @capacitor-community/ssl-pinning for cert pinning (ionic-team#8669)

* fix(android): keep callbacks on non-public base classes from R8

* fix(cli): redact sensitive values from telemetry payloads (ionic-team#8665)

* fix(cli): bump xcode to ^4.0.0 to drop vulnerable uuid@7 (ionic-team#8674)

Co-authored-by: Mark Anderson <[email protected]>

* chore: add Backstage catalog-info.yaml

Adds a catalog-info.yaml so Capacitor and its core, cli, android, and ios packages show up in OutSystems' Backstage software catalog.

* Release 8.6.0

* fix(android): skip lifecycle events when the Capacitor bridge is not present (ionic-team#8683)

* fix(ios): read CapacitorCookies/CapacitorHttp isEnabled from injected global (ionic-team#8688)

* style: format files after merging plus

---------

Co-authored-by: Github Workflow (on behalf of markemer) <users.noreply.github.com>
Co-authored-by: jcesarmobile <[email protected]>
Co-authored-by: Alex J. <[email protected]>
Co-authored-by: Robin Genz <[email protected]>
Co-authored-by: Joey Pender <[email protected]>
Co-authored-by: Minh Vu <[email protected]>
Co-authored-by: Pedro Bilro <[email protected]>
Co-authored-by: Mark Anderson <[email protected]>
Co-authored-by: Conor Brady <[email protected]>
Co-authored-by: Omri Willheim <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Manik Tyagi <[email protected]>
Co-authored-by: Eric Horodyski <[email protected]>
Co-authored-by: Chace Daniels <[email protected]>
Co-authored-by: Gábor Tóth <[email protected]>
Co-authored-by: Claude Opus 4.6 <[email protected]>
Co-authored-by: Tafel <[email protected]>
Co-authored-by: Piotr <[email protected]>
Co-authored-by: DravenLee <[email protected]>
Co-authored-by: OS-ruimoreiramendes <[email protected]>
Co-authored-by: Mauro Kinjuk <[email protected]>
Co-authored-by: Eric Horodyski <[email protected]>
Co-authored-by: Tori Chan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: getPermissionStates causes app crash

9 participants