Skip to content

feat(android): Add ANR warning breadcrumbs on Android 17+ - #6245

Draft
markushi wants to merge 3 commits into
build/jdk-21from
markushi/feat/anr-warning-listener
Draft

markushi wants to merge 3 commits into
build/jdk-21from
markushi/feat/anr-warning-listener

Conversation

@markushi

@markushi markushi commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

📜 Description

On Android 17 (API 37) and above, AppLifecycleIntegration now subscribes to ActivityManager.registerAnrWarningListener. The system calls this listener while an ANR timeout is still running, before it declares the app unresponsive. Each warning becomes an app.anr breadcrumb (type: system, level: warning, message: ANR warning) with this data:

  • anr_type: the AnrTypes constant as a readable name (for example input_dispatch, broadcast_of_intent, start_foreground_service). An unknown value is kept as unknown (<n>).
  • anr_id, timeout_ms, consumed_ms
  • description: the description from the system. It is not set if the system gives an empty value.

Details:

  • The listener is registered only when enableAppLifecycleBreadcrumbs is enabled.
  • Registration is a binder call, so it runs on the SDK executor service, not on the thread that calls Sentry.init.
  • The callback runs on the binder thread that delivers it. It does not go to the main thread, because an ANR warning means the main thread is probably already blocked.
  • close() unregisters the listener. If the executor drops or rejects the unregister task, it runs inline. A register task that is still in the queue after close() does not register the listener.
  • AppLifecycleIntegration now takes (Context, BuildInfoProvider) in its constructor. The public no-arg constructor is kept as @Deprecated. With it, the ANR warning listener is not installed.

Depends on #6242 (JDK 21 build), which is the base branch of this PR.

💡 Motivation and Context

The ANR warning arrives before the ANR. If the app then crashes, records an error, or is killed for the ANR, the event breadcrumbs show that the main thread was already slow, and they show which ANR type and how much of the timeout was used.

💚 How did you test it?

  • Unit tests in AppLifecycleIntegrationTest (Robolectric, sdk = 37): breadcrumb mapping, unknown ANR types, listener not registered when lifecycle breadcrumbs are disabled or below API 37, registration failure, double close, close while the register task is still in the queue, and a dropped or rejected executor task.
  • UI test AnrWarningBreadcrumbTest (API 37 only), verified on an API 37 device: makes service_start_foreground_timeout_ms shorter with device_config and starts SlowStartService, which never calls startForeground(). Then it checks the app.anr breadcrumb in beforeBreadcrumb. The main thread does not block.

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.
  • Public API changes reviewed by another Mobile SDK team member or implemented according to the develop docs spec.

🔮 Next steps

  • Docs: add the app.anr breadcrumb to the Android breadcrumbs page.

🤖 Generated with Claude Code

@linear-code

linear-code Bot commented Oct 12, 2026

Copy link
Copy Markdown

JAVA-771

@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 18a3504

@sentry

sentry Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.59.0 (1) release

⚙️ sentry-android Build Distribution Settings

This branch has not been deployed

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

1 participant