customerio / customerio-android

This is the official Customer.io SDK for Android.
MIT License
11 stars 9 forks source link

UninitializedPropertyAccessException - lateinit property siteId has not been initialized #370

Open remi-ollivier opened 6 days ago

remi-ollivier commented 6 days ago

SDK version: 3.10.0

Environment: Production

Are logs available?

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{ng.com.fairmoney.fairmoney/io.customer.messaginginapp.gist.presentation.GistModalActivity}: kotlin.UninitializedPropertyAccessException: lateinit property siteId has not been initialized
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3013)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3148)
       at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
       at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1861)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:193)
       at android.app.ActivityThread.main(ActivityThread.java:6819)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)

Describe the bug App crashes when GistModalActivity is initiated

To Reproduce Happens on production and can not reproduce locally

Expected behavior No crash

Screenshots

Additional context

Happens after we updated the SDK from 3.8.1 to 3.10.0

Shahroz16 commented 6 days ago

Hey @remi-ollivier thank you for reaching out, can you please share how you are initializing the SDK? and is there a way to bypass that initialization due to any conditioning in your app?

The error mentions the field siteId is uninitialized while it gets initialized when you initialize the SDK along with in-app module in the Application class. So it should be the first thing that gets initialized.

remi-ollivier commented 6 days ago

Sure! We have a CustomerIOInitializer which works like this

private val customerIO: Deferred<CustomerIO?> =
        GlobalScope.async(Dispatchers.IO, start = CoroutineStart.LAZY) {
                CustomerIO.Builder(
                    siteId = BuildKonfig.CUSTOMER_IO_SITE_ID,
                    apiKey = BuildKonfig.CUSTOMER_IO_API_KEY,
                    appContext = application,
                )
                    .addCustomerIOModule(
                        ModuleMessagingPushFCM(
                            config = MessagingPushModuleConfig.Builder().apply {
                                setNotificationCallback(this@CustomerIOInitializer)
                                setRedirectDeepLinksToOtherApps(false)
                            }.build()
                        )
                    )
                    .addCustomerIOModule(ModuleMessagingInApp())
                    .build()
        }

        override val instance: Deferred<CustomerIO?> = customerIO

and we access it by calling

customerIOInitializer.instance?.await()

We need to access it asynchronously because the initialization time takes time on our main thread and it causes ANRs, so we try to unload the main thread. Please let me know if you need more information

Shahroz16 commented 3 days ago

@remi-ollivier I think late initialization could be the reason because we need to be synchronous. The issue of ANR in your case is also not helpful, but we are discussing this internally how to tackle this. We might make it configurable the first time launching of the in-app to pre-cache the assets which you are reporting as something that causes ANR.