NoSuchMethodError: java.lang.Long.hashCode

前端 未结 4 2125
暗喜
暗喜 2020-12-15 18:28

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

var _id = Random().nextLong()

override fun getId() = _id         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-15 18:31

    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.

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

提交回复
热议问题