Search code examples
androidkotlinjava-8butterknife

How to use Kotlin with Butterknife 10.1.0 on minsdk <24?


I am migrating my Android project to AndroidX libraries. Butterknife 10.x has improved support for it, so i think it is good to use that. My project is made in Kotlin (1.3.30).

Butterknife versions >8 require that you use Java 8 like this:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

This works great for minsdk >=24. And for a good part it works with lower versions too. But as Kotlin compiles to Java 8 some features dont work on minsdk <24. Such as the one that i am getting the following exception for.

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)

Long.hashCode() is a new api in Java 8, but not supported in older android versions. This question solves the same problem by telling the compiler to use Java 6, but that is unsupported with butterknife 10.x which i need for AndroidX.

I suppose this is quite a common issue, but i have not found a solution for it. How can i solve this?


Solution

  • The problem has to do with using Kotlin 1.3.30 (see This issue). I have temporarily fixed the issue by using Kotlin 1.3.21. In a future version of D8 also the static methods of Long and Char will be desugared Issue here. It is already fixed but it will take a bit of time before it hits the stable release of D8. After that this will not be an issue anymore.