Dagger2无法访问可为空的对象.找不到javax.annotation.Nullable [英] Dagger2 cannot access nullable. javax.annotation.Nullable not found

查看:294
本文介绍了Dagger2无法访问可为空的对象.找不到javax.annotation.Nullable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个提供改造接口的模块.

@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实例,则会收到另一个错误

无法访问可为空

stacktrace显示javax.annotation.Nullable的类文件.

我找不到任何引用此错误的信息. 从12小时前开始有一个StackOverflow帖子,似乎有同样的错误,但已被删除. https://stackoverflow.com/questions/44983292/cannot-在kotlin中使用dagger2进行访问可空访问

解决方案

我遇到了javax.annotation.Nullable not found错误,我可以通过添加包含Nullable注释的findbugs库来解决此错误. >

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

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

I have a module to provide a Retrofit 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)
    }

}

The NetModule is used with NetComponent

@Singleton
@Component(modules = arrayOf(NetModule::class))
interface NetComponent {

    fun inject(application: Application)

    fun inject(activity: Activity)
}

Which is injected in the application and stored in the applications companion object.

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

In the Activity I then have

@Inject lateinit var idService: IdService

This gives a build error

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

If I attempt to inject the Retrofit instance instead, I get a different error

cannot access Nullable

The stacktrace shows class file for javax.annotation.Nullable not found.

I haven't been able to find anything referencing this error. There is a StackOverflow post from 12 hours ago which seems to have the same error, but it has been removed. https://stackoverflow.com/questions/44983292/cannot-access-nullable-on-injecting-with-dagger2-in-kotlin

解决方案

I was getting the javax.annotation.Nullable not found error, which I was able to fix by adding in the findbugs library which includes the Nullable annotation.

If you are using gradle add the following dependency:

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

这篇关于Dagger2无法访问可为空的对象.找不到javax.annotation.Nullable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆