【发布时间】:2017-07-08 18:47:16
【问题描述】:
我有一个提供 Retrofit 接口的模块。
@Module
class NetModule(val base: String) {
@Provides
@Singleton
fun provideOkHttpClient(): OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(object: Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
info("Request for ${request.url()}")
return chain.proceed(request)
}
}).build()
}
@Provides
@Singleton
fun provideGson(): Gson {
return GsonBuilder()
.enableComplexMapKeySerialization()
.serializeNulls()
.setPrettyPrinting()
.setLenient()
.create()
}
@Provides
@Singleton
fun provideRetrofit(OkHttpClient: OkHttpClient, Gson: Gson): Retrofit {
return Retrofit.Builder()
.baseUrl(base)
.client(OkHttpClient)
.addConverterFactory(GsonConverterFactory.create(Gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
.build()
}
@Provides
@Singleton
fun provideIdService(Retrofit: Retrofit): IdService {
return Retrofit.create(IdService::class.java)
}
}
NetModule 与NetComponent 一起使用
@Singleton
@Component(modules = arrayOf(NetModule::class))
interface NetComponent {
fun inject(application: Application)
fun inject(activity: Activity)
}
在应用程序中注入并存储在应用程序伴随对象中。
netComponent = DaggerNetComponent.builder().netModule(NetModule("some_url")).build()
netComponent.inject(this)
在我的活动中
@Inject lateinit var idService: IdService
这会产生构建错误
如果没有@Provides 或@Produces 注释方法,则无法提供
IdService。
如果我尝试注入 Retrofit 实例,则会收到不同的错误
无法访问 Nullable
堆栈跟踪显示 javax.annotation.Nullable 的类文件未找到。
我找不到任何引用此错误的内容。 12 小时前有一篇 StackOverflow 帖子似乎有同样的错误,但它已被删除。 https://stackoverflow.com/questions/44983292/cannot-access-nullable-on-injecting-with-dagger2-in-kotlin
【问题讨论】:
-
能否请您添加更多错误详情。
-
您找到解决方案了吗?
-
我想我最终创建了一个新的空白项目,因为我没有写太多。项目在这里github.com/tpb1908/HNKotlin/tree/master/app/src/main/java/com/…
-
你有解决办法吗?