Search code examples
androidannotationskotlindaggerbutterknife

Kotlin annotation processing ignores items with similar names


I recently converted the majority of my project to kotlin. Now I encounter several unusual errors that all seem to relate to annotation libraries. Needless to say, it didn't happen in Java.

I'll describe the cases - one in Dagger and one in Butterknife.

1. When having 2 @Provides methods in different models with the same name. For example in file "FooProvider.kt" having a "provideFooOrBar" method

@Module
class FooProvider(private val view: FooActivity) {
    ... 
    @Provides @FooScope fun provideView() = view
    @Provides @FooScope fun provideFooOrBar() = Foo()
}

And having another file "BarProvider.kt" with the same method name

@Module
class BarProvider(private val view: BarActivity) {
    ...
    @Provides @BarScope fun provideView() = view
    @Provides @BarScope fun provideFooOrBar() = Bar()
}

In this case, Dagger fails to generate some factory libraries and I get the following compilation error: Error:(27, 32) error: cannot find symbol class FooProvider_ProvideFooOrBarFactory

A sample project reproducing the issue can be found at https://github.com/maxandron/DaggerIssue325

2. This is an issue when using Butterknife. When having two @Bind annotated variables in two different classes - One of them just fails to initialize at runtime without any compilation error!

For example if I have:

class FooActivity {
    @Bind(R.id.foo) lateinit var mFoo: View
}
class NotFooActivity {
    @Bind(R.id.not_foo) lateinit var mFoo: View
}

Then one of them (or both?) will just fail to initialize without any error. Causing a kotlin.UninitializedPropertyAccessException: lateinit property mFoo has not been initialized exception to be thrown when the field is accessed.


Is it something I'm doing wrong in configuring Kotlin or is it a kotlin bug?

Thank you in advance! Ron


Solution

  • It turned out to be a bug with kapt. I posted an issue on Kotlin's bug tracker and the issue is now marked as fixed.

    This solution was merged

    Should be resolved in Kotlin version 1.0.2