Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch to Kotlin 1.3.30 breaks hashCode only on Android API 21

I switched over my Android project from 1.3.20 to 1.3.30 and I am suddenly getting following exceptions from some of the classes when they call hashCode() method. Note, I am getting this exception only on Android platform API 21 & 22, beyond Android API 24, everything works fine.

Caused by: java.lang.NoSuchMethodError: No static method hashCode(J)I in class Ljava/lang/Long; or its super classes (declaration of 'java.lang.Long' appears in /system/framework/core-libart.jar)

There is this thread that mentions similar symptoms but that was for Kotlin 1.2, I am wondering if anybody is encountering similar situation and if there is a workaround for this?

like image 619
Subodh Nijsure Avatar asked Apr 16 '19 13:04

Subodh Nijsure


People also ask

What's new in Kotlin 1. 3?

In 1.3, we've completely reworked the model of multiplatform projects in order to improve expressiveness and flexibility, and to make sharing common code easier. Also, Kotlin/Native is now supported as one of the targets!

How to upgrade Kotlin version?

Update to a new release IntelliJ IDEA and Android Studio suggest updating to a new release once it is out. When you accept the suggestion, it automatically updates the Kotlin plugin to the new version. You can check the Kotlin version in Tools | Kotlin | Configure Kotlin Plugin Updates.

In which file do we change the version of Kotlin in Android app?

jetbrains. kotlin:kotlin-gradle-plugin:$kotlin_version" . So, change ext. kotlin_version to be the value that you want.

What is Kotlin latest version?

Kotlin 1.5 was released in May 2021. Kotlin 1.6 was released in November 2021. Kotlin 1.7 was released in June 2022, including the alpha version of the new Kotlin K2 compiler.


1 Answers

As an alternative, you may set a JVM target for Kotlin compilation to "1.6", as Kotlin 1.3.30 has started to infer the JVM target from the Java version in android.compileOptions and chooses "1.8" if both sourceCompatibility and targetCompatibility are set to that or higher.

In the module's build.gradle, add:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        jvmTarget = '1.6'
    }
}

There will be no need to do this once the issue in D8 desugaring is fixed.

This issue is tracked in the Kotlin issue tracker as KT-31027.

like image 51
hotkey Avatar answered Jan 20 '23 23:01

hotkey