I am facing one issue using dagger2 + Kotlin This...
# android
p
I am facing one issue using dagger2 + Kotlin This is my ModuleClass
Copy code
object AppModule {

    @JvmStatic
    @Provides
    fun provideRequestOptions() = RequestOptions.placeholderOf(R.drawable.white_background)
            .error(R.drawable.white_background)

    @JvmStatic
    @Provides
    fun provideGlideInstance(application: Application, requestOptions: RequestOptions) =
            Glide.with(application).setDefaultRequestOptions(requestOptions)

    @JvmStatic
    @Provides
    fun provideAppDrawable(application: Application) =
            ContextCompat.getDrawable(application, R.drawable.logo)
}
In my Activity I am using 2 injects
Copy code
@Inject
    lateinit var drawableLogo: Drawable

    @Inject
    lateinit var requestManager: RequestManager

    fun setLogo() = requestManager.load(drawableLogo).into(loginLogo)
I am getting below error:-
Copy code
/app/build/tmp/kapt3/stubs/debug/com/example/di/AppComponent.java:7: error: [Dagger/Nullable] android.graphics.drawable.Drawable is not nullable, but is being provided by @org.jetbrains.annotations.Nullable @Provides android.graphics.drawable.Drawable com.example.di.AppModule.provideAppDrawable(android.app.Application)
public abstract interface AppComponent extends dagger.android.AndroidInjector<com.example.BaseApplication> {
                ^
      android.graphics.drawable.Drawable is injected at
          com.example.AuthActivity.drawableLogo
      com.example.AuthActivity is injected at
          dagger.android.AndroidInjector.inject(T) [com.example.di.AppComponent → com.example.di.ActivityBuildersModule_ContributeAuthActivity$app_debug.AuthActivitySubcomponent]

FAILURE: Build failed with an exception.
I think this issue is somehow related to
@Nullable
annotation used by the IDE but I am not sure about the exact issue, can somebody explain about this issue?
stackoverflow 2
d
>>
⚠️ Using Kotlin to build Android applications. google can answer many questions. Android Studio Integration. Android-specific Gradle. If not related to Kotlin, please consider using StackOverflow and/or http://android-united.community/.
m
You’re providing a drawable which is nullable ( ContextCompat.getDrawable returns Drawable? type ) but expecting a non-nullable injection in your field. Try to use non-nullable return type in module or mark your drawableLogo as nullable.
k
Tip: Always explicitly specify the return type of provides methods. Saves you a lot of unnecessary stress 😄
p
thx @Melih Aksoy, apparently I ran into another problem after changing
Drawble
to
Drawable?
, as
lateinit
can’t be used with nullable types.
Copy code
@Inject
lateinit var drawableLogo: Drawable? = null
then I needed to change
lateinit
to something like
internal
but then it gave another error “Dagger doesn’t support injection into private fields. then finally I used something like
Copy code
@set:Inject
internal var drawableLogo: Drawable? = null
but I don’t understand how this is working 😐