Jeff Padgett
Jeff Padgett

Reputation: 2549

Custom Java Annotation not working with Kotlin project

I am writing an Android app and created a Java file with an annotation that I am using in Kotlin classes. I am using kapt annotation processor.

Everything seems to show no errors in the compiler, but when I run the app, I get an error: incompatible types: NonExistentClass cannot be converted to Annotation - @error.NonExistentClass()

Is this just a kapt limitation? I read something about how Kotlin stubs out functions (but I think keeps declaration site). So that might mean it doesn't know what to do since the parameter in function declaration is marked with the annotation?

See: kapt replaces generated class references with error.NonExistentClass despite kapt.correctErrorTypes being enabled Any ideas how to resolve this?

Java Annotation File:

@IntDef(value = {Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC, Typeface.BOLD_ITALIC})
@Retention(RetentionPolicy.SOURCE)
public @interface MyTypefaceAnnotation { }

Kotlin Usage File:

sealed class MySealedClass(
    @StringRes val text: Int,
    @MyTypefaceAnnotation val typeface: Int,
    @ColorRes val fontColor: Int,
    val isEnrollmentButtonEnabled: Boolean
) {
    object NotSelected :
         MySealedClass (
            R.string.please_select,
            Typeface.NORMAL,
            R.color.light_grey,
            false
        )

// etc

Upvotes: 0

Views: 585

Answers (0)

Related Questions