Skip to main content

App crashes on Android when making a purchase with Kotlin Multiplatform SDK

  • December 13, 2024
  • 7 replies
  • 44 views

Forum|alt.badge.img

There are crashes when purchasing on Android when using Kotlin MultiPlatform SDK. It’s not happen with all devices, but some devices. Below is the stacktrace

 

       

  Fatal Exception: java.lang.IllegalStateException: There's no current Activity.
       at com.revenuecat.purchases.kmp.di.AndroidProviderKt.requireActivity(AndroidProvider.kt:83)
       at com.revenuecat.purchases.kmp.Purchases.purchase(Purchases.android.kt:236)
       at com.revenuecat.purchases.kmp.Purchases.purchase$default(Purchases.android.kt:227)
       at com.chipmango.kmp.revenuecat.RevenueCat.makePurchase(RevenueCat.kt:152)
       at shared.presentation.paywall.PaywallViewModel.purchase(PaywallViewModel.kt:108)
       at shared.presentation.paywall.ScreenPaywall2Kt$PaywallContent$1$2$1$1.invoke$lambda$12$lambda$11$lambda$10(ScreenPaywall2.kt:188)
       at shared.presentation.paywall.CTAButtonKt$CTAButton$1$2$1$1.invokeSuspend(CTAButton.kt:91)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
       at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:102)
       at androidx.compose.ui.platform.AndroidUiDispatcher.performTrampolineDispatch(AndroidUiDispatcher.android.kt:81)
       at androidx.compose.ui.platform.AndroidUiDispatcher.access$setScheduledFrameDispatch$p(AndroidUiDispatcher.android.kt)
       at androidx.compose.ui.platform.AndroidUiDispatcher.access$performTrampolineDispatch(AndroidUiDispatcher.android.kt)
       at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.run(AndroidUiDispatcher.android.kt:57)
       at android.os.Handler.handleCallback(Handler.java:942)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loopOnce(Looper.java:201)
       at android.os.Looper.loop(Looper.java:288)
       at android.app.ActivityThread.main(ActivityThread.java:8061)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:703)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:923)
        

 

This post has been closed for comments

7 replies

JayShortway
RevenueCat Staff
Forum|alt.badge.img+5
  • RevenueCat Staff
  • 38 replies
  • December 13, 2024

Hi ​@tiendung717, thanks for reporting this! 

Do you happen to have a section in your AndroidManifest.xml file that looks something like this?

 <provider
    android:name="androidx.startup.InitializationProvider"
    android:authorities="${applicationId}.androidx-startup"
    tools:node="remove">
 </provider>

If so, could you share that snippet? Thanks in advance! 


Forum|alt.badge.img
  • Author
  • New Member
  • 3 replies
  • December 15, 2024
        <provider
            android:name="androidx.startup.InitializationProvider"
            android:authorities="${applicationId}.androidx-startup"
            android:exported="false"
            tools:node="merge">
            <meta-data
                android:name="androidx.work.WorkManagerInitializer"
                android:value="androidx.startup"
                tools:node="remove" />
        </provider>

Yes, I have added this code to AndroidManifest.xml. Please have a look on above snippet.


JayShortway
RevenueCat Staff
Forum|alt.badge.img+5
  • RevenueCat Staff
  • 38 replies
  • December 16, 2024

Thanks for providing the snippet. That looks okay at first glance.

I'm assuming this crash was reported by Crashlytics or a similar service. Do you have any more information on the context of the crash? 

  • Do you have breadcrumb logs?
  • Can you see the distribution of devices this crash occurs on?
  • Can you see the distribution of Android versions?
  • Can you see the distribution of foreground vs background crashes?
  • Can you share a code snippet of how you're calling the RevenueCat SDK?

I'm hoping that this information can point us in the right direction. Thanks in advance!


Forum|alt.badge.img
  • Author
  • New Member
  • 3 replies
  • December 19, 2024

Thank you for your follow-up and questions.  

I’ve attached the stack trace and breadcrumb logs for your reference. The crashes occurred on the following devices:  
- Realme C51 (Android 13)  
- Redmi Note 12 (Android 14)  

While it’s possible that the crash may affect additional devices, I had to roll back the release after observing these crashes. For now, I’ve only identified two instances on these devices.  

Regarding foreground vs. background, I’m not entirely certain; however, based on the breadcrumb logs, it appears the crashes occurred when the app was in the foreground.  

As for the code snippet:
I call the RevenueCat SDK’s purchase function (Purchases.sharedInstance.purchase()) when the user clicks on the CTA button. This triggers the PaywallViewModel.purchase() function, where the RevenueCat purchase function is invoked. You can find further details in the attached files.

P/S: I’m using version 1.1.0+13.3.0 of RevenueCat SDK for Kotlin Multiplatform


JayShortway
RevenueCat Staff
Forum|alt.badge.img+5
  • RevenueCat Staff
  • 38 replies
  • December 19, 2024

Thanks a lot for providing all that information! That's very helpful. 

I noticed that CTAButton is calling purchase() from a suspend function. That leads me to the hypothesis that these users might have backgrounded the app, right after clicking the button.

One thing you could try is to make the call to purchase() synchronously. That way the Activity can't go away before the purchase flow has started. 

Let me know if you have more questions! 


Forum|alt.badge.img
  • Author
  • New Member
  • 3 replies
  • December 19, 2024

Thank you for the insight and hypothesis. 

I’ll update the implementation to ensure the call to purchase() is made synchronously. I’ll test this change and monitor the results in Crashlytics for any improvements.

It may take 7-10 days to get a result. If I run into further issues or have more questions, I’ll reach out.

Thanks again for your support!


JayShortway
RevenueCat Staff
Forum|alt.badge.img+5
  • RevenueCat Staff
  • 38 replies
  • December 19, 2024

Thank you, and good luck! 🤞


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings