Skip to content

Fix: Prevent crash when app is launched during automatic backup - #2221

Closed
d4rken wants to merge 2 commits into
mainfrom
fix/backup-receiver-crash-1274
Closed

Fix: Prevent crash when app is launched during automatic backup#2221
d4rken wants to merge 2 commits into
mainfrom
fix/backup-receiver-crash-1274

Conversation

@d4rken

@d4rken d4rken commented Feb 23, 2026

Copy link
Copy Markdown
Member

What changed

Fixed a crash that could happen when SD Maid was launched while Android's automatic backup was running. On Android 13 and below, this crash could also break subsequent app launches until the device was restarted.

Developer TLDR

  • Removed @AndroidEntryPoint from all 4 BroadcastReceivers (SchedulerRestoreReceiver, UnarchiveReceiver, UninstallWatcherReceiver, ExternalWatcherTaskReceiver)
  • Hilt's bytecode transformation injects super.onReceive() before any user code, making it impossible to guard with @AndroidEntryPoint. Switched to manual EntryPointAccessors.fromApplication() per Dagger team recommendation
  • Added Context.isValidHiltContext() helper in InjectionHelpers.kt and consolidated existing Service.isValidAndroidEntryPoint() to delegate to it
  • Each receiver validates intent/extras before DI lookup (cheap checks first), with try-catch defense-in-depth around EntryPointAccessors
  • Dropped unused CorpseFinderSettings injection from ExternalWatcherTaskReceiver
  • Added BroadcastReceiverBackupGuardTest regression test verifying all 4 receivers silently return with a restricted context

Background & research

Root cause

When Android Auto Backup runs, it invokes registered BroadcastReceivers with a RestrictedContext whose applicationContext is a plain android.app.Application — not our @HiltAndroidApp App. Hilt's Gradle plugin performs bytecode transformation on @AndroidEntryPoint receivers, inserting a super.onReceive() call at the very beginning of onReceive() — before any user code. This makes it impossible to add a guard check while using @AndroidEntryPoint.

On Android <= 13, there's a platform bug (Google issue #160946170) where crashing during backup causes the "restricted" flag to persist, breaking subsequent app launches until the device is restarted.

Why EntryPointAccessors instead of alternatives

Approach Verdict
Keep @AndroidEntryPoint + add guard in onReceive Not possible — Hilt bytecode transformation injects super.onReceive() before our code
android:allowBackup="false" Works but users lose backup/restore entirely
Custom BackupAgent Significant extra work, doesn't prevent the receiver from being invoked
Remove @AndroidEntryPoint + manual EntryPointAccessors Dagger team's recommended approach (#2798)

Known tradeoff

With @AndroidEntryPoint, adding a dependency was just @Inject lateinit var foo: Foo. Now someone needs to also add the method to the ReceiverEntryPoint interface and call it manually. Forgetting the interface method is a compile error (Dagger can't find the binding), so it's not silent — just more friction.

Related issues

Closes #1274

@d4rken d4rken added bug Something isn't working as expected c: Scheduler labels Feb 23, 2026
Remove @androidentrypoint from all 4 BroadcastReceivers and use
manual EntryPointAccessors with an isValidHiltContext() guard.

During Android Auto Backup, receivers get a RestrictedContext whose
applicationContext is a plain Application (not @HiltAndroidApp),
causing Hilt injection to crash. On Android <= 13, this crash can
poison subsequent app launches.

Closes #1274
@d4rken
d4rken force-pushed the fix/backup-receiver-crash-1274 branch from 544bc7e to c584fea Compare March 9, 2026 15:16
BroadcastReceiverBackupGuardTest referenced internal constants across module boundaries, causing compilation failures in both testFoss and testGplay CI jobs.
@d4rken d4rken closed this Apr 13, 2026
@d4rken
d4rken deleted the fix/backup-receiver-crash-1274 branch July 23, 2026 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected c: Scheduler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Background executionmay become unreliable if SD Maid was triggered while being backed up by "Auto Backup"

1 participant