Search code examples
androidkotlinbutterknife

Butterknife On Kotlin Not Working For Binding Colours And Drawables AndroidX


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.


Solution

  • 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'