Does butterknife 7.x work with Kotlin M14?
Asked Answered
N

2

10

I'm trying to use Butterknife with some Kotlin code and also Java code. I know that before M12, there was bad or no support for annotation processing that ButterKnife required. So I have kept my activities in Java. It was working at least in Java with Butterknife 6.x and preM12 Kotlin. I'm trying now butterknife 7.x with M13 and M14. It should have even annotation processing support, but it's not working for me. bind() function doesn't bind anything in my adapter which is written in Java nor in activity written in Kotlin.

I'm using this in build.gradle (tried latest version on Github):

apply plugin: 'com.neenbedankt.android-apt'

dependencies {
   provided files('libs/butterknife-annotations.jar')
   kapt files('libs/butterknife-compiler-8.0.0-SNAPSHOT.jar')
   compile 'com.jakewharton:butterknife:8.0.0-SNAPSHOT@aar'
}

This doesn't compile. I tried also 'com.neenbedankt.android-apt' which does compile but binding is not working.

I know that there is probably not support in butterknife for that yet. But is there any hack to get it working?

Nylons answered 5/10, 2015 at 15:16 Comment(5)
JakeWharton has separately released KotterKnife, isn't that what you need? github.com/JakeWharton/kotterknifeTrivial
Alternatively, there are the Kotlin Android Extensions. No need for Butterknife.Diastrophism
Yes, I know about these options. I'm going with anko. But thanks for response.Mcintyre
also there is a tool [kapt][1] that is annotation processing tool for kotlin. Maybe it has solved all problems that were the starting point for developing Kotterknife [1]: blog.jetbrains.com/kotlin/2015/05/…Clutter
Kotlin Android Extensions is very buggy. Sometimes when I start Android Studio all views provided by synthetic marked with red color. Also this does not work correct with fragment. KollerKnife is very straightforward and lightweight solution. Also this PR: github.com/JakeWharton/kotterknife/pull/7 implements reset feature, so you can use kotterknife with fragments.Tini
T
13

It does work with the current version of Kotlin (1.0.0-beta-3595), I suggest you to take a look at the android-butterknife project which can be found inside the JetBrains's kotlin-examples repo. In short all you need to do is:

  1. Add the following to your app/build.gradle file:

    kapt {
        generateStubs = true
    }
    
  2. Put the following line inside the dependencies block of the same build.gradle file (assuming you already added compile 'com.jakewharton:butterknife:7.0.1' to your dependencies):

    kapt 'com.jakewharton:butterknife:7.0.1'
    

And that should be it.

Tungus answered 18/12, 2015 at 13:34 Comment(2)
Also confirmed with stable kotlin 1.0.0.Mcintyre
As of butterknife 8.x, you now run kapt on the compiler lib instead of a second reference to butterknife itself, i.e. kapt 'com.jakewharton:butterknife-compiler:8.0.1'. JetBrains' example project linked in the answer above has been updated to reflect this change.Globetrotter
V
3

Butterknife is supported. Use kapt: [1], [2].

Note that Butterknife does not support private Java fields, so you can use the lateinit modifier to make it public.

Also, if you use kapt, apply plugin: 'com.neenbedankt.android-apt' line is not needed anymore.

Vair answered 26/10, 2015 at 11:32 Comment(1)
How to make Butterknife set onClicks and etc with kapt?Washedup

© 2022 - 2024 — McMap. All rights reserved.