I have bunch of traces in Google Play where it seems that static members of application class are null.
All the related posts I found are about storing data, but these variables are not data, they are 3rd party SDK instances or other crucial objects which use singleton pattern along with the application.
Data used in Activity
classes are stored into database and no static references are used there.
These traces do not get reported to Fabric at all which is weird since it's initialised in Application
onCreate
method.
The trace indicates that static variable is null when FirebaseMessagingService
onMessageReceived
method is triggered, there I need to pass the message to 3rd party SDK but since the instance of that is null the app crashes.
Traces only seen with Android OS 6.0
Are there any solutions on how to deal with this OS version being so aggressive nulling static members?
Thanks.
Here is example minimum code of my usage:
Application class:
@Override
public void onCreate() {
Engine.init();
}
Engine class:
private static Engine sInstance;
private 3rdParty m3rdParty;
private Engine() {
m3rdParty = new 3rdParty();
}
public static void init() {
if (sInstance == null) {
sInstance = new Engine();
}
}
public static onMessageReceived(RemoteMessage remoteMessage) {
sInstance.m3rdParty.onMessageReceived(remoteMessage);
// Above line crashes with NPE, sEngine or m3rdParty is never set to null in code
}
Extended FirebaseMessagingService:
public void onMessageReceived(RemoteMessage remoteMessage) {
Engine.onMessageReceived(remoteMessage);
}
Are there any solutions on how to deal with this OS version being so aggressive nulling static members?
The "OS" is not "nulling static members". Most likely, your process is terminated, whether due to low memory conditions or user action (e.g., swiping your task off the overview screen). This is perfectly normal.
I say "most likely", because your question does not contain a minimal, complete, and verifiable example, with the stack trace and related source code, and so I cannot preclude other possibilities. For example, if you have code that assigns null
to those static
fields under certain conditions, perhaps those conditions are being met in cases that you are not expecting.