2

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

epool
  • 6,710
  • 7
  • 38
  • 43
  • Where is the source generated, is the directory added to the source set? – EpicPandaForce May 23 '18 at 16:38
  • @EpicPandaForce the source generated is in `...build/generated/source/kapt/debug/...` it's the same directory used by dagger and other annotation processors, my Android Studio recognizes the generated class and I can use the class inside of a method, but not as a class member. – epool May 23 '18 at 16:45

1 Answers1

2

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.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • Actually, I created the project with Kotlin support from Android Studio, maybe AS is not adding it by default and it can be resolved in the new kotlin gradle plugins, but not sure. I just pushed the change for adding the kotlin runtime dependency, which didn't help. Anyways good catch. – epool Jun 02 '18 at 03:17