Skip to content

fix(android_alarm_manager_plus): catch NPE in AlarmService#3924

Open
HedAurabesh wants to merge 1 commit into
fluttercommunity:mainfrom
HedAurabesh:main
Open

fix(android_alarm_manager_plus): catch NPE in AlarmService#3924
HedAurabesh wants to merge 1 commit into
fluttercommunity:mainfrom
HedAurabesh:main

Conversation

@HedAurabesh

@HedAurabesh HedAurabesh commented Jul 10, 2026

Copy link
Copy Markdown

Description

On Android 11 phones with aggressive battery optimizations (e.g. Huawei and OnePlus), the app might be started in a sandboxed mode when receiving the reboot event.

In that sandbox, the resolution of the app's launch class name might fail with a NullPointerException, which in turn leads to a crash.

This fix avoids the crash, turning it into an error-log message instead.

The change is motivated by the following crash stacktrace received via Crashlytics for several Huawei and OnePlus phones on Android 11:

Fatal Exception: java.lang.RuntimeException: Unable to start receiver dev.fluttercommunity.plus.androidalarmmanager.RebootBroadcastReceiver: java.lang.NullPointerException: class name is null
       at android.app.ActivityThread.handleReceiver(ActivityThread.java:4047)
       at android.app.ActivityThread.access$1400(ActivityThread.java:239)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1926)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:223)
       [...]

Caused by java.lang.NullPointerException: class name is null
       at android.content.ComponentName.<init>(ComponentName.java:103)
       at android.content.Intent.setClassName(Intent.java:10044)
       at android.app.ApplicationPackageManager.getLaunchIntentForPackage(ApplicationPackageManager.java:252)
       at dev.fluttercommunity.plus.androidalarmmanager.AlarmService.createShowPendingIntent(AlarmService.java:362)
       at dev.fluttercommunity.plus.androidalarmmanager.AlarmService.scheduleAlarm(AlarmService.java:151)
       at dev.fluttercommunity.plus.androidalarmmanager.AlarmService.reschedulePersistentAlarms(AlarmService.java:338)
       at dev.fluttercommunity.plus.androidalarmmanager.RebootBroadcastReceiver.onReceive(RebootBroadcastReceiver.java:37)
       at android.app.ActivityThread.handleReceiver(ActivityThread.java:4038)
       [...]

Related Issues

No related issues

Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I titled the PR using Conventional Commits.
  • I did not modify the CHANGELOG.md nor the plugin version in pubspec.yaml files.
  • All existing and new tests are passing.
  • The analyzer (flutter analyze) does not report any problems on my PR.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • No, this is not a breaking change.

…ervice

On Android 11 phones with aggressive battery optimizations (e.g. Huawei and
OnePlus), the app might be started in a sandboxed mode when receiving the reboot
event.

In that sandbox, the resolution of the app's launch class name might fail with
a NullPointerException, which in turn leads to a crash.

Here is an example stack trace:
---------------------------------------------------------------------------
Fatal Exception: java.lang.RuntimeException: Unable to start receiver dev.fluttercommunity.plus.androidalarmmanager.RebootBroadcastReceiver: java.lang.NullPointerException: class name is null
       at android.app.ActivityThread.handleReceiver(ActivityThread.java:4047)
       at android.app.ActivityThread.access$1400(ActivityThread.java:239)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1926)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:223)
       [...]

Caused by java.lang.NullPointerException: class name is null
       at android.content.ComponentName.<init>(ComponentName.java:103)
       at android.content.Intent.setClassName(Intent.java:10044)
       at android.app.ApplicationPackageManager.getLaunchIntentForPackage(ApplicationPackageManager.java:252)
       at dev.fluttercommunity.plus.androidalarmmanager.AlarmService.createShowPendingIntent(AlarmService.java:362)
       at dev.fluttercommunity.plus.androidalarmmanager.AlarmService.scheduleAlarm(AlarmService.java:151)
       at dev.fluttercommunity.plus.androidalarmmanager.AlarmService.reschedulePersistentAlarms(AlarmService.java:338)
       at dev.fluttercommunity.plus.androidalarmmanager.RebootBroadcastReceiver.onReceive(RebootBroadcastReceiver.java:37)
       at android.app.ActivityThread.handleReceiver(ActivityThread.java:4038)
       [...]
---------------------------------------------------------------------------

This fix avoids the crash, turning it into a error-log message instead.
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