AndroidAnnotations or Butterknife

Hello there,

I’ve been trying to use AndroidAnnotations or Butterknife with my Kotlin project, I knew it wasn’t possible but apprently with 1.0 RC it should be possible but until now I’m having no luck and maybe I understood it wrong and it only supports those libraries on Java Code.

So the code compiles and everything but the properties are never initialized (talking about @ViewById and/or @Bind).

Does the kapt works with kotlin classes or it only supports Java Classes?

Thanks in advance

It is working for me… there are some extra setup… in your module gradle file add:

dependencies {
  //...
  compile 'com.jakewharton:butterknife:7.0.1'
  kapt 'com.jakewharton:butterknife:7.0.1'
}

//ButterKnife For Kotlin
kapt {
  generateStubs = true
}

Daniel probably answered your question, but as far as getting hold of your views in Kotlin there are additional choices with Kotlin:

  • Kotterknife is a Kotlin property delegate library that handles binding your views to variables (but not the rest of Butterknife). Unfortunately, there STILL is no official release of it so you either have to use a snapshot build of http://jitpack.io
  • Kotlin Android extensions generates extension properties that let you get views. I personally could never get this to work, but the latest RC build supposedly made this a lot better.
  • Anko Lets you replace XML layout files with type safe builders in your code and is actually much faster than inflating XML layouts

Be warned that the documentation for Kotlin Android Extensions and Anko is woefully out of date and you will have to do some work to apply them.

The answers below are technically incorrect, butterknife uses a specific component of javac to reverse lookup the resources. If you look at the generated classes you’ll notice that all the resources are hardcore numbers instead of R.id.abc. to means that as long as your project is done and doesn’t require a lot of resource merging, you might be about to get away with it. Normally, the hard codes number will be replaced by the resource parameter. There are open bugs on butterknife and kotlin in hopes that this will be fixed in the future, it’s a shame to lose support for such a popular library. For now, I’d recommend the extensions library.