【问题标题】:Dagger2 cannot access nullable. javax.annotation.Nullable not foundDagger2 无法访问可为空的。 javax.annotation.Nullable 未找到
【发布时间】: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)
    }

}

NetModuleNetComponent 一起使用

@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

【问题讨论】:

标签: kotlin retrofit2 dagger-2


【解决方案1】:

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

如果您使用的是 gradle,请添加以下依赖项:

implementation 'com.google.code.findbugs:jsr305:3.0.2'

【讨论】:

  • 也可以写短版:implementation 'com.google.code.findbugs:jsr305:3.0.2'
  • 谁能解释一下这个解决方案背后的原理?以及为什么首先会出现问题
  • 好吧。在 kotlin 中提供字符串数据类型时,这是可行的并解决了我的匕首问题
【解决方案2】:

没有@Provides 或@Produces 就不能提供IdService 注释方法。

这是因为你没有在 IdService 存在的地方注入你的依赖。所以dagger不知道怎么给它提供IdService

NetModule 与注入的 NetComponent 一起使用 应用程序并存储在应用程序伴随对象中。

您必须在您希望使用它的地方注入依赖项(这里是您的Activity

所以在你的Activity的OnCreate中写下注入

netComponent = DaggerNetComponent.builder()
        .netModule(NetModule("some_url"))
        .build()
        .inject(this)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2015-12-14
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    相关资源
    最近更新 更多