Search code examples
androidkotlinkotlinx.coroutines

Kotlin: ArrayIndexOutOfBoundsException during converting DispatchTask to string


We have released our first Kotlin pieces into production and we are getting crashes with this stacktrace:

Fatal Exception: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
       at kotlin.jvm.internal.ReflectionFactory.renderLambdaToString(ReflectionFactory.java:47)
       at kotlin.jvm.internal.Reflection.renderLambdaToString(Reflection.java:80)
       at kotlin.jvm.internal.Lambda.toString(Lambda.kt:22)
       at java.lang.StringBuilder.append(StringBuilder.java:202)
       at kotlinx.coroutines.experimental.DispatchedContinuation.toString(CoroutineDispatcher.kt:192)
       at java.lang.StringBuilder.append(StringBuilder.java:202)
       at kotlinx.coroutines.experimental.DispatchTask.toString(CoroutineDispatcher.kt:124)
       at java.lang.StringBuilder.append(StringBuilder.java:202)
       at android.os.Looper.loop(Looper.java:160)
       at android.app.ActivityThread.main(ActivityThread.java:5637)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Google show us this is happening in 100 % on Android 5. Crashlytics/Fabric shows also two crashes from Android 4.4. No crashes from newer Android versions.

I have no idea how to prevent this crash or how to replicate it.

Current version:

  • org.jetbrains.kotlinx:kotlinx-coroutines-core:0.16

Solution

  • That is a bug in Kotlin reflection. Meanwhile, I'd suggest to upgrade to the latest version of kotlinx.coroutines library (you can find what is the latest version here). Since version 0.17 DispatchTask.toString does not try to covert lambda to string anymore. It's toString implementation is actually more useful now and has more relevant information for debugging purposes.