FirebaseApp.UserUnlockReceiver.unregister crashes
- Firebase Component: Common
- Component version: 20.4.2
Steps to reproduce:
Crashlytics notified us about a crash in FirebaseApp.UserUnlockReceiver.unregister. The method is only called from FirebaseApp.UserUnlockReceiver.onReceive which in turn only responds to android.intent.action.USER_UNLOCKED. I don't know how many times the system sends the broadcast but it apparently isn't just once.
Relevant Code:
https://github.com/firebase/firebase-android-sdk/blob/b77c2188e5ec7c53c2a955fc4663b301e6c98c83/firebase-common/src/main/java/com/google/firebase/FirebaseApp.java#L668-L680
FirebaseApp initialization may be idempotent but Context.unregisterReceiver isn't. The method crashes if the receiver isn't registered.
Suggested solution
public void unregister() {
try {
applicationContext.unregisterReceiver(this);
} catch (IllegalArgumentException ignore) {
// The receiver isn't registered.
}
}
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
Workaround
val context = object : ContextWrapper(this) {
override fun getApplicationContext(): ContextWrapper {
return this
}
override fun unregisterReceiver(receiver: BroadcastReceiver?) {
try {
super.unregisterReceiver(receiver)
} catch (_:IllegalArgumentException) {
}
}
}
FirebaseApp.initializeApp(context)
EDIT: Maybe it won't be that easy. I got this log
E/FirebaseSessions: Failed to register lifecycle callbacks, unexpected context class ...
Hi @consp1racy, thank you for reaching out. We appreciate you investigating the issue and providing the fix. I'll inform our engineers about this with your pull request.
Hi @consp1racy thanks for reaching out, and providing a fix! I'll review the code and run the tests required to include it in the codebase.