Mobile-payment-sdk crashing the Android POS App by detecting Star Usb Printer

Greetings,

Our POS App using your mobile-payment-sdk-2.0.2 version. Everything is working fine except the following Manifest’s code of yours:

<activity
            android:name="com.squareup.usb.UsbAttachedActivity"
            android:enabled="false"
            android:exported="false"
            android:theme="@style/Theme.Translucent" >
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            </intent-filter>

            <meta-data
                android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                android:resource="@xml/usb_device_filter" />
        </activity>

When our app is on foreground and we connects the Star thermal usb printer… then your USB_DEVICE_ATTACHED intent filter called up and it’s crash our application. Following is the Crash Error for your reference:

 FATAL EXCEPTION: main
                 Process: insane.element.foodielandpos, PID: 32698
                 java.lang.NullPointerException: null cannot be cast to non-null type com.squareup.appengine.selection.AppEngineStateReader
                 	at com.squareup.appengine.selection.AppEngineStateReader$Companion.fromSystemService(AppEngineStateReader.kt:28)
                 	at com.squareup.usb.UsbAttachedActivity$onCreate$1.invokeSuspend(UsbAttachedActivity.kt:38)
                 	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                 	at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:363)
                 	at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:26)
                 	at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:21)
                 	at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:88)
                 	at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:123)
                 	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:52)
                 	at kotlinx.coroutines.BuildersKt.launch(Unknown Source:1)
                 	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:43)
                 	at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source:1)
                 	at com.squareup.usb.UsbAttachedActivity.onCreate(UsbAttachedActivity.kt:37)
                 	at android.app.Activity.performCreate(Activity.java:8995)
                 	at android.app.Activity.performCreate(Activity.java:8964)
                 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1456)
                 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4146)
                 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4322)

Please provide a fix for this ASAP. thanks!

:wave: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Find your Android Application Certificate Fingerprint
Build on Android: In-App Payments SDK
Reader SDK Quickstart

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

From what I gather from the error message something is returning null which is causing the crash.

at com.squareup.appengine.selection.AppEngineStateReader$Companion.fromSystemService(AppEngineStateReader.kt:28): This line indicates where the error occurred in the source code. It happened in the fromSystemService method of AppEngineStateReader.kt at line 28.
at com.squareup.usb.UsbAttachedActivity$onCreate$1.invokeSuspend(UsbAttachedActivity.kt:38): This line shows that the error was triggered during the execution of a coroutine in the onCreate method of UsbAttachedActivity.kt at line 38.

To fix this issue, you should check the code at AppEngineStateReader.kt line 28 and UsbAttachedActivity.kt line 38. Ensure that any object being cast is not null before performing the cast. You might need to add a null check or use Kotlin’s safe casting operator (as?) or the Elvis operator (?:) to handle potential null values gracefully. :slight_smile:

hi @Bryan-Square thanks for the reply…
But for kind your information both the classes i.e. “AppEngineStateReader.kt” & “UsbAttachedActivity.kt” are not implemented in our App’s Code! Both are from you " mobile-payments-sdk"

Kindly look at the screen shot:

And the package name of Highlighted Activity.

After removing your SDK from our app all working great! but when we add again the same problem will start happening… Something is wrong with your SDK & it’s Files…

Kindly forward these concerns to your Developers & ask them to check the null pointer exception in your Mentioned Classes: i.e.: “AppEngineStateReader.kt” & “UsbAttachedActivity.kt”

Thanks!