Hot questions for Using Butter Knife in kotlin

Question:

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?


Answer:

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.

Question:

I still use Butterknife on my Kotlin project but for only binding colours and drawables as there's no need for that on binding views. However, after updating my project to AndroidX I can't get the library to work anymore.

That's what I have

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

kapt {
generateStubs = true
}

implementation 'com.jakewharton:butterknife:10.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
kapt 'androidx.databinding:databinding-compiler:3.5.0-alpha02'
kapt 'com.android.tools.build.jetifier:jetifier-core:1.0.0-beta02'

ext.kotlin_version = '1.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

AS 3.3.1

And call them this way

 @JvmField
 @BindColor(R.color.just_pink)
 var pink: Int = 0

 @JvmField
 @BindDrawable(R.drawable.rectangle_pink_btn_all_radius)
 var rectanglePinkAllRadius: Drawable? = null

Making sure I have Butterknife.bind(this, view) on my onCreate method.

Thanks for your help.


Answer:

In Kotlin you specify the dependencies in a similar to Java way using Kotlin Annotation processing tool (kapt) instead of annotationProcessor.

So replace

annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'

with this

kapt 'com.jakewharton:butterknife-compiler:10.0.0'