Leanplum / Leanplum-Android-SDK

Leanplum's integrated solution delivers meaningful engagement across messaging and the in-app experience.
https://www.leanplum.com
Apache License 2.0
46 stars 40 forks source link

Fatal Exception: java.lang.NoClassDefFoundError: com.leanplum.internal.APIConfig #532

Closed galacticappster04 closed 1 year ago

galacticappster04 commented 1 year ago

Expected Behavior

Actual Behavior

Fatal Exception: java.lang.NoClassDefFoundError: com.leanplum.internal.APIConfig
       at com.leanplum.internal.APIConfig.getInstance(APIConfig.java:59)
       at com.leanplum.internal.RequestSender.createArgsDictionary(RequestSender.java:211)
       at com.leanplum.internal.RequestSender.saveRequest(RequestSender.java:82)
       at com.leanplum.internal.RequestSender.sendSync(RequestSender.java:243)
       at com.leanplum.internal.RequestSender.access$000(RequestSender.java:37)
       at com.leanplum.internal.RequestSender$1.run(RequestSender.java:230)
       at android.os.Handler.handleCallback(Handler.java:938)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:268)
       at android.os.HandlerThread.run(HandlerThread.java:67)
Caused by java.lang.ExceptionInInitializerError:
       at com.leanplum.internal.APIConfig.getInstance(APIConfig.java:59)
       at com.leanplum.Leanplum.setAppIdForProductionMode(Leanplum.java:258)
       at com.leanplum.segment.LeanplumIntegration.<init>(LeanplumIntegration.java:65)
       at com.leanplum.segment.LeanplumIntegration$1.create(LeanplumIntegration.java:44)
       at com.segment.analytics.Analytics.performInitializeIntegrations(Analytics.java:1624)
       at com.segment.analytics.Analytics$2$1.run(Analytics.java:333)
       at android.os.Handler.handleCallback(Handler.java:883)
       at android.os.Handler.dispatchMessage(Handler.java:100)
       at android.os.Looper.loop(Looper.java:237)
       at android.app.ActivityThread.main(ActivityThread.java:7814)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
       at com.leanplum.internal.APIConfig.load(APIConfig.java:106)
       at com.leanplum.internal.APIConfig.<init>(APIConfig.java:55)
       at com.leanplum.internal.APIConfig.<clinit>(APIConfig.java:34)
       at com.leanplum.internal.APIConfig.getInstance(APIConfig.java:59)
       at com.leanplum.Leanplum.setAppIdForProductionMode(Leanplum.java:258)
       at com.leanplum.segment.LeanplumIntegration.<init>(LeanplumIntegration.java:65)
       at com.leanplum.segment.LeanplumIntegration$1.create(LeanplumIntegration.java:44)
       at com.segment.analytics.Analytics.performInitializeIntegrations(Analytics.java:1624)
       at com.segment.analytics.Analytics$2$1.run(Analytics.java:333)
       at android.os.Handler.handleCallback(Handler.java:883)
       at android.os.Handler.dispatchMessage(Handler.java:100)
       at android.os.Looper.loop(Looper.java:237)
       at android.app.ActivityThread.main(ActivityThread.java:7814)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)

Steps to Reproduce the Problem

  1. No discernible reproduction steps as this is happens 100% in the background

Specifications

hborisoff commented 1 year ago

@neonwarge04 I'm not sure why this error happens. Both classes RequestSender and APIConfig are located in the same module, so it is not because of mismatched versions. How often does this error occur and is it on some specific devices?

galacticappster04 commented 1 year ago

By a lot @hborisoff, at least in our books. We are getting 247 crashes on 42 users.

Here are the stats

Devices

Operating System 36% Android 11 29% Android 10 13% Android 12 10% Android 8 12% Other

Device States

We have plans to update 5.10.4 -> 6.0.0 and 6.0.0 to 7.0.0 but we are still not sure if this will fix the problem. We are working on the migration but this is a blind fix.

Any input is appreciated.

galacticappster04 commented 1 year ago

Let me know if you need more info. I updated op with all the crash stack trace. If you notice the following:

Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
       at com.leanplum.internal.APIConfig.load(APIConfig.java:106)
       at com.leanplum.internal.APIConfig.<init>(APIConfig.java:55)
       at com.leanplum.internal.APIConfig.<clinit>(APIConfig.java:34)
       at com.leanplum.internal.APIConfig.getInstance(APIConfig.java:59)
       at com.leanplum.Leanplum.setAppIdForProductionMode(Leanplum.java:258)
       at com.leanplum.segment.LeanplumIntegration.<init>(LeanplumIntegration.java:65)
       at com.leanplum.segment.LeanplumIntegration$1.create(LeanplumIntegration.java:44)
       at com.segment.analytics.Analytics.performInitializeIntegrations(Analytics.java:1624)
       at com.segment.analytics.Analytics$2$1.run(Analytics.java:333)
       at android.os.Handler.handleCallback(Handler.java:883)
       at android.os.Handler.dispatchMessage(Handler.java:100)
       at android.os.Looper.loop(Looper.java:237)
       at android.app.ActivityThread.main(ActivityThread.java:7814)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)

It appears for whatever reason SharedPreferences is null.

hborisoff commented 1 year ago

@neonwarge04 The NullPointerException happens because Leanplum context is not yet set. Can you call Leanplum.setApplicationContext as soon as possible in your Application's onCreate method? This issue is fixed with newer SDK versions.

Is it possible that the ExceptionInInitializerError is because of the NullPointerException?

About the NoClassDefFound, I can't connect it to anything else, because the APIConfig can't be removed from Proguard, as it is used in a lot of places.

galacticappster04 commented 1 year ago

Hello @hborisoff , thanks for the reply, we do already call this Leanplum.setApplicationContext(this) just before Leanplum.start. Also take note we have Segment integration as well and undergoes initialization. Can you please tell us how do we order the initialization?

Is there anything else I missed in the documentation regarding the proguard rule? This one is placed in our proguard-rules.pro:

########################################
## LeanPlum
########################################
-keepclassmembers class *
{
  @com.leanplum.annotations.* <fields>;
}
-keep class com.leanplum.** { *; }
-dontwarn com.leanplum.**
########################################

Our crash count on this is climbing up and we are preparing for patch it appears upgrading leanplum is a good bet.

hborisoff commented 1 year ago

@neonwarge04 Sorry, I just realised that you have issues with the Segment SDK and not the Android SDK. It is a known issue and was fixed with latest version. The fix was introducing this line that was missing before setting the keys.

galacticappster04 commented 1 year ago

Thank you @hborisoff, I think we can close this now. I will reopen again if this shows up in our later version. Thank you!