22

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:

FATAL EXCEPTION: main
java.lang.NoSuchMethodError: java.lang.Long.hashCode
   at com.company.ormlite.AbstractORM.hashCode(AbstractORM.kt:271)
   at java.util.HashMap.put(HashMap.java:390)
   at java.util.HashSet.add(HashSet.java:95)
   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 com.company.android.tna.orm.DataManager.getTables(DataManager.kt:16)
   at com.company.android.tna.orm.DataManager.getTables(DataManager.kt:10)
   at com.company.android.core.utils.AbstractDataManager.create(AbstractDataManager.kt:25)
   at com.company.android.core.utils.AbstractDataManager.start(AbstractDataManager.kt:44)
   at com.company.android.core.utils.AbstractZKApplication.onCreate(AbstractZKApplication.kt:54)
   at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999)
   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4151)
   at android.app.ActivityThread.access$1300(ActivityThread.java:130)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4745)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
   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.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • can you post signature of `getId()` method along with imports? – Sachin Chandil Aug 29 '17 at 09:55
  • @chandil03 I've added requested information. There are no imports relevant to these methods. All imports are from internal libraries for ORM and java.util. We have no other implementation of `Long`. – m0skit0 Aug 29 '17 at 09:59
  • I used your three lines, it is working fine. I did not override getId as i did not know which interface you are using. If you let me know the interface that you are implementing then i might be able to help. – Sachin Chandil Aug 29 '17 at 10:33
  • @chandil03 Yeah it worked fine for me until yesterday, so it must be definitely something in my build messing it. I don't think the interface is related at all. It's just a simple interface with that method getId(), nothing more. – m0skit0 Aug 29 '17 at 10:35
  • 2
    Also upgrading kotlin from 1.3.20 to 1.3.30 causes this issue. See [this](https://youtrack.jetbrains.com/issue/KT-31027). – galcyurio Apr 24 '19 at 01:53

4 Answers4

19

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

getId().hashCode()

Kotlin 1.1.2 generates the following code:

Long.valueOf(this.getId()).hashCode()

while newer versions of Kotlin generate this other code:

Long.hashCode(this.getId())

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.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
5

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
marcinP
  • 106
  • 1
  • 4
  • much better solution from just dropping support to old APIs – Ahmed na Apr 16 '19 at 04:10
  • If you have Kotlin to compile to 1.6 you don't need this. – m0skit0 Apr 17 '19 at 13:42
  • @Ahmedna What do you mean "just dropping support to old APIs"? – m0skit0 Apr 18 '19 at 21:25
  • 1
    @m0skit0 with kotlin v 1.3.30 launched last week , while having 1.6 any device with api < 24 this error is thrown , grater APIs are working fine So i just need to avoid using these methods – Ahmed na Apr 19 '19 at 02:54
  • @Ahmedna Did you read my answer? You just have to set the Kotlin compiler to generate 1.6 compatible code. Doesn't matter which API or Kotlin version. – m0skit0 Apr 19 '19 at 09:06
  • 1
    @m0skit0 I've set complier to 1.6 everything was good until last week , I updated to kotlin 1.3.30 , all users with api < 24 started to crashed , I debugged with 1.6 and older version of kotlin all api were working , then with kotlin 1.3.30 all APIs pre 24 crashed – Ahmed na Apr 19 '19 at 15:24
  • Targetting JVM to v1.6 is not mandatory. Anyway, thank you, it works. – Rubén Viguera Sep 13 '19 at 08:34
3

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.

Alex Romanov
  • 11,453
  • 6
  • 48
  • 51
3

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.

Carter Chen
  • 792
  • 1
  • 8
  • 22