I'm trying to convert my Android library to a Kotlin multiplatform library.
One of the things I want to preserve are all the android specific annotations for Android Lint. I was able to convert most of them by doing simple things like
@MustBeDocumented
@Retention(AnnotationRetention.BINARY)
@Target(
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER,
AnnotationTarget.CONSTRUCTOR,
AnnotationTarget.ANNOTATION_CLASS,
AnnotationTarget.CLASS,
AnnotationTarget.VALUE_PARAMETER
)
expect annotation class MainThread()
actual typealias MainThread = androidx.annotation.MainThread
This did not work with RestrictTo
because it takes an argument.
The android RestrictTo
annotation looks like
@Retention(CLASS)
@Target({ANNOTATION_TYPE,TYPE,METHOD,CONSTRUCTOR,FIELD,PACKAGE})
public @interface RestrictTo {
/**
* The scope to which usage should be restricted.
*/
Scope[] value();
enum Scope {
}
}
I cannot seem to make the compiler happy with the type for value.
If I make the expect look like
@Target(
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER,
AnnotationTarget.FIELD,
AnnotationTarget.CONSTRUCTOR,
AnnotationTarget.ANNOTATION_CLASS,
AnnotationTarget.CLASS
)
@MustBeDocumented
@Retention(AnnotationRetention.BINARY)
expect annotation class RestrictTo(vararg val value: RestrictScope)
I get a compile error
public expect final val value: Array<out RestrictScope /* = RestrictTo.Scope */> The following declaration is incompatible because return type is different: public final val value: Array<RestrictTo.Scope>
If I change the value from a vararg to an Array I get this error.
public constructor RestrictTo(value: Array<RestrictScope /* = RestrictTo.Scope */>) The following declaration is incompatible because parameter types are different: public constructor RestrictTo(vararg value: RestrictTo.Scope)
Is there anyway to make the types work for both the constructor and the values method?
This is a bug - https://youtrack.jetbrains.com/issue/KT-20900
Feel free to upvote the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With