Kotlin/KAPT Generated Kotlin class is not recognized as class member, but it does inside of methods

3.4k Views Asked by At

I have written an annotation processor that generates a builder class for my classes annotated with @DataBuilder

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
annotation class DataBuilder

My classes annotated with this annotation are located in the com.my.package.model package and the generated builder class is also located in the same package com.my.package.model but in the generated directory of course build/generated/source/kapt/debug/com/my/package/model/MyModelBuilder.kt, I can use these generated classes fine inside of my model classes(written in Kotlin)

BUT I can NOT use the generated MyModelBuilder Kotlin class inside of a java class as a class member

package com.my.package.home;
import com.my.package.model.MyModelBuilder;
public class Home {
    MyModelBuilder builder; // <=== AS recognizes the class, but I'm having an compilation issue
}

Android Studio recognizes the class, but I’m having this compilation issue

com/my/package/home/Home.java:4: error: cannot find symbol
MyModelBuilder builder;
           ^
  symbol:   class MyModelBuilder
  location: class Home

it’s weird because I can use this generated builder class only inside of methods, this code compiles fine:

package com.my.package.home;
import com.my.package.model.MyModelBuilder;
public class Home {
    public void hello() {
        MyModelBuilder builder;
    }
}

could somebody here help me to understand this behavior and how to fix this? In advance, thanks!

UPDATE

I just created this repo with the necessary code to replicate the issue https://github.com/epool/HelloKapt

The project works fine after cloning and running, to replicate the issue please un-comment this line https://github.com/epool/HelloKapt/blob/master/app/src/main/java/com/nearsoft/hellokapt/app/MainActivity.java#L13

Note: If I convert my MainActivity.java class to Kotlin(MainActivity.kt) the issues is NOT reproducible and works fine, but I don’t want to do so due to some project limitations so far

Kotlin Issue: https://youtrack.jetbrains.net/issue/KT-24591

1

There are 1 best solutions below

1
On

Looking at your Github project, I notice that you don't declare a dependency on kotlin-stdlib-jdk7 in the app module. When I build the module, compiler emits the following warnings:

warning: unknown enum constant AnnotationTarget.CLASS
  reason: class file for kotlin.annotation.AnnotationTarget not found   
warning: unknown enum constant AnnotationRetention.SOURCE
  reason: class file for kotlin.annotation.AnnotationRetention not found    
warning: unknown enum constant AnnotationTarget.CLASS
  reason: class file for kotlin.annotation.AnnotationTarget not found   

Since kotlin-stdlib-jdk7 is declared as implementation in the annotations module, the app module doesn't see it as a transitive dependency, that might be the reason why compilation fails. To fix it, you should probably declare the correct dependency in the app module, or at least use apiElements scope for kotlin-stdlib-jdk7 in annotations.

The fact that the IDE doesn't notify you that compilation failed might be a tools bug, but there's definitely no underlying Kotlin compiler issue.