Search code examples
javakotlindagger-2

Error:cannot access NotNull


I encounter the error above when compiling, and it doesn't goes to to any source file. After slowly identify the changes that cause the issue, found out it is apparently the dagger 2 inject is causing issue.

@FragmentScope
@Subcomponent
public interface MyComponent {
    void inject(MyJavaFragment myJavaFragment);

    void inject(MyJavaClass myJavaClass);

    void inject(MyKotlinClass myKotlinClass); // <- if change to Java, all works
}

I converted MyKotlinClass from Java code to Kotlin, and the error happens. This is in a library, and I already use that in build.gradle

kapt "com.google.dagger:dagger-compiler:$daggerVersion"

Why? (note: the app code is originally Java, so I didn't change all to Kotlin yet unless needed).


Solution

  • After long hours of investigation, found out that, the calling Feature to this library, still using the Java Annotation

    annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
    

    But my library is using kapt as mentioned in the question above. So that's a clash of annotation processor used, and Java annotation processor is used instead I suspect. By changing the calling library annotation processor to

    kapt "com.google.dagger:dagger-compiler:$daggerVersion"
    

    would solve the problem. (remember to add apply plugin: 'kotlin-kapt' at the top as well)