# forEach on a LinkedHashMap on Android (Samsung s4, s5 devices) + Kotlin + Multidex -\> throws NoClassDefFoundError

**URL:** <https://discuss.kotlinlang.org/t/foreach-on-a-linkedhashmap-on-android-samsung-s4-s5-devices-kotlin-multidex-throws-noclassdeffounderror/8088>\
**Category:** Android\
**Created:** [June 8, 2018, 4:52pm UTC](https://discuss.kotlinlang.org/t/foreach-on-a-linkedhashmap-on-android-samsung-s4-s5-devices-kotlin-multidex-throws-noclassdeffounderror/8088 "2018-06-08T16:52:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![petros.paraskevopoulos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/petros.paraskevopoulos/32/3038_2.png) [@petros.paraskevopoulos](https://discuss.kotlinlang.org/u/petros.paraskevopoulos)\
**Post date:** [June 8, 2018, 4:52pm UTC](https://discuss.kotlinlang.org/t/foreach-on-a-linkedhashmap-on-android-samsung-s4-s5-devices-kotlin-multidex-throws-noclassdeffounderror/8088/1 "2018-06-08T16:52:22Z")

</div>

I just got an NoClassDefFoundError for trying to use a forEach on a LinkedHashMap with Android + Kotlin + Multidex on Samsung s4 and s5 devices.

Any idea what the problem is?

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [June 9, 2018, 3:13am UTC](https://discuss.kotlinlang.org/t/foreach-on-a-linkedhashmap-on-android-samsung-s4-s5-devices-kotlin-multidex-throws-noclassdeffounderror/8088/2 "2018-06-09T03:13:47Z")

</div>

If your lambda takes 2 parameters then it may be this problem: [android - java.lang.NoClassDefFoundError $$inlined$forEach$lambda$1 in Kotlin - Stack Overflow](https://stackoverflow.com/questions/42869086/java-lang-noclassdeffounderror-inlinedforeachlambda1-in-kotlin/42869245)

---

<div class="post-metadata">

**Author:** ![petros.paraskevopoulos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/petros.paraskevopoulos/32/3038_2.png) [@petros.paraskevopoulos](https://discuss.kotlinlang.org/u/petros.paraskevopoulos)\
**Post date:** [June 9, 2018, 6:00pm UTC](https://discuss.kotlinlang.org/t/foreach-on-a-linkedhashmap-on-android-samsung-s4-s5-devices-kotlin-multidex-throws-noclassdeffounderror/8088/3 "2018-06-09T18:00:13Z")

</div>

Thanks for the reply Ilya!

Yes, it is probably related to something similar, my lambda is exactly as:

```auto
val links = linkedMapOf<LinkEntity, EntityRange>()
...
links.forEach { link, range -> url(link, range) }

```

I understand that this code cannot run on older version of Android, but I thought Kotlin would have solved this problem underneath. My biggest worry is now to create a custom static (Lint or otherwise) warning to force developers from using such a call when working with a LinkedHashMap, which is unfortunately not ideal.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [June 10, 2018, 12:41am UTC](https://discuss.kotlinlang.org/t/foreach-on-a-linkedhashmap-on-android-samsung-s4-s5-devices-kotlin-multidex-throws-noclassdeffounderror/8088/4 "2018-06-10T00:41:00Z")

</div>

There’s already such a Lint warning (“Android | Lint | Correctness | Calling new methods on older versions”), however it didn’t work reliably until recently and only was fixed in coming Android Studio 3.2

---

<div class="post-metadata">

**Author:** ![petros.paraskevopoulos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/petros.paraskevopoulos/32/3038_2.png) [@petros.paraskevopoulos](https://discuss.kotlinlang.org/u/petros.paraskevopoulos)\
**Post date:** [June 10, 2018, 4:18pm UTC](https://discuss.kotlinlang.org/t/foreach-on-a-linkedhashmap-on-android-samsung-s4-s5-devices-kotlin-multidex-throws-noclassdeffounderror/8088/5 "2018-06-10T16:18:59Z")

</div>

Oh my, it works on Android Studio 3.2, you’re right, thank you for the tip Ilya!

```auto
Call requires API level 24 (current min is 21): java.util.LinkedHashMap#forEach ...

```

However, it is crazy (and kind of sad) that this Lint check is not reliable in earlier versions of Android Studio. I am aware of Lint not working as expected from the command line and the UAST bug (supposedly fixed with 3.1), but I thought Android Studio 3 was working just fine from the IDE itself, very interesting…

THANK YOU

---

<div class="post-metadata">

**Author:** ![petros.paraskevopoulos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/petros.paraskevopoulos/32/3038_2.png) [@petros.paraskevopoulos](https://discuss.kotlinlang.org/u/petros.paraskevopoulos)\
**Post date:** [June 10, 2018, 4:26pm UTC](https://discuss.kotlinlang.org/t/foreach-on-a-linkedhashmap-on-android-samsung-s4-s5-devices-kotlin-multidex-throws-noclassdeffounderror/8088/6 "2018-06-10T16:26:22Z")

</div>

One last question, if you’re so kind as to answer.

Since linkedMapOf() is kind of Kotlin (kotlin-stdlib), and Kotlin promises to solve this kind of language barrier versioning problem on Android, why does it fail on this instance?

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [June 10, 2018, 4:39pm UTC](https://discuss.kotlinlang.org/t/foreach-on-a-linkedhashmap-on-android-samsung-s4-s5-devices-kotlin-multidex-throws-noclassdeffounderror/8088/7 "2018-06-10T16:39:42Z")

</div>

You are calling a method from the Java 8 stdlib. Kotlin cannot make that method exist. But I am sure there is an analogous function in the Kotlin stdlib.

So, Kotlin can fix the problem for you. You just need to call the correct function. As a general rule, don’t use the Java stdlib API for collections. Always use the analogous Kotlin API.

---

<div class="post-metadata">

**Author:** ![petros.paraskevopoulos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/petros.paraskevopoulos/32/3038_2.png) [@petros.paraskevopoulos](https://discuss.kotlinlang.org/u/petros.paraskevopoulos)\
**Post date:** [June 10, 2018, 5:36pm UTC](https://discuss.kotlinlang.org/t/foreach-on-a-linkedhashmap-on-android-samsung-s4-s5-devices-kotlin-multidex-throws-noclassdeffounderror/8088/8 "2018-06-10T17:36:18Z")

</div>

Thanks for the respond, will keep that in mind.
