Dagger2 cannot access nullable. javax.annotation.Nullable not found

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

}

      

NetModule

used with NetComponent

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

    fun inject(application: Application)

    fun inject(activity: Activity)
}

      

which is entered into the application and stored in the application companion object.

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

      

In activity I then

@Inject lateinit var idService: IdService

      

This gives a build error

IdService

cannot be provided without a @Provides or @Produces annotated method.

If I try to inject an instance Retrofit

instead, I get another error

cannot access Nullable

The stack shows the class file for javax.annotation.Nullable not found.

I couldn't find anything referring to this error. There is a StackOverflow post from 12 hours ago that 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

+3


source to share


2 answers


I was getting javax.annotation.Nullable not found error which I was able to fix by adding to findbugs library which contains Nullable annotation.

If you are using gradle add the following dependency:



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

+2


source


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

This is because you didn't specify your dependency where it is present IdService

. So the dagger doesn't know how to provide IdService

it to him.

NetModule is used with NetComponent, which is injected into the application and stored in the application companion object.



You need to inject the dependencies where you want to use it (here yours Activity

)

So write an injection in your activity OnCreate

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

      

0


source







All Articles