NoClassDefFoundError when posting a Runnable to Handler


#1

I have a fairly simple block of code to clear a notification from the UI:

fun markNotificationRead(notificationId: String) {
    mainHandler.post {
        if (notifications.removeIf { it.id == notificationId }) {
            notificationLiveData.value = notifications
        }
    }
}

I can’t reproduce this on my device but I get rare crashes on Firebase saying:
NoClassDefFoundError: com.example.service.WebSocket$markNotificationRead$1$1
com.example.service.WebSocket$markNotificationRead$1.run

It looks like $1.run is throwing the exception? Is my lambda not being added to the dex?


#2

Hey,
I have the same problems.

var payloads = mutableListOf<Payload>()
override fun onObjectRemoved(object: MyClass) {
    payloads.removeIf { it. objectId == object. objectId }
    onPayloadUpdate(dayOfYear = dayOfYear)
}

The same issue is with replaceAll

It’s okay on Android 7, but 100% crashes on devices/emulators with Android 5/Android 6 .


#3

Look at the [documentation](https://developer.android.com/reference/java/util/ArrayList.html#removeIf(java.util.function.Predicate<? super E>), removeIf was added to java 8 and in android was only available from api level 24.


#4

Thanks! Adding the Guava library and using it instead did the trick. I was using the kotlin implementation of the array and assumed that they provided the 1.8 features in the library.