gpt4 book ai didi

kotlin - Dagger2 无法访问可为空的。 javax.annotation.Nullable 未找到

转载 作者:行者123 更新时间:2023-12-04 09:58:05 24 4
gpt4 key购买 nike

我有一个模块来提供改造接口(interface)。

@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)
}

}
NetModuleNetComponent 一起使用
@Singleton
@Component(modules = arrayOf(NetModule::class))
interface NetComponent {

fun inject(application: Application)

fun inject(activity: Activity)
}

它被注入(inject)应用程序并存储在应用程序伴随对象中。
netComponent = DaggerNetComponent.builder().netModule(NetModule("some_url")).build()
netComponent.inject(this)

在事件中我有
@Inject lateinit var idService: IdService

这会产生构建错误

IdService cannot be provided without an @Provides or @Produces annotated method.



如果我尝试注入(inject) Retrofit相反,我得到一个不同的错误

cannot access Nullable



堆栈跟踪显示找不到 javax.annotation.Nullable 的类文件。

我找不到任何引用此错误的内容。
12 小时前有一个 StackOverflow 帖子似乎有同样的错误,但它已被删除。
https://stackoverflow.com/questions/44983292/cannot-access-nullable-on-injecting-with-dagger2-in-kotlin

最佳答案

我收到了 javax.annotation.Nullable not found 错误,我可以通过添加包含 Nullable 注释的 findbugs 库来修复该错误。

如果您使用的是 gradle,请添加以下依赖项:
implementation 'com.google.code.findbugs:jsr305:3.0.2'

关于kotlin - Dagger2 无法访问可为空的。 javax.annotation.Nullable 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44989650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com