Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

android - NoSuchMethodError: java.lang.Long.hashCode

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...