7

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

4

2 Answers 2

17

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'

3
  • 6
    You can also write the short version: implementation 'com.google.code.findbugs:jsr305:3.0.2' Jan 10, 2018 at 13:54
  • 3
    Can anyone explain the rationale behind this solution? And why exactly the problem occurs in the first place Sep 6, 2019 at 7:10
  • well. this is works and fix my dagger issue, when providing a string data type in kotlin
    – mochadwi
    Oct 23, 2019 at 15:41
0

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

This is because you have not injected your dependency where IdService is present. So dagger doesn't know how to provide IdService to it.

The NetModule is used with NetComponent Which is injected in the application and stored in the applications companion object.

You have to inject the dependencies where you want it to be used (here your Activity)

So write the injection in your Activity's OnCreate

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.