使用Kotlin创建自定义Dagger 2范围

Abb*_*bba 9 java android kotlin dagger-2

我正在尝试将Java代码转换为Kotlin以创建自定义匕首.

这是Java代码:

@Documented
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomScope {
}
Run Code Online (Sandbox Code Playgroud)

一旦转换成kotlin,结果就是这里

@Scope
@Documented
@Retention(RetentionPolicy.RUNTIME) annotation class CustomScope 
Run Code Online (Sandbox Code Playgroud)

我有一个类型不匹配@Retention(RetentionPolicy.RUNTIME).我有以下错误消息:必需类型是AnnotationRetention但找到了RetentionPolicy类型.

@interface似乎也被取代了.

jay*_*i93 27

Retention您可能使用的注释类来自Kotlin的库(来自包kotlin.annotation).

它期望枚举类型的属性AnnotationRetention.所以,你可以这样做:

@MustBeDocumented
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class CustomScope
Run Code Online (Sandbox Code Playgroud)

顺便说一下,如果你看一下这个Annotations.kt文件,你就会发现当你没有传递任何东西时,Retention注释会采用默认属性AnnotationRetention.RUNTIME.

所以,只需@Retention注释也可以.