Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 androidx fragment incompatible types

I'm using Dagger 2.21 and when I try to do

@Module
internal abstract class FragmentModule {
    @ContributesAndroidInjector
    internal abstract fun loginFragment() : LoginFragment
}

and

@Singleton
@Component(modules = [AndroidSupportInjectionModule::class, AppModule::class, ActivityModule::class, ViewModelBuilder::class, ViewModelModule::class, RepositoriesModule::class, ApiModule::class, FragmentModule::class])
interface AppComponent : AndroidInjector<PhotocoApplication> {
    @Component.Builder
    abstract class Builder : AndroidInjector.Builder<PhotocoApplication>()
}

I get this error:

/app/build/generated/source/kapt/debug/com/photoco/app/injection/module/FragmentModule_LoginFragment$app_debug.java:18: error: incompatible types: Class LoginFragment cannot be converted to Class extends Fragment

I have been searching and saw that using 2.21 and setting this gets it to work but no luck yet

android.useAndroidX=true ; android.enableJetifier=true

LoginFragment extends:

dagger.android.support.DaggerFragment()

With all this setup can't get it to build, am I missing something here? I can make it work with Activities using DaggerActivity but not with Fragments.

PhotocoApplication extends dagger.android.support.DaggerApplication

Thanks!

like image 286
Emanuel Amiguinho Avatar asked Jan 30 '19 18:01

Emanuel Amiguinho


People also ask

Why to use Dagger in Android?

Dagger automatically generates code that mimics the code you would otherwise have hand-written. Because the code is generated at compile time, it's traceable and more performant than other reflection-based solutions such as Guice. Note: Use Hilt for dependency injection on Android.

What is Dagger dependency injection?

Dagger is a fully static, compile-time dependency injection framework for Java, Kotlin, and Android. It is an adaptation of an earlier version created by Square and now maintained by Google.


1 Answers

Fixed this issue by updating all dagger dependencies to 2.21, was missing android-support (was still using 2.16).

implementation 'com.google.dagger:dagger:2.21'
implementation 'com.google.dagger:dagger-android:2.21'
implementation 'com.google.dagger:dagger-android-support:2.21'
kapt "com.google.dagger:dagger-compiler:2.21"
kapt "com.google.dagger:dagger-android-processor:2.21"
like image 107
Emanuel Amiguinho Avatar answered Oct 03 '22 23:10

Emanuel Amiguinho