android – NoSuchMethodError: java.lang.Long.hashCode-ThrowExceptions

Exception or error:

I have the following override of method on hashCode in AbstractORM class:

var _id = Random().nextLong()

override fun getId() = _id // AbstractORM class implements an interface that defines this method getId()

override fun hashCode() = getId().hashCode()

which suddenly started to throw the following exception:

java.lang.NoSuchMethodError: java.lang.Long.hashCode
   at java.util.HashMap.put(
   at java.util.HashSet.add(
   at kotlin.collections.ArraysKt___ArraysKt.toCollection(_Arrays.kt:6518)
   at kotlin.collections.ArraysKt___ArraysKt.toSet(_Arrays.kt:6853)
   at kotlin.collections.SetsKt__SetsKt.setOf(Sets.kt:32)
   at android.os.Handler.dispatchMessage(
   at android.os.Looper.loop(
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(
   at dalvik.system.NativeStart.main(Native Method)

This has me dumbfounded for several reasons:

  • All classes in Java and Kotlin have hashCode method since it is inherited from Object or Any.
  • How can it not find a method that is on the Android SDK itself? If the SDK is not present, how is it running at all?
  • When inspecting that line of code in IntelliJ IDEA, it sends me to kotlin.Any.hashCode, not to java.lang.Long.hashcode.

Any insights would be greatly appreciated, thanks in advance.

How to solve:

After checking the compiled AbstractORM class I found the problem: newer Kotlin versions generate a different code for that line


Kotlin 1.1.2 generates the following code:


while newer versions of Kotlin generate this other code:


The problem is that this static method Long.hashCode(long) in Android is only available since API 24 (Android 7.0), while I’m testing on an Android device that has version 4.1 (API 16).

I’m temporarily fixing by calculating the hash code manually although I’ve opened an issue here.

override fun hashCode() = (getId() xor getId().ushr(32)).toInt()

As commented on the issue, switching to Java 1.6 target for the Kotlin compiler generates the old compatible code.

enter image description here

PS: I’m not 100% sure about those Kotlin versions, please take with a grain of salt.


Overriding the hashCode() function, as m0skit0 suggested, worked for me. I implemented this:

override fun hashCode() : Int = id.toString().hashCode()

My Kotlin compiler setting in AndroidStudio is :

  • incremental compilation enabled
  • target JVM ver. 1.6


I’m not sure about how to solve your problem, but I’ll try to explain why are you getting this exception.

In Java 8 a new static method was added to a Long class:

public static int hashCode(long value)

And since then hashCode() method looks like this:

public int hashCode() {
    return hashCode(this.value); // call Long.hashCode() static method

So it seems that you have some issues with Java version.


I’ve encountered the same issue and the solution for me is to assign both compileOptions and kotlinOptions in build.gradle to version 1.8

android {
  // Configure only for each module that uses Java 8
  // language features (either in its source code or
  // through dependencies).
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  // For Kotlin projects
  kotlinOptions {
    jvmTarget = "1.8"

Referenced from Use Java 8 language features of Android Developers official website.

Leave a Reply

Your email address will not be published. Required fields are marked *